/* 
 * csc110/pset2/TrafficLight.java
 * 
 * Copyright (C) 2003 phloem@fumbling.com
 *
 * This is CSC110 LF09 assignment #2. It draws a traffic light and then
 * simulates the timing of the intersection at Douglas and Hillside in
 * Victoria. (Sort of -- I didn't measure it with a stopwatch, if you
 * know what I mean.) 
 *
 * Submitted by Mike Sugimoto, 96-05886
 *
 * NB: A working (i.e., animated) version of this applet lives at 
 * <http://fumbling.com/academic/csc/light.html> 
 *
 */ 

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class TrafficLight extends Applet
{
    public void init()
    {
        setBackground(Color.white);
    }

    // initialize the pause mechanism.

    public void pause (int pause)
    {
	try
	    {
		Thread.sleep(pause);
	    }
	catch(InterruptedException e) {} // why do we need these? 
    }

    public void paint (Graphics g)
    {

	// Defines the starting positions of the ovals for the light.

	short xPos = 50;
	short yPos = 25;
	short xSize = 92, ySize = 92;

	// Draw the framework for the light

        g.setColor(Color.black);
        g.drawString("CSC 110 Assignment 2: The Traffic Light", 200, 20);
	g.drawString("This is a simulation of the light at the", 200, 30);
	g.drawString("intersection of Douglas and Hillside.", 200, 40);
        g.setColor(Color.yellow);
        g.fill3DRect(10, 10, 170, 430, false);
        g.setColor(Color.black);
        g.fill3DRect(15, 15, 160, 420, true);
        g.setColor(Color.white);
        g.drawOval(xPos, yPos, xSize, ySize);
        g.drawOval(xPos, yPos + 100, xSize, ySize);
        g.drawOval(xPos, yPos + 200, xSize, ySize);
        g.drawOval(xPos, yPos + 300, xSize, ySize);
        g.setColor(Color.gray);

	// Define polygon drawing array

	int[] xPoints[] = {{125, 125, 80, 80, 65, 80, 80, 125}};
	int[] yPoints[] = {{367, 373, 373, 378, 370, 362, 367, 367}};
        final byte nPoints = 8; // This won't ever change. ever.

        g.fillPolygon(xPoints[0], yPoints[0], nPoints);

	pause(500); // Wait a second so people can marvel at the arrow

	// blink routine, needlessly complicated.

        for (int i = 0; i <= 5; i++)
        {
            g.setColor(Color.black);
            g.fillPolygon(xPoints[0], yPoints[0], nPoints);

	    pause(200);

            g.setColor(Color.green);
            g.fillPolygon(xPoints[0], yPoints[0], nPoints);
	    
	    pause(425);
        }

	// Now we cycle the colors

	g.setColor(Color.gray); 
        g.fillPolygon(xPoints[0], yPoints[0], nPoints);	

        g.setColor(Color.green);
        g.fillOval(xPos, yPos + 200, xSize, ySize);

	pause(4500);

	// This is ugly, but I'm anal about the "cleanliness" of the circles
	// and there doesn't seem to be a better way to do it. 

	g.setColor(Color.black);
	g.fillOval(xPos, yPos + 200, xSize, ySize); 
	g.setColor(Color.white);
	g.drawOval(xPos, yPos + 200, xSize, ySize); 

	// Yellow warning

	g.setColor(Color.yellow);
	g.fillOval(xPos, yPos + 100, xSize, ySize); 

	pause(1500);

	g.setColor(Color.black);
	g.fillOval(xPos, yPos + 100, xSize, ySize);
	g.setColor(Color.white);
	g.drawOval(xPos, yPos + 100, xSize, ySize);

	// Go red.

	g.setColor(Color.red);
	g.fillOval(xPos, yPos, xSize, ySize);

	// Don't like it? Call someone who cares!

	g.setColor(Color.black);
        g.drawString("Call the traffic engineers at 361-0412", 200, 100);
	g.drawString("to whine about signal timing.", 200, 110);
	g.drawString("\"I complain, but nobody listens!\"", 200, 130);

    } 
} 

