/**
* Requires TimerRun.java, run it there
*/
import java.awt.*;
import javax.swing.*;
import java.text.*;

public class Timer implements Runnable
{
    private int x; //present co-ordinate
    private int y; //present co-ordinate
    private int timeLeft;
    private boolean stopped;
    private Component surface;
    Font f = new Font ("Arial", Font.PLAIN, 28);
    
    public Timer (int xx, int yy, Component xr)
    {   x = xx;
	y = yy;
	timeLeft = 25;
	surface = xr;

    }


    public void run ()
    { //run method, starts when "Bounce" button is pressed
	moveTimer ();
    }


    public void stopTimer ()
    { //to pause the thread in the moveTimer method, stopped must be true
	//this stops the loop in moveTimer
	stopped = true;
    }


    public void moveTimer ()
    { //calculates the new position of the Timer and moves it
	//repeats until the user clicks the stop button.
	stopped = false;

	while (!stopped && timeLeft > 0)
	{

	    drawTimer ();
	    timeLeft--;
	    //sleep for a bit after choosing new position
	    try
	    {
		Thread.sleep (100);
	    }
	    catch (InterruptedException x)
	    {
		;
	    }

	}
    }


    public void drawTimer ()
    { //Draw a white square on screen to erase old Timer
	Graphics g = surface.getGraphics ();
	g.setFont(f);
	g.setColor (Color.blue);
	g.fillRect (x - 5, y - 25, 45, 40);
	g.setColor (Color.red);
	//Draw the new Timer
	g.drawString ("" + timeLeft, x, y);

    }



}

