Traffic Light System Probelm


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Traffic Light System Probelm

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    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++);
    }













    }

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    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.

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. System Tray Icon Quality Issue
    By Joe Lachoff in forum Architecture and Design
    Replies: 1
    Last Post: 12-15-2000, 04:32 PM
  3. API For shutting, starting win NT,9x OSes
    By Saiful in forum VB Classic
    Replies: 6
    Last Post: 10-15-2000, 03:18 PM
  4. Replies: 0
    Last Post: 08-01-2000, 07:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center