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!

Recommended Answers

All 4 Replies

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.

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.

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.

Hello,

Got it! That was very helpful!

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.