Search code examples
javacomparestategraphics2d

Detect changes in Graphics2D object in Java


I'm looking for an efficient way to detect any changes to a Graphics2D object between discrete stages of rendering (so that I can optimise by aborting further superfluous stages once the first "no change" is discovered).

Is there a way to compare memory, or perhaps a checksum, or any other way to compare two states in order to identify (or rule out) change?


Solution

  • Depending on from what context your generating the graphics context, maybe, but I'd be very in efficient.

    Instead you could maintain a "current state" BufferedImage, when you update your model, have it determine if any changes occurred. If the model was changed, create a temporary BufferedImage, re-render this new state and then assign it to the "current state" BufferedImage.

    Each time you want to render to the screen, simply use the "current state" BufferedImage