/*
 * TrafficLight.java    2.0 97/07/07
 * Copyright (c) 1997, B. F. Caviness
 *
 * This software may be copied/modified for any non-commercial educational use.
 */

import java.awt.*;

/**
 * The <code>class TrafficLight</code> defines a canvas on which the
 * <code>paint( Graphics )</code> method draws a traffic light.
 * This code uses JDK 1.1.
 */

/************************************************/

public class TrafficLight extends Canvas
{

  Color onLight;	// Instance variable to indicate which color light
			// is on


  TrafficLight( )	// Zero-parameter constructor
  {
      onLight = Color.green;
  }



  TrafficLight( Color c ) // One-parameter constructor
  {
      setOnLight( c );	  // Verify color setting
  }

  public Color getOnLight()
  {
      return onLight;
  }

  public void setOnLight( Color c )
  {
      // Setters and constructors should insure that class variables
      // are set to valid values.
      if ( c == Color.red || c == Color.yellow || c == Color.green )
          {
              onLight = c;
          }
      else
          {
              System.out.println("TrafficLight.setOnLight:  cannot set " +
                                 "traffic light to color " + c +
                                 "\nSetting color to default green.");
              onLight = Color.green;
          }
  }

  /************************************************/
  public void paint(Graphics g)
  {
   System.out.println("Painting traffic light again"); // To see when
						       // paint() is called
   Dimension d = getSize();		// Get size of canvas
   int TLWidth = d.width/4;		// Set width of traffic light
   int TLHeight = 3*d.height/4;		// Set height of traffic light
   int xOrigin = (d.width - TLWidth)/2; // Center traffic light on canvas
   int yOrigin = d.height/8;

   //Draw outline of traffic light
   g.drawRect(xOrigin,yOrigin,TLWidth,TLHeight);

   // Now draw the light that is on
   Color colorSave = g.getColor();	// Save current color

   if ( onLight == Color.red ){
      g.setColor(Color.red);
      g.fillOval(xOrigin+TLWidth/4,yOrigin+TLHeight/8,TLHeight/6,
                 TLHeight/6);
   } else if ( onLight == Color.yellow ){
      g.setColor(Color.yellow);
      g.fillOval(xOrigin+TLWidth/4,yOrigin+5*TLHeight/12,TLHeight/6,
                 TLHeight/6);
   } else {			// Green light
      g.setColor(Color.green);
      g.fillOval(xOrigin+TLWidth/4,yOrigin+17*TLHeight/24,TLHeight/6,
                 TLHeight/6);
   }

   // Now draw black outline around each light
   // Red light
   g.setColor(Color.black);
   g.drawOval(xOrigin+TLWidth/4,yOrigin+TLHeight/8,TLHeight/6,
                TLHeight/6);
   // Yellow light
   g.drawOval(xOrigin+TLWidth/4,yOrigin+5*TLHeight/12,TLHeight/6,
                TLHeight/6);
   // Green light
   g.drawOval(xOrigin+TLWidth/4,yOrigin+17*TLHeight/24,TLHeight/6,
                TLHeight/6);

   g.setColor(colorSave);		// Restore original color
  }


 /************************************************/
 // Inform layout managers of minimum and preferred sizes of canvas
 // IMPORTANT: be sure to spell the names of these functions correctly
 // or else they will not be over ridden and will have no effect.

 public Dimension getMinimumSize()
 {System.out.println("getMinimumSize() called");
  return new Dimension(100,100);
 } 
 /************************************************/
 public Dimension getPreferredSize()
 {System.out.println("getPreferredSize() called");
  return new Dimension(300,300);
 }
}
