import java.awt.*;

//The statement implements Runnable promises Java that this class will
//include certain functions that are listed in an interface called Runnable.
//The function promised is run(), and as promised, it is included in the
//class
public class GenericApplet extends java.applet.Applet implements Runnable
{
   public boolean damage = true;        // you can force a render
   //The empty braces mean "do nothing". Remember
   //this function is overridden in class StickFigureApplet
   public void render(Graphics g) { }   // you can define how to render

   private Image image = null;
   private Graphics buffer = null;
   private Thread t;
   private Rectangle r = new Rectangle(0, 0, 0, 0);

   //These three functions perform thread control. Threads are simply an execution process
   //in a program. When a program is run, you could trace where the process is in the source
   //code by following it with your finger. Starting another thread starts another point of execution
   //in the same code, basically giving you another finger. For more information on threads
   //click here. Notice: any function with t. in front of it calls that function in Thread, not in
   //StickFigureApplet

   //This start() creates a new thread for GenericApplet and begins running it by calling its start() function
   public void start() { if (t == null) { t = new Thread(this); t.start(); } }
   //The stop() function is magically called whenever the applet is stopped, for instance
   //when a web page is closed. The t.stop() statement, however, is deprecated and should
   //not be used anymore. Click here to read why
   public void stop()  { if (t != null) { t.stop(); t = null; } }
   //run() is automatically called by start() and is where execution begins (like a main() function)
   public void run() {
      try { while (true) { repaint(); t.sleep(30); } }
      catch(InterruptedException e){};
   }

   //update() is another "magic" function that is called by repaint(). The Graphics g object that is
   //passed in is the actual graphics that are on the screen. REMEMBER since StickFigureApplet
   //is the applet that is running and it is extended from this class, these functions are actually being
   //run in StickFigureApplet, not GenericApplet
   public void update(Graphics g) {
      //r is originally set to be a rectangle with no size and bounds() checks the size of the
      //current applet, so this if statement will always be run at least once
      if (r.width != bounds().width || r.height != bounds().height) {
         image = createImage(bounds().width, bounds().height);
         buffer = image.getGraphics();
         r = bounds();
         damage = true;
      }
      //Remeber, this is the render() is called by StickFigureApplet
      render(buffer);
      damage = false;
      if (image != null)
      g.drawImage(image,0,0,this);
   }
}