Don’t use getGraphics(). Any painting you do will be temporary and will be lost the next time Swing determines a component needs to be repainted.
Instead override the paintComponent()
method of a JComponent or JPanel to do your custom painting. See Custom Painting for more details and examples.