Traffic Light using Applet in Java | algorithm and source code free 1


Traffic Light using Applet in Java | algorithm and source code

Traffic signal automation using java | Applet program in java

Aim (project using applet in java)

To design Traffic Light using Applet in Java

Algorithm for Traffic Light Design | Applet in java

  1. To design the TLD se the packages by importing it from  java. applet,  awt,  awt. Event
  2. Use Applet implements ItemListener,
  3. init() create the CheckboxGroup() and Checkbox() add it to addItemListener()
  4. Use repaint() to call the paint method repeatedly until certain condition.
  5. Paint method design signal using drawOval()
  6. Alter the signal using setColor(Color.red/green/orange)

Source code to implement traffic light application | Applet in java

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();
}
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 | Applet in java project

Traffic Light using Applet in Java | algorithm and source code

Applet in Java Project Recommended Post

Traffic Light using Applet in Java | algorithm and source code

Python Recommended Post

Java Recommended Post

traffic light project using applet in java, algorithm and source code | traffic light project using applet in java, algorithm and source code | traffic light project using applet in java, algorithm and source code | traffic light project using applet in java, algorithm and source code |

https://en.wikipedia.org/wiki/Java_applet

https://en.wikipedia.org/wiki/Traffic_light

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Domain & free Hosting

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.