import java.applet.*;
import java.awt.*;

public class PowerHour extends Applet implements Runnable
{

	Thread counterThread;
	Label countValue;
	int count=20;
	boolean startCounter=false;

	public PowerHour()
	{
	}

	public String getAppletInfo()
	{
		return "Name: PowerHour\r\n" +
		       "Author: Brian Jahns\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	public void init()
	{
    	add(new Label("Count: "));
		countValue=new Label(Integer.toString(count));
		add(countValue);
		add(new Button("Stop"));
		add(new Button("Start"));

	}

	public void destroy()
	{
	}

	public void paint(Graphics g)
	{
		
	}

	public void start()
	{
		if (startCounter == true)
		{
			if (counterThread == null)
			{
				counterThread = new Thread(this);
				counterThread.start();
			}
		}
	}
	
	public void stop()
	{
		if (counterThread != null)
		{
			counterThread.stop();
			counterThread = null;
		}
	}

	public void run()
	{
		while (true)
		{
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
			}
			countIt();
		}
	}

	public boolean action(Event evt, Object arg)
	{
		String buttonStr=(String)arg;
		if(buttonStr=="Stop")
		{
			if(counterThread != null)
			{
				end();
				return true;
			}
			return true;
		}
		if(buttonStr=="Start")
		{
			if(counterThread != null)
			{
				begin();
				return true;
			}
			return true;
		}
		return true;
	}

	public void begin()
	{
		startCounter=true;
		init();
		start();
	}

	public void end()
	{
		startCounter=false;
		stop();
	}

	public void countIt()
	{
		count--;
		countValue.setText(Integer.toString(count));
		if(count==0)
			count=21;
	}
}
