// CMPU-101-51, Spring 2006
// Code used in class.
// April 20, 2006

// The StopLight class draws a stop light on a JFrame window
// It includes 3 JButtons, one to show the light, one to change the 
// color of the light and one to exit the window

// Some packages to import (they define the "J" classes used below)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  

public class StopLight extends JFrame implements ActionListener
// "extends" means this class inherits all the methods of the JFrame built-in class.
// "implements" means this class must have a definition for the method with signature 
//      public void actionPerformed(ActionEvent e)
// when an object that has an attached "ActionListener" generates an "ActionEvent", 
//      the actionPerformed method is called by the system to perform some action.
{
  // the last color displayed on the StopLight
  static Color last = Color.green;
  // the three JButtons that appear in the JFrame
  JButton changeJButton, exitJButton, showJButton;
  // the container holding the buttons, etc.
  Container cPane;
  
  // constructor for the stoplight class, taking 0 arguments 
  StopLight()
  {
    // THIS = the newly created StopLight object
    // the cPane is the part of the JFrame where things are displayed
    cPane = this.getContentPane();
    // setting the layout of the cPane to null allows us to use "absolute positioning"
    // (i.e., the exact specification of x and y coordinates)
    cPane.setLayout(null);
    
    // add JButtons to the cPane
    this.addComponents();
    this.setTitle("StopLight");
    this.setSize(300,300);
    // make the window frame voisible
    setVisible(true);
  }
 
  // Called by the constructor to  add three buttons to the cPane
  public void addComponents()
  {
    // Each button is instantiated and given coordinates where it should appear in the first 2 lines.
    // The 3rd line sets up a "listener" on the button and the 4th line adds the button to the cPane.
    showJButton = new JButton("ShowLight");
    showJButton.setBounds(175,10,100,50);
    showJButton.addActionListener(this);
    cPane.add(showJButton);
    
    exitJButton = new JButton("EXIT");
    exitJButton.setBounds(175,200,100,50);
    exitJButton.addActionListener(this);
    cPane.add(exitJButton);
    
    // the "setEnabled(false);" line makes this button initially unavailable (grayed out)
    changeJButton = new JButton("Change");
    changeJButton.setBounds(175,100,100,50);
    changeJButton.addActionListener(this);
    changeJButton.setEnabled(false);
    cPane.add(changeJButton);
  }
    
  // Because this JFrame has 3 components with attached listeners, this method checks the 
  // source of the ActionEvent object produced by the component.  If the source is 
  //       changeJButton - stoplight is re-drawn
  //       exitJButton       - System.exit(0) called
  //       showJButton    - enable change and disable show
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource().equals(changeJButton))
      drawLight();
    else if (e.getSource().equals(exitJButton))
      System.exit(0);
    else {  // e.getSource().equals(showJButton)
      // only execute this one time and then disable the button
      drawLight();
      showJButton.setEnabled(false);
      changeJButton.setEnabled(true);
    }
  }
  
  // method to do drawing.  alternative to using the paint method.  Here the
  // canvas to be drawn on is acquired using the getGraphics method of the cPane.
  public void drawLight()
  {
    final int LEFT_X = 110, CIRCLE = 30, TOP_Y = 80;    
    Graphics canvas = cPane.getGraphics();    
    if (last.equals(Color.green))
    {
      canvas.fillRect(100,70,50, 150);
      canvas.setColor(Color.green);
      canvas.drawOval(LEFT_X,TOP_Y+80,CIRCLE,CIRCLE);
      canvas.setColor(Color.red);
      canvas.drawOval(LEFT_X, TOP_Y, CIRCLE, CIRCLE);
      canvas.setColor(Color.yellow);
      // YELLOW light is FILLED (others are only drawn)
      canvas.fillOval(LEFT_X,TOP_Y+40,CIRCLE,CIRCLE);
    }
    else if (last.equals(Color.yellow))
    {
      canvas.fillRect(100,70, 50, 150);
      canvas.setColor(Color.yellow);
      canvas.drawOval(LEFT_X,TOP_Y+40,CIRCLE,CIRCLE);
      canvas.setColor(Color.green);
      canvas.drawOval(LEFT_X,TOP_Y+80,CIRCLE,CIRCLE);
      canvas.setColor(Color.red);
      // RED light is FILLED (others are only DRAWn)
      canvas.fillOval(LEFT_X, TOP_Y, CIRCLE, CIRCLE);
    }
    else
    {
      canvas.fillRect(100, 70, 50, 150);
      canvas.setColor(Color.red);
      canvas.drawOval(LEFT_X, TOP_Y, CIRCLE, CIRCLE);
      canvas.setColor(Color.yellow);
      canvas.drawOval(LEFT_X,TOP_Y+40,CIRCLE,CIRCLE);
      canvas.setColor(Color.green);
      // GREEN light is FILLED (others are only DRAWn)
      canvas.fillOval(LEFT_X,TOP_Y+80,CIRCLE,CIRCLE);
    }
    last = canvas.getColor();
  }
}
