CODE FOR TRAFFIC MANAGEMENT SYSTEM
Traffic management is one of the issues that needs to care.
Congestion may result due to heavy
traffic at a
junction. In developing cities like
traffic management is
becoming important issue day by day due
to rapid
increase in number of vehicles. Lot of
man-hours are
being
wasted in traveling due to bad traffic management.
JAVA CODE
NOTE:SAVE THE FOLLOWING CODE WITH TrafficLight.java
<code>
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
class TrafficLight
{
static JFrame f1;
static Thread t1;
static Thread t2;
static Thread t3;
static Thread t4;
TrafficLight()
{
f1=new JFrame("Traffic Control");
f1.setLayout(null);
f1.setBackground(Color.lightGray);
f1.setSize(800,600);
f1.setVisible(true);
}
public static void main(String[] st)
{
TrafficLight tl=new TrafficLight();
TrafficLight1 tl1=new TrafficLight1(f1);
TrafficLight2 tl2=new TrafficLight2(f1);
TrafficLight3 tl3=new TrafficLight3(f1);
TrafficLight4 tl4=new TrafficLight4(f1);
Thread t1=new Thread(tl1);
Thread t2=new Thread(tl2);
Thread t3=new Thread(tl3);
Thread t4=new Thread(tl4);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
//-------------------------------------------------------------------
class TrafficLight2 implements Runnable
{ Graphics g;
JFrame jf;
Font f,f1;
int i=0,a=0,j=0;
JLabel lb1,lb2;
TrafficLight2( JFrame ft)
{
this.jf=ft;
g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel();
lb2=new JLabel();
}
int t=50;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal2();
try{
TrafficLight.t3.start();
wait(3*61*1000);}catch(Exception e){System.out.println(e);}
}
else
if(i<=3 && i>0)//yellow
{
a=2;
signal2();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal2();
}
if(j==25)//end of green(return to red)
{
t=75;
run();
}
}
}
signal2();
}
public void signal2()
{
jf.setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//POLE UP
g.fillRect(350,50,50,150);
g.drawRect(350,50,50,150);
g.drawOval(350,50,50,50);//RED
g.drawOval(350,100,50,50);//YELLOW
g.drawOval(350,150,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
//g.drawString(""+i,350,20);
lb1.setBounds(250,40,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(350,50,50,50);
g.drawOval(350,50,50,50);
g.drawString("STOP",420,80);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(350,100,50,50);
g.drawOval(350,100,50,50);
g.drawString("READY",420,130);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,400,20);
lb2.setBounds(275,40,30,20);
lb2.setText(""+j);
jf.add(lb2);
g.setColor(Color.green);
g.fillOval(350,150,50,50);
g.drawOval(350,150,50,50);
g.drawString("GO",420,180);
}
}
}
//------------------------------------------------------------------
class TrafficLight1 implements Runnable
{
Graphics g;
JFrame jf;
JLabel lb1,lb2;
Font f,f1;
int i=0,a=0,j=0;
TrafficLight1( JFrame ft)
{
this.jf=ft;
this.g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel("");
lb2=new JLabel("");
}
int t=25;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal();try{
TrafficLight.t2.start();
wait(3*60*1000);}catch(Exception e){System.out.println(e);}
}
else if(i<=3 && i>0)//yellow
{
a=2;
signal();
}
else if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal();
}
if(j==25)//end of green(return to red)
{
t=75;
run();
}
}
}
signal();
}
public void signal()
{
jf.setBackground(Color.lightGray);//ROAD
g=jf.getGraphics();
g.setColor(Color.black);//pole top
g.fillArc(100,150,100,100,0,180);
g.drawArc(100,150,100,100,0,180);
g.setColor(Color.black);//POLE UP
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);
g.setColor(Color.black);//POLE DOWN
g.fillRect(165,300,20,155);
g.drawRect(165,300,20,155);
g.drawOval(150,150,50,50);//RED
g.drawOval(150,200,50,50);//YELLOW
g.drawOval(150,250,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
//g.drawString(""+i,50,100);
lb1.setBounds(50,50,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(150,150,50,50);
g.drawOval(150,150,50,50);
g.drawString("STOP",20,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(150,200,50,50);
g.drawOval(150,200,50,50);
g.drawString("READY",20,250);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,150,100);
lb2.setBounds(80,50,30,20);lb2.setText(""+j);
jf.add(lb2);if(j==24){lb2.setText("0");}
g.setColor(Color.green);
g.fillOval(150,250,50,50);
g.drawOval(150,250,50,50);
g.drawString("GO",20,300);
}
}
}
//----------------------------------------------------------------
class TrafficLight3 implements Runnable
{
Graphics g;
JFrame jf;
Font f,f1;
int i=0,a=0,j=0;
JLabel lb1,lb2;
TrafficLight3( JFrame ft)
{
this.jf=ft;
this.g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel();
lb2=new JLabel();
}
int t=75;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal3();
try{
TrafficLight.t4.start();
wait(3*61*1000);}catch(Exception e){System.out.println(e);}
}
else
if(i<=3 && i>0)//yellow
{
a=2;
signal3();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal3();
}
if(j==25)//end of green(return to red)
{
int t=75;
run();
}
}
}
signal3();
}
public void signal3()
{
jf.setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//pole top2
g.fillArc(500,150,100,100,0,180);
g.drawArc(500,150,100,100,0,180);
g.setColor(Color.black);//POLE UP2
g.fillRect(500,150,50,150);
g.drawRect(500,150,50,150);
g.setColor(Color.black);//POLE DOWN2
g.fillRect(515,300,20,155);
g.drawRect(515,300,20,155);
g.drawOval(500,150,50,50);//RED2
g.drawOval(500,200,50,50);//YELLOW2
g.drawOval(500,250,50,50);//GREEN2
g.setColor(Color.red);//COUNTDOWNSTOP
g.setFont(f);
//g.drawString(""+i,650,100);
lb1.setBounds(650,50,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(500,150,50,50);
g.drawOval(500,150,50,50);
g.drawString("STOP",600,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(500,200,50,50);
g.drawOval(500,200,50,50);
g.drawString("READY",600,200);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,700,100);
lb2.setBounds(680,50,30,20);
lb2.setText(""+j);
jf.add(lb2);
g.setColor(Color.green);
g.fillOval(500,250,50,50);
g.drawOval(500,250,50,50);
g.drawString("GO",600,250);
}
}
}
//-------------------------------------------------------------------
class TrafficLight4 implements Runnable
{
Graphics g;
JFrame jf;
Font f,f1;
int i=0,a=0,j=0;
JLabel lb1,lb2;
TrafficLight4( JFrame ft)
{
this.jf=ft;
this.g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel();
lb2=new JLabel();
}
int t=100;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal4();try{
TrafficLight.t1.notify();
wait(3*60*1000);}catch(Exception e){System.out.println(e);}
}
else
if(i<=3 && i>0)//yellow
{
a=2;
signal4();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal4();
}
if(j==25)//end of green(return to red)
{
t=75;
run();
}
}
}
signal4();
}
public void signal4()
{
jf.setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//POLE UP
g.fillRect(350,350,50,150);
g.drawRect(350,350,50,150);
g.drawOval(350,350,50,50);//RED
g.drawOval(350,400,50,50);//YELLOW
g.drawOval(350,450,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
//g.drawString(""+i,350,320);
lb1.setBounds(340,300,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(350,350,50,50);
g.drawOval(350,350,50,50);
g.drawString("STOP",420,380);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(350,400,50,50);
g.drawOval(350,400,50,50);
g.drawString("READY",420,430);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,380,320);
lb2.setBounds(380,300,30,20);
lb2.setText(""+j);
jf.add(lb2);
g.setColor(Color.green);
g.fillOval(350,450,50,50);
g.drawOval(350,450,50,50);
g.drawString("GO",420,480);
}
}
}
import java.awt.*;
import javax.swing.*;
import java.applet.*;
class TrafficLight
{
static JFrame f1;
static Thread t1;
static Thread t2;
static Thread t3;
static Thread t4;
TrafficLight()
{
f1=new JFrame("Traffic Control");
f1.setLayout(null);
f1.setBackground(Color.lightGray);
f1.setSize(800,600);
f1.setVisible(true);
}
public static void main(String[] st)
{
TrafficLight tl=new TrafficLight();
TrafficLight1 tl1=new TrafficLight1(f1);
TrafficLight2 tl2=new TrafficLight2(f1);
TrafficLight3 tl3=new TrafficLight3(f1);
TrafficLight4 tl4=new TrafficLight4(f1);
Thread t1=new Thread(tl1);
Thread t2=new Thread(tl2);
Thread t3=new Thread(tl3);
Thread t4=new Thread(tl4);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
//-------------------------------------------------------------------
class TrafficLight2 implements Runnable
{ Graphics g;
JFrame jf;
Font f,f1;
int i=0,a=0,j=0;
JLabel lb1,lb2;
TrafficLight2( JFrame ft)
{
this.jf=ft;
g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel();
lb2=new JLabel();
}
int t=50;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal2();
try{
TrafficLight.t3.start();
wait(3*61*1000);}catch(Exception e){System.out.println(e);}
}
else
if(i<=3 && i>0)//yellow
{
a=2;
signal2();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal2();
}
if(j==25)//end of green(return to red)
{
t=75;
run();
}
}
}
signal2();
}
public void signal2()
{
jf.setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//POLE UP
g.fillRect(350,50,50,150);
g.drawRect(350,50,50,150);
g.drawOval(350,50,50,50);//RED
g.drawOval(350,100,50,50);//YELLOW
g.drawOval(350,150,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
//g.drawString(""+i,350,20);
lb1.setBounds(250,40,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(350,50,50,50);
g.drawOval(350,50,50,50);
g.drawString("STOP",420,80);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(350,100,50,50);
g.drawOval(350,100,50,50);
g.drawString("READY",420,130);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,400,20);
lb2.setBounds(275,40,30,20);
lb2.setText(""+j);
jf.add(lb2);
g.setColor(Color.green);
g.fillOval(350,150,50,50);
g.drawOval(350,150,50,50);
g.drawString("GO",420,180);
}
}
}
//------------------------------------------------------------------
class TrafficLight1 implements Runnable
{
Graphics g;
JFrame jf;
JLabel lb1,lb2;
Font f,f1;
int i=0,a=0,j=0;
TrafficLight1( JFrame ft)
{
this.jf=ft;
this.g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel("");
lb2=new JLabel("");
}
int t=25;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal();try{
TrafficLight.t2.start();
wait(3*60*1000);}catch(Exception e){System.out.println(e);}
}
else if(i<=3 && i>0)//yellow
{
a=2;
signal();
}
else if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal();
}
if(j==25)//end of green(return to red)
{
t=75;
run();
}
}
}
signal();
}
public void signal()
{
jf.setBackground(Color.lightGray);//ROAD
g=jf.getGraphics();
g.setColor(Color.black);//pole top
g.fillArc(100,150,100,100,0,180);
g.drawArc(100,150,100,100,0,180);
g.setColor(Color.black);//POLE UP
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);
g.setColor(Color.black);//POLE DOWN
g.fillRect(165,300,20,155);
g.drawRect(165,300,20,155);
g.drawOval(150,150,50,50);//RED
g.drawOval(150,200,50,50);//YELLOW
g.drawOval(150,250,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
//g.drawString(""+i,50,100);
lb1.setBounds(50,50,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(150,150,50,50);
g.drawOval(150,150,50,50);
g.drawString("STOP",20,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(150,200,50,50);
g.drawOval(150,200,50,50);
g.drawString("READY",20,250);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,150,100);
lb2.setBounds(80,50,30,20);lb2.setText(""+j);
jf.add(lb2);if(j==24){lb2.setText("0");}
g.setColor(Color.green);
g.fillOval(150,250,50,50);
g.drawOval(150,250,50,50);
g.drawString("GO",20,300);
}
}
}
//----------------------------------------------------------------
class TrafficLight3 implements Runnable
{
Graphics g;
JFrame jf;
Font f,f1;
int i=0,a=0,j=0;
JLabel lb1,lb2;
TrafficLight3( JFrame ft)
{
this.jf=ft;
this.g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel();
lb2=new JLabel();
}
int t=75;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal3();
try{
TrafficLight.t4.start();
wait(3*61*1000);}catch(Exception e){System.out.println(e);}
}
else
if(i<=3 && i>0)//yellow
{
a=2;
signal3();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal3();
}
if(j==25)//end of green(return to red)
{
int t=75;
run();
}
}
}
signal3();
}
public void signal3()
{
jf.setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//pole top2
g.fillArc(500,150,100,100,0,180);
g.drawArc(500,150,100,100,0,180);
g.setColor(Color.black);//POLE UP2
g.fillRect(500,150,50,150);
g.drawRect(500,150,50,150);
g.setColor(Color.black);//POLE DOWN2
g.fillRect(515,300,20,155);
g.drawRect(515,300,20,155);
g.drawOval(500,150,50,50);//RED2
g.drawOval(500,200,50,50);//YELLOW2
g.drawOval(500,250,50,50);//GREEN2
g.setColor(Color.red);//COUNTDOWNSTOP
g.setFont(f);
//g.drawString(""+i,650,100);
lb1.setBounds(650,50,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(500,150,50,50);
g.drawOval(500,150,50,50);
g.drawString("STOP",600,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(500,200,50,50);
g.drawOval(500,200,50,50);
g.drawString("READY",600,200);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,700,100);
lb2.setBounds(680,50,30,20);
lb2.setText(""+j);
jf.add(lb2);
g.setColor(Color.green);
g.fillOval(500,250,50,50);
g.drawOval(500,250,50,50);
g.drawString("GO",600,250);
}
}
}
//-------------------------------------------------------------------
class TrafficLight4 implements Runnable
{
Graphics g;
JFrame jf;
Font f,f1;
int i=0,a=0,j=0;
JLabel lb1,lb2;
TrafficLight4( JFrame ft)
{
this.jf=ft;
this.g=ft.getGraphics();
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
lb1=new JLabel();
lb2=new JLabel();
}
int t=100;
public void run()
{
for(i=t;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=t && i>3)//red
{
a=1;
signal4();try{
TrafficLight.t1.notify();
wait(3*60*1000);}catch(Exception e){System.out.println(e);}
}
else
if(i<=3 && i>0)//yellow
{
a=2;
signal4();
}
else
if(i==0)//green
{
for(j=0;j<25;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
signal4();
}
if(j==25)//end of green(return to red)
{
t=75;
run();
}
}
}
signal4();
}
public void signal4()
{
jf.setBackground(Color.lightGray);//ROAD
g.setColor(Color.black);//POLE UP
g.fillRect(350,350,50,150);
g.drawRect(350,350,50,150);
g.drawOval(350,350,50,50);//RED
g.drawOval(350,400,50,50);//YELLOW
g.drawOval(350,450,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(f);
//g.drawString(""+i,350,320);
lb1.setBounds(340,300,30,20);
lb1.setText(""+i);
jf.add(lb1);
g.setColor(Color.white);//CROSSING1
g.fillRect(300,5,15,125);
g.drawRect(300,5,15,125);
g.setColor(Color.white);
g.fillRect(300,145,15,135);
g.drawRect(300,145,15,135);
g.setColor(Color.white);
g.fillRect(300,300,15,135);
g.drawRect(300,300,15,135);
g.setColor(Color.white);//CROSSING2
g.fillRect(450,5,15,125);
g.drawRect(450,5,15,125);
g.setColor(Color.white);
g.fillRect(450,145,15,135);
g.drawRect(450,145,15,135);
g.setColor(Color.white);
g.fillRect(450,300,15,135);
g.drawRect(450,300,15,135);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(350,350,50,50);
g.drawOval(350,350,50,50);
g.drawString("STOP",420,380);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(350,400,50,50);
g.drawOval(350,400,50,50);
g.drawString("READY",420,430);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.setFont(f);
//g.drawString(""+j,380,320);
lb2.setBounds(380,300,30,20);
lb2.setText(""+j);
jf.add(lb2);
g.setColor(Color.green);
g.fillOval(350,450,50,50);
g.drawOval(350,450,50,50);
g.drawString("GO",420,480);
}
}
}
</code>
No comments:
Post a Comment