// Base class for animated applets
// uses the Virtual Graphics applet as a base class

public class AniApplet extends Vapplet implements Runnable
  {
  Thread thread;
  public int timerate=100; // milliseconds per frame
  
  // kick off animation thread
  public void start()
    {
    if (thread==null)
      {
      thread=new Thread(this);
      thread.start();
      }
    }
    
// Stop thread
  public void stop()
    {
    thread=null;
    }

// Here is the main animation thread
  public void run()
    {
    long time=System.currentTimeMillis();
    int i;
    while (thread!=null)
      {
      try
        {
        time+=timerate;  // wait for next frame
        Thread.sleep(Math.max(0,time-System.currentTimeMillis()));
        }
      catch (InterruptedException e) { }
      animate();
      }
    }
    
public void animate()  // derived classes will override
  {
  }
 }