This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Detecting a mouse click inside a box painted in Frame?  RSS feed

 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • 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.










 
Knute Snortum
Sheriff
Posts: 3546
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question looks like it is better suited to the Swing/AWT forum, so I moved it there.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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: 56
3
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 56
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!