previous
contents
next

Code for Double Buffering

// Instance variables
Image offscreenImage = null;
Graphics offscreenGraphics = null;

public void update(Graphics g) {
   // Create the offscreen Graphics of the correct size
   if (offscreenImage == null) {
      offscreenImage = createImage(getSize().width, getSize().height);
      offscreenGraphics = offscreenImage.getGraphics();
   }
   // Paint into the offscreen Graphics
   background(offscreenGraphics);
   paint(offscreenGraphics);
   // Copy the offscreen onto the screen
   g.drawImage(offscreenImage, 0, 0, null);
}