Basically, you've broken the paint chain.
When a paint cycle runs, a top level method is called (typically paint
), which then calls a chain of events to perform the actual painting.
Each element in the chain does a particular job and builds on each other, failure to honour this chain will cause you problems.
Generally, the Graphics
context for a window is a shared resource, meaning that during any given paint cycle, all the components that are painted share the same Graphics
context
To fix the initial problem of " it draws on what the user drew, as well as GUI components that the user has hovered over", you need to call super.paintComponent
, for example...
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D board = (Graphics2D) comp;
This will clear the Graphics
context from what was painted to it previous.
paintComponent
should remain protected
as the should no reason to allow anybody else to ever call it directly.
Take a look at Performing Custom Painting and Painting in AWT and Swing for more details