import java.applet.Applet;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JTextField;

/*
 * Created on Nov 29, 2004
 *
 * Simple applet to illustrate threads.
 */

/**
 * @author Aditya Mathur
 *
 * 
 */
public class ThreadIlustrator  extends Applet implements Runnable,ActionListener{
	
	// Add a button and two text fields.
	
	JButton stopButton=new JButton("STOP"); // Stops and removes the thread.
	JButton incRate=new JButton("Increase blink rate");
	JButton decRate=new JButton("Decrease blink rate");
	
	JTextField tf=new JTextField("CS 178 is GREAT!");
	JTextField message=new JTextField(20);
	
	
	static int REFRESH_RATE=100; // Blink at the rate of 100ms/frame. Is static needed here?
	static int DEFAULT_REFRESH_RATE=100;  // In case user reduced the refresh rate to below 0.
	
	// Note that frames/second=1000/REFRESH_RATE.
	
	
	boolean blink=true; // Toggle used for changing the text in tf fromsomething to empty.
	boolean blinking=true;
	
	
	// Set applet dimensions upon startup.
	int width=500;
	int height=200;
	
	
	Thread animation;
	
	public void init(){
		
		resize(width,height); // Set applet screen size.
		
		// Add buttons and textfields.
		
		add(stopButton); add(incRate); add(decRate);
		add(tf);
		add(message);
		
		// Add listeners to buttons.
		
		stopButton.addActionListener(this);
		incRate.addActionListener(this);
		decRate.addActionListener(this);
		setBackground(Color.orange);
	}
	
	public void actionPerformed(ActionEvent e){
		Object buttonClicked=e.getSource(); // Get button clicked.
		
		if(buttonClicked==stopButton){ // If stop button then stop or start blinking.
			blinking=false;
			if(animation!=null){
				stopButton.setText("START");
				stop();
				REFRESH_RATE=0;
			}else{
				REFRESH_RATE=DEFAULT_REFRESH_RATE;
				blinking=true;
				stopButton.setText("STOP");
				start(); // Restart animation.
			}
		}
		else if(buttonClicked==incRate){ // else increase refresh rate.....
			REFRESH_RATE=REFRESH_RATE-20;
			if (REFRESH_RATE<=0){
				REFRESH_RATE=DEFAULT_REFRESH_RATE;
			}
		}
		else {
			REFRESH_RATE=REFRESH_RATE+20; // or decrease refresh rate.
		}
	displayRate(); // Display  the updated refresh rate.
	}
	
	public void start(){
		
		animation=new Thread(this); // Create a new thread.
		if(animation!=null){
			animation.start(); // Execute the run() method in a new thread.'
			message.setText("Thread spawned.");
		}else{
			message.setText("Thread could not be spawned.");
		}
	}
	
	public void run(){
		
		displayRate(); // Display  the latest refresh rate.
		while(blinking){
			if(!blink){
			tf.setText("");
			blink=!blink;
		}
		else{
			tf.setText("CS 178 is GREAT!");
			blink=!blink;
			
		}
		try{
			Thread.sleep(REFRESH_RATE);
		}catch(Exception e){}
	}
	}
	
	public void stop(){
		message.setText("Thread removed.");
		animation.stop();
		animation=null;
	}
	
	public void displayRate(){
		
		//		 Display refresh rate in frames/second.
		int framesPerSecond=0;
		
		if(animation==null){
			return; // No display update if the animation has stopped.
		}
		if(REFRESH_RATE>0){
			framesPerSecond=(int)1000/REFRESH_RATE;
		}
		if(framesPerSecond<=1){
			message.setText("Refresh rate is very low");
			
		}else{
			message.setText("Refresh rate: "+ Integer.toString(framesPerSecond)+" frames/second");
		}
		
		
	}

}
