import java.awt.Graphics;
import java.awt.Color;

public class Threader extends java.awt.Canvas implements Runnable {
  int myPosition =0;
  String myName;
  int numberofSteps=600;
  boolean keepRunning = true;

  //Constructor for a Threader. We need to know our name when we
  //create the Threader.
  public Threader (String inName){
    myName=new String (inName);
  }

  public synchronized void paint(Graphics g){
    //Draw a line for the 'racing line'.
    g.setColor (Color.black);
    g.drawLine (0,getSize().height/2,getSize().width,getSize().height/2);

    //Draw the round racer.
    g.setColor (Color.yellow);
    g.fillOval((myPosition*getSize().width/numberofSteps),0,15,getSize().height);
  }

  public void stop(){
    keepRunning = false;
  }

  public void run(){
    //Loop until we have finished the race.
    while ((myPosition <numberofSteps)&& keepRunning){
      //Move ahead one position.
      myPosition++;
      repaint();

      //Put ourselves to sleep so the paint thread can get around to painting.
      try{
        Thread.currentThread().sleep(10);
      }catch (Exception e){System.out.println("Exception on sleep");}
    }
    System.out.println("Threader:"+myName+" has finished the race");
  }


}//end class Threader. 
