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

class StopAux extends Thread {
   private Image buffer;
   private StopThread app;
   private Graphics gBuff;
   public StopAux(Image b, StopThread a) { 
      buffer = b; app = a;
      gBuff = buffer.getGraphics();
      gBuff.setPaintMode();
      gBuff.setColor(Color.orange);
      gBuff.fillRect(0,0,500,300);
      app.repaint();
   }
   public void run() {
      try {
         grow(50,50,Color.red);
         grow(100,100,Color.blue);
         grow(150,150,Color.green);
      } catch (InterruptedException e) {
         System.out.println("... stop accepted :-)");
      }
      // gBuff.dispose();
   }

   public void grow(int x, int y, Color color) throws InterruptedException {
      gBuff.setColor(color);
      for(int i=0; i<200; i++) {
         synchronized (app) {
            if (app.stopped) 
               throw (new InterruptedException());
            else gBuff.fillRect(x,y,i,i/2);
	}
         try {Thread.sleep(20);}
         catch(InterruptedException e) {
            System.err.println("Growing interrupted!");
	 }
         app.repaint();
      }
   }
} // end of class StopAux()

public class StopThread extends Applet {
   public boolean stopped;
   private Image buffer;
   public void init() {
      buffer = createImage(500,300);
   }
   public void start() {
      System.out.println("New Animation ...");
      synchronized (this) { stopped = false;}
      (new StopAux(buffer, this)).start();
   }
   public void stop() {
      synchronized (this) { stopped = true;}
   }
   public void update(Graphics page) {
       paint(page);
   }
   public synchronized void paint(Graphics page) {
      page.setClip (0,0,500,300);
      page.drawImage(buffer,0,0,this);
   }
} // end of Applet StopThread
     


