/************************************************
 * File
 * ~caviness/Class/CISC367/exer-solutions/TrafficLightThread/soln2/TrafficLightThread.java
 ************************************************/

import java.lang.*;		// for class Thread
import java.awt.Color;

public class TrafficLightThread extends Thread
{
   // Instance variables
   TrafficLight TL;
   PedestrianLight PL;
   FlasherCoordinator f;

   // Constructor
   TrafficLightThread( FlasherCoordinator f, TrafficLight TL, PedestrianLight PL )
   { this.f = f; this.TL = TL; this.PL = PL; }

   // Instance methods
   public void run()
   {
      System.out.println("Entering TrafficLightThread:run()");
      PL.setMsg( "Don't Walk" );
      while ( true )
      {
         TL.setColor( Color.red );	// Change traffic light to red
         try{ sleep(15000); }
         catch ( InterruptedException e ) {}

         TL.setColor( Color.green );	// Change traffic light to green
         PL.setMsg( "Walk" );		// Change ped light to "Walk"
         try{ sleep(10000); }
         catch ( InterruptedException e ) {}
					// Ten secs before changing light
         PL.setMsg( "Don't Walk" );	// change ped light to "Don't Walk"
         f.turnOn();			// & start ped light flashing.
         try{ sleep(10000); }
         catch ( InterruptedException e ) {}

         f.turnOff();			// Chg ped light from flash to solid
         TL.setColor( Color.yellow );	// Change traffic light to yellow
         try{ sleep(5000); }
         catch ( InterruptedException e ) {}

      }
   }
}
