/* Overlap Animation Applet
   JDK1.1‘Î‰ž”Å
                  Coded By.KeN    Mar.19,1997
*/

import java.applet.AudioClip; 
import java.awt.*;
import java.lang.Integer;
import java.net.URL;
import java.awt.event.*;

public class Windmill extends java.applet.Applet implements Runnable, MouseListener {
  
  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( getSize().width, getSize().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" );
    addMouseListener( this );
  }

  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) { break; }
      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 void mouseClicked( MouseEvent e ) {
    if ( pause == true ) {
      anim.resume();
      bgm.loop();
    } else {
      anim.suspend();
      if ( bgm != null ) {
	bgm.stop();
      }
    }
    pause = ! pause;
  }

  public void mouseEntered( MouseEvent e ) {
  }

  public void mouseExited( MouseEvent e ) {
  }

  public void mousePressed( MouseEvent e ) {
  }

  public void mouseReleased( MouseEvent e ) {
  }

  public void update( Graphics scr ) {
    paint( scr );
  }

}

