Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Traffic Light System Probelm

Hi. my name is Kamal i'm Very much New to Java Programaing i love to learn Java because its very interesting. and i have a Small Problem we have given a Assinment about desinging a Traffic Lights System. my problem is that i dont know how to move the Traffic Lights Acording to the Movement of Cars.
Here is my programe so far i have writen if you could compile it and see it would be better. but i need a Solution soon for this please. from any one.. on how to move the Lights acording to the movemenet of the cars..

thnaks
kamal


/*
// header - edit "Data/yourJavaAppletHeader" to customize
// contents - edit "EventHandlers/Java Applet/onCreate" to customize
*/
import java.awt.*;
import java.applet.*;

public class TrafficLights extends Applet
{
int x = 140;
int y = 150;
int x2 = 350;
int y2 = 140;
int yx = 5;
int oy = 10;
public void init()
{

}

public void paint(Graphics g)
{
//Draw road
g.fillRect(0,0,500,500);
g.setColor(Color.white);
g.fillRoundRect(0,0,150,150,5,5);
g.fillRoundRect(350,0,150,150,5,5);
g.fillRoundRect(0,350,150,150,5,5);
g.fillRoundRect(350,350,150,150,5,5);

//Function Prototype//

loop(g); //Dot lines loop function

DrawCars(g); //Cars Function

TraficLightsH(g); //LightsFunc1

TrafficLightsV(g); //LightsFunc2


ColorTraffic(g); //ColorFunc

Movement(g); //AnimationFunc



}

public void loop(Graphics g)

{

//Draw dot lines Function

for(int i = 0;i < 10;i++)
{
if((i%3)==1)
{
//draw black line
g.setColor(Color.black);
g.drawLine(x,y,x,y+=5);
g.drawLine(x2,y2,x2-=5,y2);

}
else
{

//draw white line
g.setColor(Color.white);
g.drawLine(x,y,x,y+=5);
g.drawLine(x2,y2,x2-=5,y2);
}

}
}
public void DrawCars(Graphics g)
{

g.setColor(Color.blue);
g.fillRect(5,155,15,15);//Vertical Car

g.setColor(Color.pink);
g.fillRect(330,10,15,15);//Horizontal Car

}


public void TraficLightsH(Graphics g)
{
//Draw Horizontal Traffic Lights

g.setColor(Color.gray);
g.fillRect(355,75,20,65);

g.setColor(Color.white);
g.fillOval(360,80,10,10);

g.fillOval(360,100,10,10);
g.fillOval(360,120,10,10);

}

public void TrafficLightsV(Graphics g)

{
//Draw Vertical TrafficLights

g.setColor(Color.gray);
g.fillRect(355,360,20,65);

g.setColor(Color.white);
g.fillOval(360,365,10,10);

g.fillOval(360,385,10,10);
g.fillOval(360,405,10,10);

}


public void ColorTraffic(Graphics g)
{
g.setColor(Color.green);//Horizontal
g.fillOval(360,120,10,10);

g.setColor(Color.red);//Vertical
g.fillOval(360,365,10,10);

}


public void Movement(Graphics g)
{
for(int i = 0;i<=480;i++)
{
//moving yellow car
g.setColor(Color.black);
g.fillRect(yx,155,15,15);

g.setColor(Color.blue);
g.fillRect(yx+=1,155,15,15);

//moving orange car
g.setColor(Color.black);
g.fillRect(330,oy,15,15);

g.setColor(Color.pink);
g.fillRect(330,oy+=1,15,15);

DelayLoop(g);//Delay loop Function


}


}
public void DelayLoop(Graphics g)
{
for(int j=0;j<=10000000;j++);
}













}
[3980 byte] By [kasen200] at [2007-11-11 7:53:53]
# 1 Re: Traffic Light System Probelm
my problem is that i dont know how to move the Traffic Lights Acording to the Movement of Cars.

well, I dont understand in detail, what your assignment is. what are the conditions of moving cars and regulating the lights?
perhaps you should post the assignment to get a better understanding what kind of simulation you have to do.
usually good traffic systems base on multiagent simulations, where every car is an independent object acting on his environment while the lights would just have fixed intervalls on which they switch between stop and go. but this would be a quiet complex application.
graviton at 2007-11-11 22:37:00 >