/*

Java class to implement a double buffered applet

      by Ken Perlin @ NYU, 1998.

You have my permission to use freely, as long as you keep the attribution. - Ken Perlin

Note: this BufferedApplet.html file also works as a legal BufferedApplet.java file. If you save the source under that name, you can just run javac on it.

*/
import java.awt.*;

public abstract class BufferedApplet extends java.applet.Applet implements Runnable
{
   public boolean damage = true;            //Flag advising app. program to rerender
   public abstract void render(Graphics g); //App. program must define render method

   Image bufferImage = null;                //Image for the double buffer
   private Graphics bufferGraphics = null;  //Graphics context for double buffer
   private Thread t;                        //Background thread for rendering
   private Rectangle r = new Rectangle(0,0,0,0); //Bounding box for double buffer

   //Extend the start,stop,run methods to implement double buffering.

   public void start() { if (t == null) { t = new Thread(this); t.start(); } }
   public void stop()  { if (t != null) { t.stop(); t = null; } }
   public void run() {
      try {
         while (true) { repaint(); t.sleep(30); } //Repaint every 30 milliseconds.
      }
      catch(InterruptedException e){}; //We must catch any interruptions of sleep().
   }

   //Update(Graphics) is called by repaint() - the system adds a Graphics context.
   //We extend the update method to create a double buffer whenever necessary.

   public void update(Graphics g) {
      if (r.width != bounds().width || r.height != bounds().height) {
         bufferImage    = createImage(bounds().width, bounds().height);
         bufferGraphics = bufferImage.getGraphics(); //Whenever applet changes size
         r = bounds();                               //create a new double buffer, and
         damage = true;                              //tell application there's damage
      }
      render(bufferGraphics); //We ask application program to render to the buffer
      damage = false;
      g.drawImage(bufferImage,0,0,this); //And then paste in the rendered buffer.
   }
}