Traffic Light using Applet in Java with algorithm and source code

Traffic Light System using Applet in Java with algorithm and source code

In this program, we learn to build a Traffic light system, about traffic light control and we will create three different traffic light green, red and yellow. In this project, the light of the traffic will automatically change after some particular second.

Algorithm for Traffic Light Design using Applet in java

  1. To design the TLD by importing packages
  2. import java. applet.*;
  3. import java.awt.*;
  4. import java.event.*;
  5. Write applet code inside the comment
    1. /*<applet code=”Traffic” width=700 height=600> </applet>*/
  6. Create class Traffic extends Applet implements Runnable
  7. Create Thread t;
  8. create function
    1. public void start() {
    2. t=new Thread(this);
    3. t.start();
  9. public void run()
  10. {
  11. for(i=20;i>=0;i–){ //countdown
  12. public void run(){
    1. for(i=20;i>=0;i–)//countdown
      1. Thread.sleep(1000);
  13. init() create the : CheckboxGroup() and Checkbox() add it to addItemListener()
  14. Use repaint() to call: the paint method repeatedly until certain condition.
  15. Paint method design: signal using drawOval()
  16. Alter the signal using: setColor(Color.red/green/orange)

Source Code to Implement Traffic Light System

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code=”Traffic” width=700 height=600>
</applet>*/
public class Traffic extends Applet implements Runnable
{
Thread t;
int i=0,a=0,j=0;
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
for(i=20;i>=0;i–)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=20 && i>3)//red
{
a=1;
repaint();
}
else
if(i<=3 && i>0)//yelloe
{
a=2;
repaint();
}
else
if(i==0)//green
{
for(j=0;j<20;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
repaint();
}
if(j==20)//end of green(return to red)
{
run();
}
}
}
repaint();
}
// graphical design of traffic light
public void paint(Graphics g)
{
g.setColor(Color.black);//pole top
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);
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.drawString(“”+i,50,50);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(150,150,50,50);
g.drawOval(150,150,50,50);
g.drawString(“STOP”,50,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(150,200,50,50);
g.drawOval(150,200,50,50);
g.drawString(“READY”,50,200);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.drawString(“”+j,150,50);
g.setColor(Color.green);
g.fillOval(150,250,50,50);
g.drawOval(150,250,50,50);
g.drawString(“GO”,50,250);
}
}
}

Output

<!-- wp:heading -->
<h2><strong>Traffic Light System using Applet in Java with algorithm and source code</strong></h2>


<p>In this program, we learn to build a Traffic light system, about traffic light control and we will create three different traffic light green, red and yellow. In this project, the light of the traffic will automatically change after some particular second.</p>

Recommended Posts:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment