Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A MouseEvent object is not "passed" to an object of another class.

 
Mohammed Azeem
Ranch Hand
Posts: 51
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm extending the example used in The Java Tutorials: Lesson: Performing Custom Painting. http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html I've took it apart and learnt a great deal from it.

Very briefly, in the example a red square is painted on to a JPanel, and moves to a new location specified by either a mouse click or a mouse drag.


I successfully extended the example by having the square move only if the mouse is within its bounds and then dragged. In the original example, the drag start-point did not matter (the square would just move to the drag end-point).


Then thinking about further refining it, I decided on the following mini-specification:

TheRectangle class would define a rectangle and each rectangle object should:
* know its original location and dimensions.
* know its colour
* know if the mouse has been pressed or clicked upon itself
* Be able to compute its new location
* Have the responsibility of drawing itself.


ThePanel class would define a panel which:
* would listen for mouse events (in the final implementation just for mousePressed() and mouseReleased() )
* would paint the rectangle onto its surface
* repaint the background of the area vacated when the rectangle moved to a new location.


Now here is the problem:

During de-bugging, I discovered the MouseEvent object is not being passed to the rectangle object.
If it had been, lines 134-138 would have reported the location of the mouse released event?

Am I doing anything wrong? As always, any help will be most welcome.
I'm really sorry for making this post so long, especially as your'e doing this for free, but thankyou again.


TheRectangle Class





ThePanel class:





Display class (contains main method):








 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Azeem wrote:If it had been, lines 134-138 would have reported the location of the mouse released event?

Why do you think so? I don't see any place where you call testtheclick method. Is it an actual problem or your have forgot something while creating an example?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic