I’m somewhat confused by the Swing paint process with animations in a windowed environment (not full screen).
In paintComponent(), I need to know two things:
- The region of the graphics buffer that has been damaged or invalidated since the last paint, provided by the graphics clip.
- The region of the model that has changed (is dirty) since the last paint and must be redrawn, provided by the associated model.
In the first case, the region might have been obscured and redrawn in a single-buffer graphics system, or the back buffer was lost and re-created in a double-buffer system. Either way, the clip area must be completely redrawn (background and sprites). When a region is damaged, a repaint() is automatically called with the region as a parameter that is accumulated into the next paint clip.
In the second case, if that part of the graphics buffer is preserved from the last paint (not in clip), I can decide whether a full redraw (background and sprites) or an update draw (erase old changed sprites and draw new) is more efficient. In many animations, the update draw is far more common and efficient. I should be able to schedule a paint without a clip since there is no damaged region. If the graphics clip is empty or does not intersect the dirty region, I can decide whether to just update the dirty region.
The problem is that the JComponent repaint() method calls the RepaintManager.addDirtyRegion() method, and that method throws away any call with a width or height parameter <= 0. There seems to be no way to schedule a paint for a model update when there is no damaged region.
My question is: What is the correct way in Java2D to implement a paint process that can tell when to update a preserved region and when to completely repaint a damaged region?