
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Rosemary Ross 
 * @course CSCI 1130-51
 * @date September 14, 2015
 * @description TrafficLight is a java applet that changes the color of a stoplight depending on what button you push.
 */


/*I've had a lot of problems with this, and I actually ended up scrapping my original idea for this much more basic one, because I was afraid of time constraints. I couldn't get 
 * ActionListener to work for anything.... and after scrapping my whole program I discovered that it was because I'd typed "performed" as "preformed". 
 */


public class TrafficLight extends JApplet implements ActionListener 
{
    
    
    //declaring my variables
    JButton goButton, waitButton, stopButton;
    JPanel buttonArea, graphicsArea, signArea;
    JLabel signLabel, carLabel;
    Image goSign, waitSign, stopSign, littleCar;
    ImageIcon colorIcon, carIcon;

    public void init()
    {      
        //I chose BorderLayout because it seemed easiest for program with multiple parts.
        setLayout(new BorderLayout() );
        getContentPane().setBackground(Color.white);
        
        setupButtons();
        
        goSign=getImage (getCodeBase( ), "greenlight.jpg");
        waitSign=getImage (getCodeBase( ), "yellowlight.jpg");
        stopSign=getImage (getCodeBase( ), "redlight.jpg");
        littleCar=getImage (getCodeBase( ), "car.jpg");
               
        colorIcon = new ImageIcon( );
        signLabel = new JLabel( colorIcon, JLabel.CENTER);
        add( signLabel, BorderLayout.CENTER);
        
        carIcon = new ImageIcon( );
        carLabel = new JLabel( carIcon, JLabel.CENTER);
        add( carLabel, BorderLayout.SOUTH );
        
        setupGraphics( goSign );    
        
        setupCar(littleCar);
    }
        
    public void setupButtons( )
    {
            
        goButton = new JButton ("Go!");
        waitButton = new JButton ("Wait");
        stopButton = new JButton("Stop");
        goButton.addActionListener(this);
        waitButton.addActionListener(this);
        stopButton.addActionListener(this);
        
        buttonArea = new JPanel( new GridLayout( 3, 1 ));
        buttonArea.add( goButton );
        buttonArea.add( waitButton );
        buttonArea.add( stopButton );
        add( buttonArea, BorderLayout.WEST);

    }
        
    
    public void setupGraphics( Image img )
    {
        colorIcon.setImage( img );
        signLabel.setIcon( colorIcon );
        repaint();
        
    }
    
    public void setupCar( Image img )
    {
        carIcon.setImage( img );
        carLabel.setIcon( carIcon );
    }
     

    public void actionPerformed(ActionEvent event) 
    {
        Object source = event.getSource();
        if ( source == goButton )
            setupGraphics( goSign );
        else if ( source == waitButton )
            setupGraphics( waitSign );
        else if ( source == stopButton )
            setupGraphics( stopSign );
        
        }
    }