How do you paint shapes on a JPanel?
R Johnson wrote:For that matter bufferedImage is a great example of being able to draw on it using the createGraphics and what is drawn is stored on the Object...... It seems strange that the behavior would be different for a panel.
But a JPanel and a BufferedImage are two completely different animals and should not be expected to behave the same. The Graphics object obtained from a JPanel is only temporary and will be replaced any time the JPanel is repainted including repainting that is not under your control such as when the operating system requests a repaint of a portion of your app. So using this knowledge you will expect a JComponent's Graphics object will not to persist and adjust your code accordingly. Nothing strange here as long as you understand what's going on.
A great link that explains a lot of the inner workings of Swing painting is this one: Painting in Swing and AWT