Write an application that draws a traffic light and uses a push button to change
ID: 3676151 • 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
Slightly changed your code.
import java.awt.*;
import javax.swing.*;
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( new TrafficPanel ( ) ) ;
frame.pack();
frame.setVisible(true);
}//end main
}//end class
-----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.*;
public class Trafficlight extends JPanel
{
//int that holds count
private int count;
//hold push in Jbutton
private JButton push;
private JLabel label;
public Trafficlight()
{
int red = 0;
int yellow = 1;
int green = 2;
//The first click will be equal to one
count = 0;
push = new JButton ("Push");
push.addActionListener(new ButtonListener());
label =new JLabel( " Traffic Lights " ) ;
add(push);
add( label ) ;
setPreferredSize (new Dimension(300,300));
setBackground( Color.black ) ;
}
public void paintComponent(Graphics page)
{
//Constructor: Creates three circle objects
Trafficlight.super.paintComponent( page ) ;
//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);
}
page.setColor ( Color.white ) ;
}//end
//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();
count =count %3 ; // the variable counter takes to 0 , 1 (or) 2
}
}
}//end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.