import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
 * Class CSCI - NHCC Bssic Programming class
 * 
 * @author (Martin Schlosser)
 * @version (13)
 */
public class TraficLight extends JApplet implements ActionListener
{
    JButton greenButton, yellowButton, redButton; //sets up variables for buttons
    Image greenImg, yellowImg, redImg; //sets up variables for images
    ImageIcon icon; //label the icon
    JLabel imagelabel; //labels the images
    JPanel butpanel; //labels a panel
    public void init()
    {
        setLayout( new BorderLayout()); //uses the boderlayout for buttons
        setupButtons(); //seting up bottons
        greenImg=getImage(getCodeBase(),"greenlight.jpg");//uses stop light when greenImg is said
        yellowImg=getImage(getCodeBase(),"yellowlight.jpg");//same but for yellowImg
        redImg=getImage(getCodeBase(),"redlight.jpg");//same but for redImg
        icon = new ImageIcon();//creates an icon
        imagelabel=new JLabel( icon, JLabel.CENTER);//where the buttons and icon will be displayed
        add(imagelabel,BorderLayout.CENTER);//makes the image in the center
        setupImage(greenImg);// makes the default greenImg
    }
    public void setupButtons()
    {
        greenButton = new JButton("Go");//labels the first button
        yellowButton = new JButton("Wait");//the second button
        redButton = new JButton("Stop");//the third button
        greenButton.addActionListener(this);//gives the button an action
        yellowButton.addActionListener(this);//same
        redButton.addActionListener(this);//smae
        
        butpanel=new JPanel( new GridLayout(3,1));//puts the buttons in a row
        butpanel.add(greenButton);//adds the first button to the row
        butpanel.add(yellowButton);//the second
        butpanel.add(redButton);//the third
        add(butpanel, BorderLayout.WEST);// puts the row on the left
    }
    public void setupImage( Image img)
    {
        icon.setImage(img);//sets the image as an icon
        imagelabel.setIcon(icon);//helps change icon
        repaint();//changes the image
    }
    public void actionPerformed(ActionEvent ae)
    {
        Object source = ae.getSource();//looks for images
        if(source == greenButton)//if the first button is pushed
        setupImage(greenImg);//display the first image
        else if(source == yellowButton)//if the second is pushed
        setupImage(yellowImg);//display the second image
        else if(source == redButton)//if the third is pushed
        setupImage(redImg);//display the last image
    }
}
        