
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.Graphics;
import java.awt.Color;

/**
 * Class Trafficlight - This applet consists of three buttons that control a traffic light.
 * 
 * @author Ben Worwa
 * @version 2/28/2017 csci 1130
 */
public class Trafficlight extends JApplet implements ActionListener
{
    JButton redButton;  //all of the statements needed for this applet to run.
    JButton yellowButton;
    JButton greenButton;
    JPanel redPanel;// I don't think I need this many  panels however, I just wanted to make sure.
    JPanel yellowPanel;
    JPanel greenPanel;
    Boolean one;
    Boolean two;
    Boolean three;
    AudioClip idlecar;
    AudioClip revcar;
    public void init()
    {
        getContentPane().setBackground( Color.GRAY ); //my Jpanels.
        redPanel = new JPanel(new FlowLayout());
        yellowPanel = new JPanel(new FlowLayout());
        greenPanel = new JPanel(new FlowLayout());

        

        setLayout( new FlowLayout()); //My red button layout.
        redButton= new JButton("Stop");
        redButton.addActionListener(this);
        this.add(redButton,FlowLayout.LEFT);

        setLayout( new FlowLayout()); //Yellow button layout.
        yellowButton = new JButton("Wait");
        yellowButton.addActionListener(this);
        this.add(yellowButton,FlowLayout.CENTER);

        setLayout( new FlowLayout()); //Green button layout.
        greenButton = new JButton("Go");
        greenButton.addActionListener(this);
        this.add(greenButton,FlowLayout.RIGHT);

        AudioClip idlecar = getAudioClip(getDocumentBase(), "Car Idling Sound Effect.wav");//both my audio clips.
        AudioClip revcar = getAudioClip(getDocumentBase(), "Engine Rev Sound Effect.wav");
        idlecar.play();
    }

   
    public void actionPerformed(ActionEvent event){
        Object source = event.getSource( );//my if else statements declaring what booleans are true and false.
        if(source == redButton){
            one = true;
            two = false;
            three = false;
            
            repaint();
            
        }
        else if(source == yellowButton){
            two = true;
            one = false;
            three = false;
            repaint();
            revcar.play();
        }
         else if(source == greenButton){
            two = false;
            one = false;
            three = true;
            repaint();
            revcar.play();
        }
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        g.setColor(new Color(192,192,192));
        g.fillRect(0,350,500,100);              //Main road.

       
        g.setColor(new Color (255,255,255)); //the lines in the road.
        g.fillRect(0,395,45,10);
        g.fillRect(75,395,45,10);
        g.fillRect(150,395,45,10);
        g.fillRect(225,395,45,10);
        g.fillRect(300,395,45,10);
        g.fillRect(375,395,45,10);
        g.fillRect(450,395,45,10);

        Image img=getImage(getDocumentBase(),"Car.png"); //loading my car image.
        

        g.setColor(Color.BLACK);    //my traffic light.
        g.fillRect(350,45,90,200);
        g.setColor(new Color(114,30,30));
        g.fillOval(365,50,60,60);
        g.setColor(new Color(114,111,30));
        g.fillOval(365,115,60,60);
        g.setColor(new Color(38,114,30));
        g.fillOval(365,180,60,60);

        if(one==true){
            g.setColor(Color.RED);//The "light" that draws over the red oval when stop is clicked.
            g.fillOval(365,50,60,60);
            g.drawImage(img, 20,400, this);// the car movement.
            
        }
        if(two==true){
            g.setColor(Color.YELLOW);//Yellow "light".
            g.fillOval(365,115,60,60);
            g.drawImage(img, 300,400, this);// car movement.

        }
        if(three==true){
            g.setColor(Color.GREEN); //Green "light".
            g.fillOval(365,180,60,60);
            g.drawImage(img, 450,400, this);//car movement.

        }
    }
}


