I need two different GUI applications for a traffic light that changes lights ev
ID: 3555935 • Letter: I
Question
I need two different GUI applications for a traffic light that changes lights every two seconds. The traffic light has to be sideways. Here is the assignent that is due tonight:
Write a GUI application program using a JFrame object. It will be a traffic light that will show a red circle at the beginning. After two seconds, it will change to show a yellow circle, then after another two seconds, it will show a green circle. It will keep repeating these three steps forever.
Initially it should show a red circle:
After two seconds, it will show a
yellow circle
After two seconds, it will show
a green circle.
And it will repeat these three steps. (after two seconds, it will go back to show a red circle.)
You need to write this program using two different ways:
Exercise 1: using the Timer class in the java.util package.
Exercise 2: using the Timer class in the javax.swing package.
public class LightDrive class
{
public static void main(String[] args)
{
TrafficLight light = new TrafficLight();
}
}
// you can create two versions of TrafficLight class, one with java.util.Timer,
//one with javax.swing.Timer.
public class TrafficLight
Explanation / Answer
//===========================java.util.Timer
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class LightDrive2 {
public static void main( String [] args){
final TrafficLight light = new TrafficLight();
}
}
public class TrafficLight extends JFrame{
Color currentColor;
public TrafficLight(){
currentColor = Color.RED;
/*
* Timer TASK
*/
Timer t = new Timer();
t.schedule(new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stuB
repaint();
}
}, 2000, 2000);
this.setBounds(100, 100, 100, 300);
this.setResizable(false);
this.setVisible(true);
}
public void paint( Graphics g){
Rectangle b = this.getBounds();
g.setColor(Color.BLACK);
g.fillRect(25, 0, 100, 300);
g.setColor(Color.GRAY);
g.fillOval(50, 30, 50, 50);
g.fillOval(50, 25+100, 50, 50);
g.fillOval(50, 25+200, 50, 50);
if( currentColor == Color.RED){
g.setColor(Color.YELLOW);
g.fillOval(50, 25+100, 50, 50);
currentColor = Color.YELLOW;
}else if( currentColor == Color.YELLOW){
g.setColor(Color.GREEN);
g.fillOval(50, 25+200, 50, 50);
currentColor = Color.GREEN;
}else {
g.setColor(Color.RED);
g.fillOval(50, 30, 50, 50);
currentColor = Color.RED;
}
}
}
//==============================javax.swting.Timer=====================
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;
public class LightDrive {
public static void main( String [] args){
final TrafficLight light = new TrafficLight();
}
}
public class TrafficLight extends JFrame{
Color currentColor;
public TrafficLight(){
currentColor = Color.RED;
/*
* Timer TASK
*/
int delay = 2000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
repaint();
}
};
((Timer) new Timer(delay, taskPerformer)).start();
this.setBounds(100, 100, 100, 300);
this.setResizable(false);
this.setVisible(true);
}
public void paint( Graphics g){
Rectangle b = this.getBounds();
g.setColor(Color.BLACK);
g.fillRect(25, 0, 100, 300);
g.setColor(Color.GRAY);
g.fillOval(50, 30, 50, 50);
g.fillOval(50, 25+100, 50, 50);
g.fillOval(50, 25+200, 50, 50);
if( currentColor == Color.RED){
g.setColor(Color.YELLOW);
g.fillOval(50, 25+100, 50, 50);
currentColor = Color.YELLOW;
}else if( currentColor == Color.YELLOW){
g.setColor(Color.GREEN);
g.fillOval(50, 25+200, 50, 50);
currentColor = Color.GREEN;
}else {
g.setColor(Color.RED);
g.fillOval(50, 30, 50, 50);
currentColor = Color.RED;
}
}
}