Monday, January 16, 2012

Traffic Light Java Programming

The following is a collection of three files that combine to produce a  animated picture of a working traffic light. If you push the button, the light changes.

Traffic Light(The guts of the program)

//************************************************
// Traffic_Light.java
// CS151A, Assignment 6
// Draws traffic light and keeps track of its state.
//************************************************


import java.util.*;
import javax.swing.*;
import java.awt.*;

public class Traffic_Light extends JPanel
{
    //Instance Data//

    int length;
    int height;
    int state;


    //Constructor//

    public Traffic_Light(int x,int y,int color)
    {
        x = length;
        y = height;
        color = state;
        state = 1;


        setPreferredSize (new Dimension(200,500));
    }

    //Drawing the light//

    public void paintComponent(Graphics page)
    {
        //Background//
        page.setColor(Color.black);
        page.fillRect(0,0,200,500);

        //Red Light//
        if (state == 3)
            page.setColor(Color.red);
                else page.setColor(Color.gray);
                    page.fillOval(20,20,150,150);

        //Yellow Light//
        if (state == 2)
            page.setColor(Color.yellow);
                else page.setColor(Color.gray);
                    page.fillOval(20,180,150,150);

        //Green Light//
        if (state == 1)
            page.setColor(Color.green);
                else page.setColor(Color.gray);
                    page.fillOval(20,340,150,150);
    }

    //Change Method//

    public void change()
    {
        if (state < 3)
        {
            state++;
            repaint(state);
        }
        else
        {
            state = 1;
            repaint(state);
        }

    }

}

Traffic Light Control Panel

//****************************************************************************
// Traffic_Control_Panel.java
// CS151A, Assignment 6
// Creates the panel in which the button and lights will be displayed. Contains Listener Method
//****************************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Traffic_Control_Panel extends JPanel
{
    public JButton push;
    Traffic_Light light;
    int state;

    public Traffic_Control_Panel(int x, int y)
    {
        //Contructor, creats new Traffic_Light Object//

        light = new Traffic_Light(100,200,1);
        add(light);

        //Sets preferred size of panel//

        setPreferredSize (new Dimension(300,600));

        //Creats a button//

        push = new JButton ("Change Light");
        push.addActionListener (new ButtonListener());

        add(push);
        setBackground(Color.cyan);
    }
    //Creates a Listener for the button//
    private class ButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            if (event.getSource() == push)
                light.change();
        }
    }
}

Traffic Light Driver

//*************************************************
// Traffic_Light_Driver.java // CS151A, Assignment 6
// Contains the main method.
//*************************************************

import javax.swing.*;
import java.awt.*;

public class Traffic_Light_Driver
{
    public static void main (String[] args)
    {
        int x = 0;
        int y = 0;
        JFrame frame = new JFrame ("Traffic_Light_Driver");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(new Traffic_Control_Panel(x,y));
        frame.pack();
        frame.setVisible(true);
    }
}

No comments:

Post a Comment