//Sasha Devore
//12/10/97
//this applet creates a metronome just like the program Metronome.java
//the only difference being that this applet makes rectangles appear
//instead of a sound playing

//please do not modify this code in any way

import java.awt.*;
import java.applet.*;
import java.lang.*;


public class animation extends Applet implements Runnable 
{

        
        private Thread runner;
	private Button start, stop;
	private Scrollbar TickTime;
        private TextField tf1;
        private int tempo;
	private boolean play = false;
        int pic;
    
    
	public void init() {
		this.setBackground(Color.white);
	
		start = new Button("Start");
		stop = new Button("Stop");
		TickTime = new Scrollbar(Scrollbar.HORIZONTAL, 0,1, 30, 180);
		tf1 = new TextField("blah",3);

	       
		this.add(TickTime);
		this.add(tf1);
		this.add(start);
		this.add(stop);

		
	}

	public boolean action(Event e, Object arg) {
	    	if(e.target == start) {
			play = true;
			
		} else if(e.target == stop) {
			play = false;
			
		}
                return true;		    
	}	



     public void start()
        {      
		if (runner == null)
                {       runner = new Thread( this); 
                        runner.start() ;            
                }
        }

	public void stop()
        {
	   if(runner != null)
           {
	       runner.stop();
               runner = null;
           }
        }
       

        public void run ()
        {   
            while(true) {
		tempo = this.TickTime.getValue();
		String s=String.valueOf(tempo);
		tf1.setText(s);
	        if (play) {
	             repaint();
		     try { Thread.sleep(60*1000/tempo); } catch (InterruptedException e) {}
	        }
           }
	}
  
public void paint(Graphics g )
{
    if(play) {
    if(pic == 0)
      {
        g.setColor(Color.blue);
        g.fillRect(25,75,50,50);
        pic = 1;
    } else if(pic ==1) {
        g.setColor(Color.red);
        g.fillRect(225,75,50,50);
        pic = 0;
    }
    
    }	
}

}


