0

There is a question in this textbook I am reading:

" How would you determine if the mouse is pointing to a particular object _ball which is declared to be an Ellipse2D.Double in a subclass of JPanel. the MouseEvent object is referenced by the parameted named "e". "

I am confused as to what it means by "mouse is pointing" does that mean the mouse is being clicked on the _ball? if that is the case, wouldn't the answer be to implement the java.awt.event.MouseListener on the _ball object and use the java.awt.event.MouseEvent class somehow?

steven
  • 57
  • 1
  • 5

1 Answers1

3

You can define what it means.

It could mean "clicking" on the ball as you have suggested. Or it could mean "moving over" the ball.

Either way you would add a MouseListener to the panel. Then you either need to add your code to the mousePressed() or mouseMoved() event. When the event fires you would need to get the mouse point of the event and then use the contains() method of the ellipse to determine if you "clicked" or are "moving over" the ball.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • when you say "add your code to the mousePressed() or mouseMoved() even" -- where are these methods located? in the JPanel? and when you say "add a MouseListener" does that mean implement MouseListener interface in the JPanel and create a private mouseListener class to handle the mouse point? – steven Jun 29 '13 at 01:40
  • @user2533249, Read the Swing tutorial on [How to Write a Mouse Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) for an example of using a MouseListener. – camickr Jun 29 '13 at 01:44
  • +1.... @user2533249 also see [this](http://stackoverflow.com/questions/12933592/clicking-on-a-drawn-object/12934769#12934769) answer which demonstrates camickr's solution – David Kroukamp Jun 29 '13 at 10:16