• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Detecting a mouse click inside a box painted in Frame?

 
Ranch Hand
Posts: 110
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello,

I'm a beginner to Java programming.

Would someone look at this code and tell me where I've gone wrong?

In the method mouseClicked(MouseEvent me) the co-ords of the click are obtained.
* if they are within the confines of the rectangle rect, console output System.out.println("inside box") is displayed
* in either case the click co-ords are displayed in the console

I'm getting the co-ords of the mouse clicks but no message when the click is inside the box

Any help most welcome.

Thank you.










 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question looks like it is better suited to the Swing/AWT forum, so I moved it there.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I ask why you're using AWT, which was superseded by Swing about 15 years ago?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from that, your ARectangle class has its own fields x, y, w, h which shadow the fields in its superclass. The corresponding fields in teh superclass -- which will be used by its contains(...) method -- will all be 0.
 
Mohammed Azeem
Ranch Hand
Posts: 110
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Daryl.

Your comment first of all made me realise that:

* for this experiment, that is catch a mouse click within a painted box, I don't need to subclass Rectangle - just create a new Rectangle object.
* I need to re-visit inheritance and fully understand the pitfalls of hidden fields.

By the way, I'm using AWT before moving on to Swing. I think it will a good grounding for it.

The problem is resolved.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, AWT is 16 years out of date and Swing is nearly obsolete. If you are starting GUIs, go for JavaFX.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, AWT is 16 years out of date...



... and learning it will teach you bad habits for Swing and JavaFX.
 
Mohammed Azeem
Ranch Hand
Posts: 110
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One learns from one's stupidity. After all, at 50 years old, I can't think as fast as the young things!

I've realised my mistake. I hope my analysis is correct. So here it is:

Just because I can store the dimensions of a rectangle (in say the variables xcoord, ycoord, wide and high and then use g.fillRect(...) to paint it doesn't mean I've created a rectangle object - even though I may have subclassed Rectangle and declared xcoord, ycoord ....etc., within that subclass (or worse still, declared variables that hid the instance variables of Rectangle).

An instance of the subclass wouldn't "know" what the new variables were for and the inherited instance variables (x, y, width, height) would all be zero - thus creating a rectangle (0,0,0,0).

An application of the .contains(mouse click coords) to an instance of such a subclass would thus always return false because a mouse click would never fall within a physically non-existent rectangle.

So the experiment succeeded after these changes:

* I wanted to subclass Rectangle because I wanted a Rectangle class that 'knew' its colour. It contained a new instance variable of type Color
* In the subclass's constructor I used super to call the Rectangle(int x, int y, int width, int height)

Everything then worked as originally planned for.

Thank you for your help.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammed Azeem wrote:I've realised my mistake. I hope my analysis is correct.


It is, and sharing that explanation deserves a Cow!
 
reply
    Bookmark Topic Watch Topic
  • New Topic