I have a problem with disappearing graphics on a JPanel which I'd like to describe. I have a class that extends JPanel that looks like this:
My Polygon class's draw method looks like this:
The idea is that whenever I draw a polygon to the screen I also add it to the arraylist of polygons (I do this in the constructor of the Polygon class) and so whenever the paintComponent method of the MyPanel class is automatically called I loop through the arraylist calling draw() on each of the polygons.
This is sort of work but there is a problem. Whenever I maximise, minimise or resize the frame in which the panel is a component the polygons disappear and only reappear when the frame is moved around the screen a bit.
Does anyone know what I'm doing wrong and how I might solve this issue?
I tried calling super.paintComponent but that didn't work. I have access to the Graphics object in the draw method but I don't know what use it will be.
thanks for that. It works but what I don't understand is why it works. Why by passing the graphics context in does the refresh happen automatically? In my draw method I've had access to the panel and I've been doing something like this: