I was trying to draw a circle in JPanel
with paintComponent
, when I call repaint()
every time it remove all original paint. But when I remove super
, it will show the trace of circle moving. So how should I do for keeping the circle after moving without showing the trace of it?

- 168,117
- 40
- 217
- 433

- 1
-
After reading that several times, I'm still not sure of the effect you'd like to produce. If you want to keep previous circles, draw everything to an image and display the image. If not, use `super.paint(g)` as first line of the overridden method. – Andrew Thompson Apr 12 '17 at 23:03
2 Answers
Painting in Swing is destructive, that's how it works.
When a paint cycle occurs, you are expected to repaint the component state from scratch.
This would suggest that you need to maintain some kind of model which the paint methods can use to repaint the state in it's entirety
I would recommend having a look at Painting in AWT and Swing for more details about how painting works

- 343,457
- 22
- 230
- 366
Start by reading the section from the Swing tutorial on Custom Painting. It shows how to draw a square at the location where the mouse is clicked.
So your logic would be similar except that instead of using the mouse to determine the new location of the circle you will use your programmed logic to change the x/y location of the circle.
You can use a Swing Timer
to schedule this animation. The tutorial also has a section on How to Use Swing Timers
which contains a working example to get you started.

- 321,443
- 19
- 166
- 288