import java.awt.*;
import java.lang.String;
import java.lang.System;
import java.io.PrintStream;
import java.applet.Applet;
import java.util.*;

public class sm extends Applet implements Runnable {
    long PAUSE_TIME = 1000;
    final int numimgs = 20;
    Image img[] = new Image[numimgs];
    Image cover_image;
    int index=0;
    boolean stopped = false;
    boolean loading=false;
    boolean loaded=false;
    MediaTracker tracker;
    Thread animator;
    String image_file;
    String loading_message = new String("Please be patient while the images are loaded....");


    public void init() {

        cover_image = this.getImage(this.getDocumentBase(), "cart.gif");

        Panel bottomPanel = new Panel();

        setLayout(new BorderLayout());
        bottomPanel.add(new Button("Start"));
        bottomPanel.add(new Button("Stop"));

        add("South", bottomPanel);

        resize(358,463);

    }

    public void start() {
        animator = new Thread(this);
    }

    public void run() {
	int j;

// When this thread is first executed, tell the user to be patient while the
// images for the animation are being loaded

	loading = true;
	repaint();

        tracker = new MediaTracker(this);

        for(j=0; j<numimgs; ++j) {
         image_file = "img_"+j+".gif";
         img[j] = this.getImage(this.getDocumentBase(), image_file);
         tracker.addImage(img[j], j);
        }

	for(j=0; j<numimgs; ++j) {

	try {

	  showStatus("Loading image "+(j+1)+" out of "+numimgs);
	  tracker.waitForID(j);
	  loaded = true;
	  loading = false;
	  repaint();

	}
	catch (InterruptedException e) {
                    System.out.println("Problem waiting");;
        }

	}

// This is the loop that animates the images with a pause of PAUSE_TIME msec

	 Thread me = Thread.currentThread();
	 while(animator == me) {

	  showStatus("Animation Running");


                try {
                    Thread.sleep(PAUSE_TIME);
                } catch (InterruptedException e) {
                    return;
                }


	  	repaint();

	  if (index == 480) index = 0;

	 }


    }


// This function catches the button push events and either stops or starst the
// animation thread accordingly


     public boolean action(Event evt, Object obj ) {

	if(evt.target instanceof Button) {
	 String label = obj.toString();

	 if(label.equals("Start")) {
	  if(stopped) {
	   animator.resume();
	   showStatus("Animation Running");
	  }
	  else  {
	   animator.start();
	   animator.setPriority(Thread.MAX_PRIORITY);
	  }
	 }
	 else if(label.equals("Stop")) {
	  if(animator.isAlive()) {
	   animator.suspend();
	   showStatus("Animation Suspended");
	   stopped = true;
	  }
	 }

	}


   	return true;
    }


    public void stop() {
	animator.stop();
	animator = null;
    }

    public void destroy() {
    }


    public void paint(Graphics g) {


	 if(loading) {
	  g.clearRect(0,0,600,600);
	  g.drawString(loading_message,15,200);
	 }

// Make this thread wait until all of the images are loaded before animating

	 else if(loaded) {

	  g.drawImage(img[index%numimgs], 25, 50, this);

	  synchronized (this) {
	  ++index;
	  if(index > 40)
	   index = 0;
	  }

	 }

         else {
          g.drawImage(cover_image, 10, 10, this);
         }


    }

}

