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

public class ArmHack extends Applet implements Runnable {
  private Image offScreenImage;
  private Graphics offScreenGraphics;
  private Dimension offScreenSize;
  Image jake;
  Stick arm;
  boolean wave;
  int wavect;
  Transform2D t2d;

  public void init() {
    String i = getParameter("img");
    try {
      jake = getImage(new URL(getDocumentBase(), i));
    } catch (MalformedURLException e){ return; }
    arm = new Stick(65, 130, 145, 240, 6, this);
    t2d = new Transform2D();
    wavect=0;
    doLayout();
  }

  public final synchronized void update(Graphics theG){
      Dimension d = size();
      if((offScreenImage == null) || (d.width != offScreenSize.width) ||
         (d.height != offScreenSize.height)) 
        {
          offScreenImage = createImage(d.width, d.height);
          offScreenSize = d;
          offScreenGraphics = offScreenImage.getGraphics();
          offScreenGraphics.setFont(getFont());
        }
      offScreenGraphics.setColor(Color.white);
      offScreenGraphics.fillRect(0,0,d.width, d.height);
      paint(offScreenGraphics);
      theG.drawImage(offScreenImage, 0, 0, null);
  }

  public void start() {
    (new Thread(this)).start();
  }

  public void run() {
    while(true){
      if(wave){
	wavect++;
	if(wavect < 40){
	  arm.rotateAround1(-.03);
	}
	else if(wavect < 80){
	  arm.rotateAround1(.03);
	}
	else{
	  wavect = 0;
	  wave = false;
	}
      }
      repaint();
      try { Thread.sleep(30); } catch (Exception e) { }
    }
  }

  public void paint(Graphics g){
    g.drawImage(jake, 0, 0, this);
    arm.draw(g, t2d); 
  }

  public boolean mouseDown(Event e, int x, int y){
    wave = true;
    return true;
  }
}

