I've just started working with java 2d graphics applications, on my studies repaint is redrawing our graphics wasting a lot of resources. but I want to know what repaint is, does and how to use it efficiently, thread safely and fast for many movable dynamic objects on my canvas?
I would start by having a read through Performing Custom Painting and Painting in AWT and Swing
repaint
makes a request to the RepaintManager
to paint part or all of a component. The RepaintManager
will decide what and how much will be painted, possible consolidating repaint requests into as small a number of updates as possible (so repeatedly calling repaint
could actually slow your paint process).
The RepaintManager
then pushes a paint
event onto the Event Dispatching Thread. This ensures that the paint event is handle within the context of the EDT.
There are many possible solutions for improving speed and resource management when it comes to painting in Swing.
You could consider implementing your own double buffering strategy, painting your updates to an off screen buffer and when ready, switching to the active buffer, which will get painted.
This means that the paint is quick as all the work has already being done (presumably in a background thread).
For examples...
You could also take a look at Passive vs. Active Rendering, but I'd be very sure that you know what you're getting yourself in for...