Write an application that draws a traffic light and uses a push button to change
ID: 3676040 • Letter: W
Question
Write an application that draws a traffic light and uses a push button to change the state of the light. Derive the drawing surface from the JPanel class and use another panel to organize the drawing surface and the button.
After completing this assignment you should have two files PP4_17.java(the file with the main method in it) and Trafficlight.java
You should have three colors on the light: red/yellow/green
The next color chosen should follow the typical pattern for a traffic light- green/yellow/red and then back to green again.
This is what I have but I am having some errors and I don't know what they are.
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class Trafficlight extends JPanel
{
//int that holds count
private int count;
//hold push in Jbutton
private JButton push;
public Trafficlight()
{
//The first click will be equal to one
count = 0;
push = new JButton ("Push");
push.addActionListener(new ButtonListener());
add(push);
setPreferredSize (new Dimension(300,300));
}
public void paintComponent(Graphics page)
{
//Constructor: Creates three circle objects
super.paintComponent(page);
page.setColor(Color.yellow);
page.fillRect(100,100,100,100);
page.setColor(Color.black);
page.fillOval(100, 100, 100, 100);
page.fillOval(100, 100, 100, 100);
page.fillOval(100, 100, 100, 100);
page.setColor(Color.white);
page.fillOval(100, 100, 100,100);
page.fillOval(100, 100, 100,100);
page.fillOval(100, 100, 100,100);
//Draws lights that fit in the circles
//If the button is clicked once the green light will display
if (count == 1)
{
page.setColor(Color.green);
page.fillOval(100, 100, 100,100);
}
//If the button is clicked twice the yellow light will display
if (count == 2)
{
page.setColor(Color.yellow);
page.fillOval(100, 100, 100,100);
}
//If the button is clicked three times the red light will display
if (count == 3)
{
page.setColor(Color.red);
page.fillOval(100, 100, 100,100);
count=0;
}
//Represents a listener for button psuh (action) events.
private class ButtonListener implements ActionListener
{
//Updates tje consturctor when button is pushed
public void actionPerformed(ActionEvent event)
{
count ++;
repaint();
}
}//end
}
}//end class
----------------------------------------------------------------------------------------------------------------------------
import javax.swing.JFrame;
public class PP4_17
{
//Ctrates the main frame of the program.
public static void main(String[] args)
{
JFrame frame = new JFrame("Traffic light");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Trafficlight panel = new Trafficlight();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}//end main
}//end class
Explanation / Answer
TrafficLightPanel.java
import java.awt.*;
import javax.swing.*;
public class TrafficLightPanel extends JPanel
{
public int currentLight, red = 1, yellow = 2, green = 3;
public TrafficLightPanel()
{
setBackground(Color.darkGray);
setPreferredSize(new Dimension(300, 400));
}
public void paintComponent(Graphics light)
{
super.paintComponent(light);
light.setColor(Color.black);
light.fillRect(75, 40, 150, 300);
light.setColor(Color.red);
light.fillOval(112, 50, 75, 75);
light.setColor(Color.gray);
light.fillOval(112, 150, 75, 75);
light.setColor(Color.gray);
light.fillOval(112, 250, 75, 75);
if (currentLight == yellow)
{
light.setColor(Color.gray);
light.fillOval(112, 50, 75, 75);
light.setColor(Color.yellow);
light.fillOval(112, 150, 75, 75);
light.setColor(Color.gray);
light.fillOval(112, 250, 75, 75);
}
else if(currentLight == green)
{
light.setColor(Color.gray);
light.fillOval(112, 50, 75, 75);
light.setColor(Color.gray);
light.fillOval(112, 150, 75, 75);
light.setColor(Color.green);
light.fillOval(112, 250, 75, 75);
}
}
public void setLight(int light)
{
currentLight = light;
}
}
TrafficLightControls.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TrafficLightControls extends JPanel
{
private TrafficLightPanel traffic;
private JButton changeLight;
private int count = 1;
public TrafficLightControls(TrafficLightPanel trafficLight)
{
traffic = trafficLight;
changeLight = new JButton("Change the Light");
changeLight.addActionListener(new lightListener());
add(changeLight);
}
private class lightListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(count >= 3)
{
count = 1;
}
else
{
count++;
}
if(count <= 3 && count >= 1)
{
if(count == 2)
{
traffic.setLight(2);
traffic.repaint();
}
else if (count == 3)
{
traffic.setLight(3);
traffic.repaint();
}
else
{
traffic.setLight(1);
traffic.repaint();
}
}
else
{
count = 0;
}
}
}
}
TrafficLight.java
import javax.swing.*;
import java.awt.*;
public class TrafficLight
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Traffic Light");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TrafficLightPanel trafficLight = new TrafficLightPanel();
TrafficLightControls controls = new TrafficLightControls(trafficLight);
JPanel panel = new JPanel();
panel.add(trafficLight);
panel.add(controls);
panel.setBackground(Color.darkGray);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.