
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Class TrafficLights - At the click of a button, change the traffic lights 
 * 
 * @author Chase Conner
 * @version JDK 8
 * @course CSCI 1130-01
 * @date 10-2-15
 */
public class TrafficLights extends JApplet implements ActionListener {
    private JButton GO, STOP, WAIT 
;
    private int carX = 200; 
    
    private boolean clickGO = false; 
    private boolean clickSTOP = false;
    private boolean clickWAIT = false;
    
    
    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * has been loaded into the system. It is always called before the first 
     * time that the start method is called.
     */
    public void init()
    {
        setLayout( new FlowLayout()); // changes the layout from BorderLayout to FlowLayout
        
        WAIT = new JButton ("WAIT"); //adds WAIT label to button
        WAIT.setForeground(Color.yellow); //changes the label to yellow 
        
        //adds the WAIT JButton to the screen
        add (WAIT); 
        WAIT.addActionListener(this);
        
        
        GO = new JButton ("GO"); //adds GO label to button 
        GO.setForeground(Color.green); //changes the label to green
        
        //adds the button to the screen
        add (GO);
        GO.addActionListener(this);
        
        
        STOP = new JButton ("STOP"); //adds STOP label to button 
        STOP.setForeground(Color.red); //changes the label to red 
        
        //adds STOP JButton to screen 
        add (STOP);
        STOP.addActionListener(this); 

        
       
    }
    
 

    /**
     * Called by the browser or applet viewer to inform this JApplet that it 
     * should start its execution. It is called after the init method and 
     * each time the JApplet is revisited in a Web page. 
     */
    public void start()
    {
        // provide any code requred to run each time 
        // web page is visited
    }

    /** 
     * Called by the browser or applet viewer to inform this JApplet that
     * it should stop its execution. It is called when the Web page that
     * contains this JApplet has been replaced by another page, and also
     * just before the JApplet is to be destroyed. 
     */
    public void stop()
    {
        // provide any code that needs to be run when page
        // is replaced by another page or before JApplet is destroyed 
    }
       
  
    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        super.paint(g);
        
        //declares and retrieves the images from their file locations  
        Image img = getImage(getDocumentBase(), "stoplights.png");
        Image img2 = getImage(getDocumentBase(), "car.jpg");
       
        g.drawImage( img, 50, 100, 300, 350, 0, 0, 5000, 5000, this ); //draws and resizes the stoplights image 
        g.drawImage( img2, carX, 400, 1000, 1000, 0, 0, 5000, 5000, this); //draws and resizes the car image 
        
        /*
         * Statement to activate the squence of instructions when GO is clicked.
         * Turns on the green light and
         * switches off the red or the yellow light. 
         */ 
        if (clickGO == true)
        {
        // instructions to draw and fill in the circle to display the green light when GO is pressed
        g.setColor(Color.black);
        g.drawOval(63, 184, 30, 30);
        g.setColor(Color.green);
        g.fillOval(63, 184, 30, 30);
        
        //switches off the red and yellow light when GO is pressed
        clickSTOP = false; 
        clickWAIT= false;
        }
        
        /* 
         * Statement to activate the squence of instructions when STOP is clicked.
         * Turns on the red light and
         * switches off the green or the yellow light.
         */       
        if (clickSTOP == true)
        {
        //instructions to draw and fill in the circle for the red light when STOP is clicked
        g.drawOval(63, 112, 30, 30); 
        g.setColor(Color.red);
        g.fillOval(63, 112, 30, 30);
        
        //switches off the green and yellow light when STOP is pressed
        clickGO = false;
        clickWAIT = false;
        }
      
        /*
         * Statement to activate the sequence of instructions when WAIT is clicked.
         * turns on the yellow light for wait and
         * turns off the red or the green light.
         */
        if (clickWAIT == true)
        {
        //instructions to draw and fill in the circle for the yellow light when WAIT is clicked 
        g.setColor(Color.black);
        g.drawOval(63, 148, 30, 30);
        g.setColor(Color.orange);
        g.fillOval(63, 148, 30, 30); 
        
        //switches off the green and red light when WAIT is pressed
        clickGO = false;
        clickSTOP = false; 
        }
        
        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    }
    
      public void actionPerformed(ActionEvent event)
    {
       
       if (event.getSource() == GO)
         {
             clickGO = true; //turns on the green light 
             clickWAIT = false; //switches off the yellow light if it was on 
             clickSTOP = false; //switches off the red light if it was on 
             
             carX -= 15; //moves the car to the left 
             
             repaint(); //redraws and refill the oval for the green light 
            }
       if (event.getSource() == STOP)
         {
             clickSTOP = true; //turns on the red light
             clickGO = false; //switches off the green light if it was on 
             clickWAIT = false; //switches off the yellow light if it was on 
             
             repaint(); //redraw and refill the the oval for the red light 
            }
       if (event.getSource() == WAIT)
         {
             clickWAIT = true; //turns on the yellow light
             clickSTOP = false; // switches off the red light if it was on
             clickGO = false; // switches off the green light if it was on 
             
             repaint(); //redraw and refill the oval for the yellow light
            }
    } 
   
    /**
     * Called by the browser or applet viewer to inform this JApplet that it
     * is being reclaimed and that it should destroy any resources that it
     * has allocated. The stop method will always be called before destroy. 
     */

    {
        // provide code to be run when JApplet is about to be destroyed.
    }


    /**
     * Returns information about this applet. 
     * An applet should override this method to return a String containing 
     * information about the author, version, and copyright of the JApplet.
     *
     * @return a String representation of information about this JApplet
     */
    public String getAppletInfo()
    {
        // provide information about the applet
        return "Title: Traffic Lights   \nAuthor: Chase Conner  \n At the click of a button, change the traffic lights";
    }


    /**
     * Returns parameter information about this JApplet. 
     * Returns information about the parameters than are understood by this JApplet.
     * An applet should override this method to return an array of Strings 
     * describing these parameters. 
     * Each element of the array should be a set of three Strings containing 
     * the name, the type, and a description.
     *
     * @return a String[] representation of parameter information about this JApplet
     */
    public String[][] getParameterInfo()
    {
        // provide parameter information about the applet
        String paramInfo[][] = {
                 {"firstParameter",    "1-10",    "description of first parameter"},
                 {"status", "boolean", "description of second parameter"},
                 {"images",   "url",     "description of third parameter"}
        };
        return paramInfo;
    }
}
