
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Class TrafficLights - Program that allows the user to change the change the light on a traffic light to either green, yellow or red depending on which button they press
 * 
 * @author Thomas Haller 
 * CSCI 1130-51
 * @version 1.00
 * date 2/6/17
 */


public class TrafficLights extends JApplet implements ActionListener
{
    
    //variables
    JButton go, wait, stop;
    JLabel title, imageLabel;
    JPanel buttonPanel, titlePanel;
    ImageIcon icon;
    Image defaultLights, goLights, waitLights, stopLights;
    
    
    public void init()
    {
        setLayout (new BorderLayout() );
        
        setupButtons();
        
        setupTitle();
        
        getImages();
       
    }

  
    public void setupButtons()
    {
        
        //create buttons
        go = new JButton( "GO" );
        wait = new JButton( "WAIT" );
        stop = new JButton( "STOP" );
        
        //customize buttons
        go.setBackground(Color.GREEN);
        go.setFocusPainted( false );
        
        wait.setBackground(Color.YELLOW);
        wait.setFocusPainted( false );
        
        stop.setBackground( new Color( 255, 119, 114 ));
        stop.setFocusPainted( false );
        
        //add action listeners to buttons 
        go.addActionListener( this );
        wait.addActionListener( this );
        stop.addActionListener( this );
        
        //create JPanel
        buttonPanel = new JPanel( new FlowLayout() );
        
        //add buttons to JPanel
        buttonPanel.add( go );
        buttonPanel.add( wait );
        buttonPanel.add( stop );
        
        //add the JPanel to the border layout
        add( buttonPanel, BorderLayout.SOUTH );
      
    }
    
   
    public void getImages()
    {
        defaultLights=getImage( getCodeBase( ), "defaultLights.PNG"); // no light
        goLights=getImage( getCodeBase( ), "goLights.PNG"); //used for green light
        waitLights=getImage( getCodeBase( ) ,"waitLights.PNG"); // used for yellow light
        stopLights=getImage( getCodeBase( ), "stopLights.PNG"); // used for red light
        
        icon = new ImageIcon();
        imageLabel = new JLabel( icon, JLabel.CENTER );
        add(imageLabel, BorderLayout.WEST);
        
        //This loads the defaultLights image before any buttons are clicked
        setupImage(defaultLights);
    }
    
   
    public void setupImage( Image img )
    {
        icon.setImage( img );
        imageLabel.setIcon( icon );
        repaint();
    }
    
    
    public void actionPerformed( ActionEvent ae )
    {
        Object source = ae.getSource();
        
        //I used an if - else statement because I couldn't figure out how to associatate the JButtons with a switch statement
        if( source == go)
            setupImage(goLights);
         
        else if(source == wait)
            setupImage(waitLights);
            
        else if(source == stop)
            setupImage(stopLights);
           
    }
        
    
    public void setupTitle()
    {
        title = new JLabel( "Traffic Lights", JLabel.CENTER );
        title.setFont( new Font("Serif", Font.BOLD, 24 ));
        
        titlePanel = new JPanel();
        titlePanel.add(title);
        
        add( titlePanel, BorderLayout.NORTH );

    }
    
    
    public void paint(Graphics g)
    {
        super.paint(g);
       
        paintCar(g);
    
    }
    
    public void paintCar(Graphics g)
    {
        
        //road
        g.drawLine(200,390,500,390);
        
        //wheels of car
        g.fillOval(350, 370, 20, 20);
        g.fillOval(400, 370, 20, 20);
        
        //body of car
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(325, 350, 120, 25);
        g.fillRect(360, 335, 60, 20);
        

      
    }
    
  
     
  
    
    
    
  
        
    
    
    




    }




