
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;

public class RISTest extends Applet implements Runnable
{
  Image image, rotImage, rotImages[];
  int w, h, pixels[];
  int frames, rot;

  Thread thread;
  boolean suspended = false;

  Image offImage;
  Graphics offGraphics;
  
  public void RISTest()
  {
  }
  
  public void init()
  {
    String imageSrc = getParameter("image");
    try {
      frames = Integer.parseInt(getParameter("frames"));
    }
    catch (Exception e) {
      System.err.println(e.toString());
    }
    
    rot = 0;
    try {
      image = getImage(new URL(getCodeBase() + imageSrc));
    }
    catch (MalformedURLException e) {
      System.err.println(e.toString());
    }
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
      mt.waitForID(1);
    }
    catch (InterruptedException ie) {
      System.err.println(ie.toString());
    }

    if (mt.isErrorID(1)) {
      Object errs[] = mt.getErrorsID(1);
      for (int i = 0; i < errs.length; i++) {
	System.err.println(errs[i].toString());
      }
    }

    rotImages = new Image[frames];
    rotImages[0] = image;
  }

  public void start()
  {
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
    else {
      suspended = false;
      thread.resume();
    }
  }

  public void stop()
  {
    if (!suspended) {
      suspended = true;
      thread.suspend();
    }
  }
  
  public boolean mouseDown(Event e, int x, int y)
  {
    if (suspended) {
      suspended = false;
      thread.resume();
    }
    else {
      suspended = true;
      thread.suspend();
    }
    return true;
  }
  
  public void update(Graphics g)
  {
    Dimension d = size();
 
    if ((offImage == null) ||
        (d.width != offImage.getWidth(null)) ||
        (d.height != offImage.getHeight(null))) {
      offImage = createImage(d.width, d.height);
      offGraphics = offImage.getGraphics();
    }
    paint(offGraphics);
    g.drawImage(offImage, 0, 0, null);
  }

  public void paint(Graphics g)
  {
    if (rotImage != null) {
      //long t = System.currentTimeMillis();
      g.clearRect(0, 0, size().width, size().height);
      g.drawImage(rotImage,
		  size().width/2 - rotImage.getWidth(this)/2,
		  size().height/2 - rotImage.getHeight(this)/2,
		  null);
      //System.err.println("paint() took " + (System.currentTimeMillis()-t) + "ms");
    }
  }

  public void run()
  {
    long t, s = 500;
    
    while (true) {
      t = System.currentTimeMillis();
      rot = ++rot % frames;

      if (rotImages[rot] == null) {
	RotateImageSource ris = null;
	try {
	  ris = new RotateImageSource(image, Math.PI/frames*2 * rot);
	}
	catch (Exception e) {
	  System.err.println(e.toString());
	}
	rotImages[rot] = createImage(ris);
      }
      rotImage = rotImages[rot];

      repaint();

      try {
	t = System.currentTimeMillis() - t;
	if (t > s) {
	  s = t;
	}
	Thread.sleep(67);  // ~15 fps
      }
      catch (InterruptedException e) {
      }
    }
  }
}

