Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

java program to create animated traffic signal

Ads

 
 

Share on Google+Share on Google+

Pari Kapoor
java program to create animated traffic signal
2 Answer(s)      5 years and 10 months ago
Posted in : Java Beginners

a java program to create animated traffic signal (use 5 seconds to blink each light one after the other) using multithreading.

Ads
View Answers

September 7, 2011 at 12:58 PM


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class traffic extends Applet implements Runnable{
Thread t;
Font f,f1;
int i=0,a=0,j=0;
public void init(){

setBackground(Color.lightGray);
f=new Font("TimesNewRoman",f.ITALIC,28);
f1=new Font("TimesNewRoman",Font.ITALIC+Font.BOLD,28);
}
public void start() {
t=new Thread(this);
t.start();
}
 public void run(){
  for(i=25;i>=0;i--){
   try {
Thread.sleep(5000);
}
catch(Exception e){
System.out.println(e);
}
if(i<=25 && i>3){
a=1;
repaint();
}
else
if(i<=3 && i>0){
a=2;
repaint();
}
else
if(i==0){
for(j=0;j<25;j++){
a=3;
try {
Thread.sleep(5000);
}
catch(Exception e){
System.out.println(e);
}
  repaint();
}
 if(j==25){
run();
}
  }
  }
 repaint();
}


September 7, 2011 at 1:00 PM


continue..

 public void paint(Graphics g) {
setBackground(Color.lightGray);
g.setColor(Color.black);
g.fillArc(100,150,100,100,0,180);
g.drawArc(100,150,100,100,0,180);

g.setColor(Color.black);
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);

g.setColor(Color.black);
g.fillRect(165,300,20,155);
g.drawRect(165,300,20,155);

g.drawOval(150,150,50,50);
g.drawOval(150,200,50,50);
g.drawOval(150,250,50,50);

g.setColor(Color.red);
g.setFont(f);
g.drawString(""+i,50,50);

g.setColor(Color.white);
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);
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);

g.setColor(Color.black);
g.fillRect(600,300,15,135);
g.drawRect(600,300,15,135);

g.setColor(Color.green);
g.fillArc(560,290,100,100,0,180);
g.drawArc(560,290,100,100,0,180);

g.setColor(Color.black);
g.fillRect(460,300,15,135);
g.drawRect(460,300,15,135);

g.setColor(Color.green);
g.fillArc(420,290,100,100,0,180);
g.drawArc(420,290,100,100,0,180);

if(a==1){
g.setColor(Color.red);
g.fillOval(150,150,50,50);
g.drawOval(150,150,50,50);
g.drawString("STOP",50,150);
}
if(a==2){
g.setColor(Color.yellow);
g.fillOval(150,200,50,50);
g.drawOval(150,200,50,50);
g.drawString("READY",50,200);
}
if(a==3){
g.setColor(Color.blue);
g.setFont(f);
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);
 }
int x1[]={220,300,300,280};
int y1[]={250,150,250,150};
int n1=4;
int n2=3;
int x2[]={340,380,380};
int y2[]={150,100,150};
int x3[]={460,460,500};
int y3[]={150,100,150};

g.setColor(Color.black);
g.fillPolygon(x1,y1,n1);
g.drawPolygon(x1,y1,n1);
g.setColor(Color.yellow);
g.fillRect(380,100,80,50);
g.drawRect(380,100,80,50);

g.setColor(Color.yellow);
g.fillPolygon(x2,y2,n2);
g.drawPolygon(x2,y2,n2);

g.setColor(Color.yellow);
g.fillPolygon(x3,y3,n2);
g.drawPolygon(x3,y3,n2);

g.setColor(Color.black);
g.fillOval(440,210,60,60);
g.drawOval(440,210,60,60);

g.setColor(Color.black);
g.fillOval(340,210,60,60);
g.drawOval(340,210,60,60);

g.setColor(Color.red);
g.fillRect(300,150,250,100);
g.drawRect(300,150,250,100);

g.setColor(Color.black);
g.setFont(f1);
g.drawString ("Zumbo",380,200);
 }
}

Ads









