I am making a program in which there is a square that changes its x and y positions when a key is pressed. The square moves but the the old square is still there. How do I remove/clear everything from a panel before I repaint it? Calling removeAll had no effect.
I am making a program in which there is a square that changes its
Share
Presumably your code includes custom
paintComponent()logic. The key thing to observe is what does your panel look like when you do not overridepaintComponent()? An empty (or cleared) panel:Thus the solution is to invoke the
paintComponent()method of the parent type on the panel, before executing your custompaintComponent()logic: