0

Hello,

I have a program where if I click anywhere in a JFrame, a ball appears(using fillOval).If I happen to click on the ball once it has appeared, how do I make it disappear?

Thank you!

Edited by sciprog1: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by sciprog1
0

If you use Shape objects for rendering, you can use their contains() method to determine if they are under the mouse. So if you created an Ellipse2D for your ball and used draw(Shape) to draw it, you could later determine when it is clicked on with a mouse listener and "undraw" (probably delete) it.

0

Hello Ezzaral,

Thank you!

If I am not using the Shape class and I am simply using the paint() function of JFrame, can I simply use g.setColor with no color in it? The idea was to repaint over the colored ball using no color. Is this even possible?

If no, I will look into the Shape class.

Edited by sciprog1: n/a

0

Well, the issue is keeping track of the coordinates of the shape so you know when you have clicked on it. That is where the contains() method comes in handy because it does the math for you to determine if a point is within its bounds.

The "erasing" part is just a matter of not drawing the shape at all when you repaint.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.