Write a JAVA program that would simulate traffic lights using the concept of Mul
ID: 3742657 • Letter: W
Question
Write a JAVA program that would simulate traffic lights using the concept of Multithreading. One example is shown in the following figure. Each light has two statuses, "on" and "off". Moreover, the time of each light switching status can be set using the text input box beforehand. If the user clicks on the "Start" button, the program starts to work. While if the "End" button is clicked, the program stops working but not terminates. The program terminates only when the "X" button on right-upper corner. Please note that initially the switching time for each light would be set to "3". As shown in the last column of the following figure, you should also display the text indicating the status of the traffic light.
I have found answer of this program but it is not working and you can check the error pop up box image down below. can you please solve this for me?
Green Green Yellow Red Yellow 3 Start Stop stop Traffic light demo change light color now light:l after 2 seconds will change color now light:1 after1 seconds will change color now light :1 after 0 seconds ill change color change lisht color now light:2 after 2 seconds will change color now light:2 after 1 seconds will change color now light:2 after 0 seconds will change color change 1ight color Green Yellow RedExplanation / Answer
JAVA program that would simulate traffic lights using the concept of Multithreading :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TrafficLight extends JFrame implements Runnable
{
JButton red, green, yellow ;
TrafficLight()
{
setTitle("TrafficLight") ;
setSize(500,500) ;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
red = new JButton() ;
yellow = new JButton() ;
green = new JButton() ;
setLayout(new GridLayout(3,1));
this.add(red); this.add(yellow); this.add(green) ;
Thread t = new Thread(this) ;
t.start();
}
public void run()
{
while(true)
{
Thread turnRed = new Thread(new TurnRed());
turnRed.start() ;
synchronized(turnRed)
{
try { turnRed.wait() ; } catch(InterruptedException e) {}
}
Thread turnYellow = new Thread(new TurnYellow());
turnYellow.start() ;
synchronized(turnYellow)
{
try { turnYellow.wait() ; } catch(InterruptedException e) {}
}
Thread turnGreen = new Thread(new TurnGreen());
turnGreen.start() ;
synchronized(turnGreen)
{
try { turnGreen.wait() ; } catch(InterruptedException e) {}
}
}
}
public static void main(String[] args)
{
new TrafficLight().setVisible(true);
}
class TurnRed implements Runnable
{
public void run()
{
synchronized(this) {
green.setBackground(Color.WHITE) ;
red.setBackground(Color.RED) ;
try { Thread.sleep(1000); } catch(InterruptedException e) {}
notify(); }
}
}
class TurnYellow implements Runnable
{
public void run()
{
synchronized(this) {
red.setBackground(Color.WHITE) ;
yellow.setBackground(Color.YELLOW) ;
try { Thread.sleep(1000); } catch(InterruptedException e) {}
notify(); }
}
}
class TurnGreen implements Runnable
{
public void run()
{
synchronized(this) {
yellow.setBackground(Color.WHITE) ;
green.setBackground(Color.GREEN) ;
try { Thread.sleep(1000); } catch(InterruptedException e) {}
notify(); }
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.