Reputation: 57
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?
Upvotes: 0
Views: 91
Reputation: 324108
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.
Upvotes: 3