/* Overlap Animation Applet
                  Coded By.KeN    May.6,1996
*/

import java.applet.AudioClip; 
import java.awt.*;
import java.lang.Integer;
import java.net.URL;

public class Windmill extends java.applet.Applet implements Runnable {
  
  Thread anim;
  
  int interval;
  int startframe;
  int endframe;
  int frame;
  String filename;
  String bgfilename;
  boolean pause = false;
  Image src[];
  Image bgimage;
  AudioClip bgm = null;
  int count = 0;
  int x,y;
  Graphics goff;
  Image ioff;

  public void init() {
    String s = null;
    int i,j=0;
    s = getParameter("start") ;
    startframe =  ( s != null ) ? Integer.valueOf(s).intValue() : 1;
    s = getParameter("end");
    endframe =  ( s != null ) ? Integer.valueOf(s).intValue() : 1;
    frame = endframe-startframe+1 ;
    src = new Image[frame];
    s = getParameter("interval");
    interval =  ( s != null ) ? Integer.valueOf(s).intValue() : 1000;
    filename = getParameter("src");
    s = getParameter("x");
    x =  ( s != null ) ? Integer.valueOf(s).intValue() : 0;
    s = getParameter("y");
    y =  ( s != null ) ? Integer.valueOf(s).intValue() : 0;
    bgfilename = getParameter("bgimage");
    s = getParameter("audio");
    if ( s != null ) {
      bgm = getAudioClip(getCodeBase(), s + ".au" );
    }
    ioff = createImage( size().width, size().height );
    goff = ioff.getGraphics();
    for ( i = startframe ; i <= endframe ; i++ ) {
      src[j] = getImage(getDocumentBase(), filename + i + ".gif");
      j++;
    }
    if ( bgfilename != null ) bgimage = getImage(getDocumentBase(), bgfilename + ".gif");
    
  }

  public void start() {
    if ( anim == null ) {
      anim = new Thread( this );
      anim.start();
    }
  }

  public void stop() {
    if ( bgm != null ) {
      bgm.stop();
    }
    if ( anim != null ) {
      anim.stop();
      anim = null;
    }
  }

  public void run() {
    if ( bgm != null ) {
      bgm.loop();
    }

    while ( true ) {
      try { Thread.currentThread().sleep(interval);} catch (InterruptedException e) {};
      repaint();
    }
  }

  public void paint(Graphics scr) {
    if ( bgfilename != null ) {
      goff.drawImage(bgimage, 0, 0, this);
    }
    if (src[count] != null) {
      goff.drawImage(src[count], x, y, this);
    }
    scr.drawImage( ioff, 0, 0, this );
    count++;
    if ( count == frame ) count=0;
  }

  public boolean mouseDown(Event e,int x,int y) {
    if ( pause == true ) {
      anim.resume();
      bgm.loop();
    } else {
      anim.suspend();
      if ( bgm != null ) {
	bgm.stop();
      }
    }
    pause = ! pause;
    return( true );
  }

  public void update( Graphics scr ) {
    paint( scr );
  }

}

