Basically, painting is outside of your control and there is (very little) you can do about.
paintComponent
is called (indirectly) when the repaint manager decides that the component needs to be re-painted because of some event, such as the component been re-sized (directly or because the parent container was resized) or it has become displayable (now visible on the screen or added to a component that is displayable) and any number of system events.
The first thing you need to do (apart from calling super.paintComponent
before you do any custom painting) is to relinquish the illusion of control you might think you have over the paint process.
Next, you should read through Painting in AWT and Swing and understand how the painting process works.