import java.applet.Applet;
import java.awt.*;

public class Sampleanim extends Applet implements Runnable {
  int i,x1,y1,xx0 = 60;
  Font fn;
  Color cl[] = {Color.blue, Color.cyan, Color.gray, Color.green, 
		  Color.lightGray, Color.magenta, Color.orange, Color.pink, 
		  Color.red, Color.white, Color.yellow };
  Thread th;
  Button b;
  Dimension dim;
  boolean loop = true;
  boolean status = true;

  public void init() {
    dim = this.size();
    setLayout(null);
    b = new Button("stop");
    b.setForeground(Color.white);
    b.reshape(10,dim.height-19,40,18);
    add(b);
    i = x1 = y1 = 0;
    setBackground(Color.black);
//    resize(300,100);
  }
  public void start() {
    if (th == null) {
      th = new Thread(this);
      th.start();
    }
  }
  public void stop() {
    if (th != null) {
      th.stop();
      th = null;
    }
  }
  public void paint(Graphics g) {
    if (i == cl.length) i = 0;
    g.setColor(cl[i]);
    g.fillOval(x1, y1, 10, 10);
    if (loop) {
      x1 += 10;
      if (x1 >= dim.width) {
	x1 = 0;
	y1 +=10;
	i++;
	if (y1 >= dim.height-20) {
	  loop = !loop;
	  y1 = 0;
	}
      }
    } else {
      y1 += 10;
      if (y1 >= dim.height-20) {
	y1 = 0;
	x1 += 10;
	i++;
	if (x1 >= dim.width) {
	  loop = !loop;
	  x1 = 0;
	}
      }
    }
  }
  public void mousepaint(Graphics g, int x, int y) {
    int xx = x-x%10, yy = y-y%10;
    if (xx0 < dim.width-10) {
      g.copyArea(xx, yy, 10, 10, xx0-xx, dim.height-yy-15);
      xx0 += 10;
    } else {
      g.copyArea(70, dim.height-15, dim.width-10, 10, -10, 0);
      g.copyArea(xx, yy, 10, 10, xx0-xx, dim.height-yy-15);
    }
    g.fillOval(xx, yy, 10, 10);
  }
  public void update(Graphics g) {
    paint(g);
  }
  public void run() {
    while(true) {
      try {
	th.sleep(10);
      } catch(InterruptedException e) {
	break; 
      }
      repaint();
    }      
  }
  public boolean action(Event e, Object o) {
    if (e.target instanceof Button){
      if (status) {
	th.suspend();
	b.setLabel("start");
      }
      else {
	th.resume();
	b.setLabel("stop");
      }
      status = !status;
      return true;
    }
    return false;
  }
  public boolean mouseDown(Event e, int x, int y){
    if (y < dim.height-20) 
      mousepaint(this.getGraphics(),x,y);
    return true;
  }
}
