I am indeed calling getGraphics(); in the init() method of the JApplet..
That is the problem. The Graphics
object is a transient thing that will be repainted the very next time the JVM thinks there is any need to do so. That might be triggered by:
- Changing the size or location of the window.
- Covering it with another program and then removing the covering app.
- Adding new components or changing values that are displayed.
See Performing Custom Painting for more details on how to do what you are attempting to achieve. OTOH Swing has a JLabel
that can show a BufferedImage
. You can use the BufferedImage
in the way you want. When it is updated, call repaint()
on the label to see the effect.