Related Tutorials/Questions & Answers:
How to design animated traffic lights
How to design animated traffic lights You would have seen an animated traffic light on the road, I am interested with this so I have decided to design it. You can also make it by using this interesting example so
How to Create Animated Main Menu Application with Java Swnig Buttons - Java Beginners
How to Create Animated Main Menu Application with Java Swnig Buttons  Hello sir I want to display Main Menu after Log in i have created Log in Form but I want More attractive Animated Main Menu which includes Following
Advertisements
How to create first program in Java?
How to create first program in Java?  Hi, I am new in Java programming. Tell me How to create first program in Java? Thanks   Hi, Read more at First Java Program. Thanks
How to create first Java Program?
How to create first Java Program?  Hi, I am beginner and I want to write first Java program. How I can setup my machine and write first Java program... and run first Java program: Create program in Note pad: Step 1: Open
how to make traffic light code in java
how to make traffic light code in java  what is code of traffic light in java
create and use an array variable in a java program
create and use an array variable in a java program  how do i write a program that will prompt the user for a list of 5 prices, once the user has entered all values , your program should compute and display the following: The sum
Create a java program using Java with LinkedLists
Create a java program using Java with LinkedLists  Assignment is -- Question For this part of the assignment, you will develop a Java application... that reverses the list in place (that is, does not create a new copy of the list) Step 2
java program - Java Beginners
java program  1.write a program to show traffic signal using multithreading. 2.Except an integer from 1-12 and display corresponding month. if the integer is not between 1-12 then give the errer message and promit
program to create nested tables
program to create nested tables  program to create nested tables
How to create the program in Java for getting the forgotten Password Form?
How to create the program in Java for getting the forgotten Password Form? ... to the user on email address. How to create the program in Java for getting the forgotten Password Form? Thanks   For this you should create a form
java program to create xml file with append data in well-formed
java program to create xml file with append data in well-formed   Sorry sir your given xml append data program is not well-formed. I'll make you more clear what I want. If this is my xml file Tom Cruise 45 Now when I append
coding for eletric signal graph
coding for eletric signal graph  How can I draw an electric signal from Manchester and Differential Manchester coding from binary bits that converted from a character for example character
Traffic Simulator
Traffic Simulator  Hi, I am doing a project on creating a traffic simulator for a particular road junction in java. I am looking for ideas on how I would go about recreating the visual of the junction in which I can have moving
Create a Program that Calculates Input
Create a Program that Calculates Input   Create a program that takes user input and does a calculation with it, then prints the results back.... For top points, provide options and make the program pretty. (ASCII art &
program to create node.
program to create node.  Hi i'm new to flex and trying to create a node and it should also have an option of right click wit any properties. how do i do this. Also i need to create a gui having drag and dop options. Please help
create a jsp program
create a jsp program  please help , create web Application using jsp and jdbc which tacking your name,id and DOB .On clicking to submit Button it stores the information into the database and Displays alll the records
html program to create home page
html program to create home page  html program to create home page
Java program to create a frame with 4 fields;name,sreet,city,pin;with suitable table
Java program to create a frame with 4 fields;name,sreet,city,pin;with suitable table  Java program to create a frame with 4 fields;name,street,city,pin;with suitable table. Also add a button "OK". whe it is clicked
program to create student report using java applet,read the input using text boxesand generate the grades..?
program to create student report using java applet,read the input using text boxesand generate the grades..?   sir plz give me java applet codes for above question
how to create classes for lift program
how to create classes for lift program  i would like to know creating classes for lift program
To write a Java program To create a frame with 4 fields;name,street,city,pin;with suitable table
To write a Java program To create a frame with 4 fields;name,street,city,pin;with suitable table  I want to write a Java program To create... to be appeared in text field Can someone write a program for the above? I have
write a program to create a user defined
write a program to create a user defined   plz send me code   Hi Friend, Please specify what do you want to create? Thanks
what's the logic for create pattern program
what's the logic for create pattern program   1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  write a program to create text area and display the various mouse handling events
Increase Web Traffic,How to Increase Web Traffic,Increase Website Traffic,Free Tips to Increase Website Traffic
How to Increase Web Traffic This tutorial will help you increase your website traffic. Today website success depends on the steady flow of the web traffic... adwords program. But if you don't have money or you don't want to spend money
java program
java program  write a program to create server and client such that server receives data from client using BuuferedReader and sends reply to client using PrintStream
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
Java Program
Java Program  I want to Write a program in JAVA to display to create a class called MATRIX using a two-dimensional array of integers. Perform the addition and subtraction of two matrices. Help me