:rolleyes:

Hi,

Could you please help?? I am tired and confused. I don't what to do any more! I am trying to write a traffic simulation and i wrote some interface and some classes but the main one that has a big chunk of code seems to me like nightmare. Certainly it is confusing and doesn't make sense. I have three lights that have green yellow and red. I wrote three classes westligh, southlight and eastlight implementing threads so that there a pause for each of them. I have to code when a car is running it has to stop for any obstruction such a car infront of it, or a red light. The light have different timing such the first one 30 second for red 5 for yellow and 45 for green. the next cross light has 50 for red 5 for yellow and 25 for green. The simulation is run base on the time given by the user. Now i though i can use the Date class. Still how to tell if it is first second do this and if it is the second second do that...... I will paste my confusing code for class Car.java. Please help me with this and tell me what could i improve and how to use the Date class and this the wright way to move a car? Thank you very much and Here is the code:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;


class Car implements CarTs{

//Checking the date.
private Date d1;

//Array will hold the cars.
ArrayList l;

//Reference to the lights
WestLight w;
EastLight e;
SouthLight s;

//Reference to the road.
Road r;

//variable for the velocity of the car, the distance from an obstruction.
double velocity = 2.4;
int d = 15;
double dmin = 1.5;
double climit = 13.9;

//Checking the lights
boolean checke1 = false;
boolean checke2 = false;
boolean checke3 = false;

boolean checkw1 = false;
boolean checkw2 = false;
boolean checkw3 = false;

boolean checks1 = false;
boolean checks2 = false;
boolean checks3 = false;

//These are coordinates: position for the cars. 
int x1 = 0;
int y1 = 0;

// coordinates for the cars: height and width.
int x2 = 5; 
int y2 =  3;

public Car(WestLight w, EastLight e, SouthLight s)
{
 d1 = new Date();
 r = new Road();
 l = new ArrayList();
 this.w = w;
 this.e = e;
 this.s = s;
}


 // When extending a swing component to be used for drawing
 // We should always call the superclass paintComponent method.
 // This will properly draw the figures the way you intended

 public void paintComponent( Graphics g ) {
   super.paintComponent(g);
             //setBackground( Color.black );
  // Setting up an iterator to the beginning of the list
    for(Iterator it = l.iterator(); it.hasNext(); )
     ((MyShape)it.next()).draw(g);    // PolyMorphism
  } // ~paintComponent

y1 = = r.gety1() + 3;

public void DrawCars1()
{
 int red   =  (int)(Math.random() * 256);
 int green =  (int)(Math.random() * 256);
 int blue  =  (int)(Math.random() * 256);
   Color color = new Color(red,green,blue);
 //Split the array so we can have cars going South, West and East.
 int a = 13;
 int a1 = (a / 4) + 1; //For cars Running from west to east
 int a2 = (a / 4);     //For cars Running from 1rst south.
 int a3 = (a / 4);  //For cars Running from 2nd south.
 int a4 = (a / 4);  //For cars Running from 3rd south.

 for(int i = 0; i < a1; i++)
 {l.add( new FillRectangle( x1,y1,x2,y2,color) ); 
  x1 = (x1 - 15);
 }
  repaint();

 //Changing the x and y according to Road direction. Cars will come from the first south direction.
  x1 = r.getx3() + 3;
  y1 = r.gety3();
  x2 = y2;
  y2 = x2;
  for(int j= 0; j < a2; j++)
  {
   l.add( new FillRectangle( x1,y1,x2,y2,color) );
   y1 = (y1 - 15);
  }
    repaint();

 //Changing the x and y according to Road direction. Cars will come from the second south direction.
  x1 = r.getx5() + 3;
  y1 = r.gety5();
  x2 = y2;
  y2 = x2;
  for(int ji= 0;ji < a3; ji++)
  {
   l.add( new FillRectangle( x1,y1,x2,y2,color) );
   y1 = (y1 - 15);
  }
    repaint();

 //Changing the x and y according to Road direction. Cars will come from the third south direction.
  x1 = r.getx7() + 3;
  y1 = r.gety7();
  x2 = y2;
  y2 = x2;
  for(int jii= 0; jii < a4; jii++)
  {
   l.add( new FillRectangle( x1,y1,x2,y2,color) );
   y1 = (y1 - 15);
  }
    repaint();

}//End of method DrawCars1


public void DrawCars2()
{
  //Checking the ligh for cars Running in all directions.
 //This light would represent cars coming from the west and cars coming from 1st south.
 checkw1  = w.getGreen();
 checkw2 = w.getRed();
 checkw3 = w.getYellow();
 //This light would represent cars coming from the west and cars coming from 2nd south.
 checks1  = s.getGreen();
 checks2 = s.getRed();
 checks3 = s.getYellow();
 //This light would represent cars coming from the west and cars coming from 3rd south.
 checke1  = e.getGreen();
 checke2 = e.getRed();
 checke3 = e.getYellow();

  Iterator it = l.iterator();         //Iterator
  int tseconds = d1.getSeconds();
  int t2seconds = tseconds;
  int checktime = 0;

//vi = climit       for x greater or equal than D.
//vi = mx + b   for Dmin < x < D then vi= (climit  / (D - Dmin))(x - Dmin)                
//vi = 0        for x less or equal Dmin

//x is the distance from vehicle i to any obstruction (such as another vehicle or a red traffic signal) in front of it.
// D is the maximum distance at which an obstruction has an influence on vehicle i.
//Dmin is the distance at which vehicle i stops for the obstruction.

  x1 = 0;
  x1 = (x1 + velocity);
   int vallightx = w.getvalx(); /////////////MUST CODE
   int vallighty = w.getvaly(); /////////////MUST CODE
   int vallight2x = s.getvalx(); ////////Must code.
   int vallight2y = s.getvaly(); ////////Must code.
   int vallight3x = e.getvalx(); ///////Must code.
   int vallight3y = e.getvaly(); ///////Must code.
   int x0 = 0; 
   int x = 0;
   int xx0 = 0;
   int xxx0 = 0;

   y1 = 0;
   y1 = (y1 + velocity);
   int vallighty = w.getvaly(); /////////////MUST CODE
   int y0 = 0; 
   int y = 0;   

//Checking the time.
 Date d2 = new Date(); int vald = d2.getMinutes();
 while(vald <= 2)
 { 
  /////////////////////////////Cars coming from the west/////////// ////////////////////
  if(checkw1 == true) 
  {  //Checking the first car.
      MyShape DL = (MyShape) l.get(0);
      int dl = DL.getx1(); 
       x0 = dl - vallightx; //distance from a traffic light.
     //if the distance of x is less than the traffic light
     if(dl < vallightx){  //starts
      if(x0 <= dmin)
      {DL.setx1(dl);} //putting the old value back.
      else
      {DL.setx1(dl + x1);} // new value no obstruction.

      if(dmin < x0 && x0 < d)
      {double vi= (climit  / (d - dmin)) * (x0 - dmin);
       DL.setx1(vi);} // new value no obstruction.
      else
      {DL.setx1(x1 + dl);} // new value approaching obstruction. 
                               repaint();} // ends

   //////when it is green and the car passed the light there is a possibility that the car would reach the next red light.
      if(dl > vallightx &&(s2==true || s3 == true) ){
       MyShape DDL = (MyShape) l.get(0);
      int ddl = DDL.getx1(); 
       xx0 = ddl - vallight2x; //distance from next traffic light.
      if(ddl < vallight2x){
      if(xx0 <= dmin)
      {DDL.setx1(ddl);} //putting the old value back.
      else
      {DDL.setx1(ddl + x1);} // new value no obstruction.

      if(dmin < xx0 && xx0 < d)
      {double vii= (climit  / (d - dmin)) * (xx0 - dmin);
       DDL.setx1(vii);} // new value no obstruction.
      else
      {DDL.setx1(x1 + ddl);} // new value no obstruction. } 
                                                          repaint();}

//Checking the rest of the cars.
   for(int i = 0; i < a1; i++)
   { 
    MyShape LD = (MyShape) l.get(i);
      int ld = LD.getx1(); 
       x = ld - vallightx; //distance from a traffic light.
      if(ld < vallightx){
      if(x <= dmin)
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int pr = prev.getx1();
      if( (pr - ld) <= 1.5) ) //Checking the distane between two cars.
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction. 

     if(dmin < x && x < d)
      {double VI = (climit  / (d - dmin)) * (x - dmin);
      LD.setx1(VI);} // new value approaching obstruction.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction. }

     //////when it is green and the car passed the light there is a possibility that the car would reach the next red light.
      if(ld > vallightx &&(s2==true || s3 == true) ){
       MyShape LLD = (MyShape) l.get(i);
      int lld = LLD.getx1(); 
       xxx0 = lld - vallight2x; //distance from next traffic light.
      if(lld < vallight2x){
      if(x <= dmin)
      {LLD.setx1(lld);} //putting the old value back.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int ppr = pprev.getx1();
      if( (ppr - lld) <= 1.5) ) //Checking the distane between two cars.
      {LLD.setx1(lld);} //putting the old value back.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction. 

     if(dmin < xxx0 && xxx0 < d)
      {double VII = (climit  / (d - dmin)) * (xxx0 - dmin);
      LLD.setx1(VII);} // new value approaching obstruction.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction. } }

     repaint();     
   } //end of the for loop.

  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { x1 = (x1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }
   repaint();
   checkw1  = w.getGreen();
   checks2 = s.getRed();
   checks3 = s.getYellow();

  ////////Now 1southcars would run until they find the red ligh//////////////


  //Checking the first car.
    {  MyShape DELL = (MyShape) l.get(0);
      int dll = DELL.gety1(); 
       y0 = dll - vallighty; //distance from a traffic light.
     //if the distance of y is less than the traffic light
     if(dll < vallighty){
      if(y0 <= dmin)
      {DELL.setx1(dll);} //putting the old value back.
      else
      {DELL.setx1(dll + y1);} // new value no obstruction.

      if(dmin < y0 && y0 < d)
      {double vi= (climit  / (d - dmin)) * (y0 - dmin);
       DELL.setx1(vi);} // new value no obstruction.
      else
      {DELL.setx1(y1 + dll);} // new value approaching obstruction. }
      repaint();

     //////when it is green and the car passed the light to the other site keep up the same speed.
      if(dll > vallighty )
       { DELL.sety1(climit); } 
      repaint();

//Checking the rest of the cars.
   for(int mi = 0; mi < a2; mi++)
   { 
    MyShape DAR = (MyShape) l.get(mi);
      int dar = DAR.gety1(); 
       y = dar - vallighty; //distance from a traffic light.
      if(y <= dmin)
      {DAR.sety1(dar);} //putting the old value back.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(mi-1);
      int pr = prev.gety1();
      if( (pr - dar) <= 1.5) ) //Checking the distane between two cars.
      {DAR.sety1(dar);} //putting the old value back.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

     if(dmin < y && y < d)
      {double VI = (climit  / (d - dmin)) * (y - Dmin);
       DAR.sety1(VI);} // new value no obstruction.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

     /////when it is green and the car passed the light to the other site keep up the same speed.
      if(dar > vallighty )
       { DAR.sety1(climit); } 
    repaint();
   }

  checkw1  = w.getGreen();
  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { y1 = (y1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }  }//End of the block.

   checkw1  = w.getGreen();
   checks2 = s.getRed();
   checks3 = s.getYellow();  


  }//End if.

   d2.getMinutes();
  ////////////////////////////////////////Cars Running from the west and from the second south///////////////
  if(checks1 == true) 
  {  //Checking the first car.
      MyShape DL = (MyShape) l.get(0);
      int dl = DL.getx1(); 
       x0 = dl - vallight3; //distance from a traffic light.
     //if the distance of x is less than the traffic light
     if(dl < vallight3){  //starts
      if(x0 <= dmin)
      {DL.setx1(dl);} //putting the old value back.
      else
      {DL.setx1(dl + x1);} // new value no obstruction.

      if(dmin < x0 && x0 < d)
      {double vi= (climit  / (d - dmin)) * (x0 - dmin);
       DL.setx1(vi);} // new value no obstruction.
      else
      {DL.setx1(x1 + dl);} // new value approaching obstruction. 
                               repaint();} // ends

   //////when it is green and the car passed the light there is a possibility that the car would reach the next red light.
      if(dl > vallight &&(s2==true || s3 == true) ){
       MyShape DDL = (MyShape) l.get(0);
      int ddl = DDL.getx1(); 
       xx0 = ddl - vallight4; //distance from next traffic light.
      if(ddl < vallight4){
      if(xx0 <= dmin)
      {DDL.setx1(ddl);} //putting the old value back.
      else
      {DDL.setx1(ddl + x1);} // new value no obstruction.

      if(dmin < xx0 && xx0 < d)
      {double vii= (climit  / (d - dmin)) * (xx0 - dmin);
       DDL.setx1(vii);} // new value no obstruction.
      else
      {DDL.setx1(x1 + ddl);} // new value no obstruction. } 
                                                          repaint();}

//Checking the rest of the cars.
   for(int i = 0; i < a3; i++)
   { 
    MyShape LD = (MyShape) l.get(i);
      int ld = LD.getx1(); 
       x = ld - vallight3; //distance from a traffic light.
      if(ld < vallight3){
      if(x <= dmin)
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int pr = prev.getx1();
      if( (pr - ld) <= 1.5) ) //Checking the distane between two cars.
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction. 

     if(dmin < x && x < d)
      {double VI = (climit  / (d - dmin)) * (x - dmin);
      LD.setx1(VI);} // new value approaching obstruction.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction. }

     //////when it is green and the car passed the light there is a possibility that the car would reach the next red light.
      if(ld > vallight &&(s2==true || s3 == true) ){
       MyShape LLD = (MyShape) l.get(i);
      int lld = LLD.getx1(); 
       xxx0 = lld - vallight4; //distance from a traffic light.
      if(lld < vallight4){
      if(x <= dmin)
      {LLD.setx1(lld);} //putting the old value back.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int ppr = pprev.getx1();
      if( (ppr - lld) <= 1.5) ) //Checking the distane between two cars.
      {LLD.setx1(lld);} //putting the old value back.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction. 

     if(dmin < xxx0 && xxx0 < d)
      {double VII = (climit  / (d - dmin)) * (xxx0 - dmin);
      LLD.setx1(VII);} // new value approaching obstruction.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction. } }

     repaint();     
   } //end of the for loop.

  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { x1 = (x1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }
   repaint();
   checkw1  = w.getGreen();
   checks2 = s.getRed();
   checks3 = s.getYellow();

  ////////Now 2southcars would run until they find the red ligh//////////////


  //Checking the first car.
    {  MyShape DELL = (MyShape) l.get(0);
      int dll = DELL.gety1(); 
       y0 = dll - vallight3; //distance from a traffic light.
     //if the distance of y is less than the traffic light
     if(dll < vallight3){
      if(y0 <= dmin)
      {DELL.setx1(dll);} //putting the old value back.
      else
      {DELL.setx1(dll + y1);} // new value no obstruction.

      if(dmin < y0 && y0 < d)
      {double vi= (climit  / (d - dmin)) * (y0 - dmin);
       DELL.setx1(vi);} // new value no obstruction.
      else
      {DELL.setx1(y1 + dll);} // new value approaching obstruction. }
      repaint();

     //////when it is green and the car passed the light to the other site keep up the same speed.
      if(dll > vallight3 )
       { DELL.sety1(climit); } 
      repaint();

//Checking the rest of the cars.
   for(int mi = 0; mi < a3; mi++)
   { 
    MyShape DAR = (MyShape) l.get(mi);
      int dar = DAR.gety1(); 
       y = dar - vallight3; //distance from a traffic light.
      if(y <= dmin)
      {DAR.sety1(dar);} //putting the old value back.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(mi-1);
      int pr = prev.gety1();
      if( (pr - dar) <= 1.5) ) //Checking the distane between two cars.
      {DAR.sety1(dar);} //putting the old value back.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

     if(dmin < y && y < d)
      {double VI = (climit  / (d - dmin)) * (y - Dmin);
       DAR.sety1(VI);} // new value no obstruction.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

     /////when it is green and the car passed the light to the other site keep up the same speed.
      if(dar > vallight3 )
       { DAR.sety1(climit); } 
    repaint();
   }

  checkw1  = w.getGreen();
  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { y1 = (y1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }  }//End of the block.

   checkw1  = w.getGreen();
   checks2 = s.getRed();
   checks3 = s.getYellow();  


  }//End if.

////////////////////////////////////////cars approaching the east side and others coming from the thirsSouth. 
  if(checke1 == true) 
  {  //Checking the first car.
      MyShape DL = (MyShape) l.get(0);
      int dl = DL.getx1(); 
       x0 = dl - vallight; //distance from a traffic light.
     //if the distance of x is less than the traffic light
     if(dl < vallight){  //starts
      if(x0 <= dmin)
      {DL.setx1(dl);} //putting the old value back.
      else
      {DL.setx1(dl + x1);} // new value no obstruction.

      if(dmin < x0 && x0 < d)
      {double vi= (climit  / (d - dmin)) * (x0 - dmin);
       DL.setx1(vi);} // new value no obstruction.
      else
      {DL.setx1(x1 + dl);} // new value approaching obstruction. 
                               repaint();} // ends

   //////when it is green and the car passed the light there is a possibility that the car would reach the next red light.
      if(dl > vallight &&(s2==true || s3 == true) ){
       MyShape DDL = (MyShape) l.get(0);
      int ddl = DDL.getx1(); 
       xx0 = ddl - vallight2; //distance from next traffic light.
      if(ddl < vallight2){
      if(xx0 <= dmin)
      {DDL.setx1(ddl);} //putting the old value back.
      else
      {DDL.setx1(ddl + x1);} // new value no obstruction.

      if(dmin < xx0 && xx0 < d)
      {double vii= (climit  / (d - dmin)) * (xx0 - dmin);
       DDL.setx1(vii);} // new value no obstruction.
      else
      {DDL.setx1(x1 + ddl);} // new value no obstruction. } 
                                                          repaint();}

//Checking the rest of the cars.
   for(int i = 0; i < a4; i++)
   { 
    MyShape LD = (MyShape) l.get(i);
      int ld = LD.getx1(); 
       x = ld - vallight; //distance from a traffic light.
      if(ld < vallight){
      if(x <= dmin)
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int pr = prev.getx1();
      if( (pr - ld) <= 1.5) ) //Checking the distane between two cars.
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction. 

     if(dmin < x && x < d)
      {double VI = (climit  / (d - dmin)) * (x - dmin);
      LD.setx1(VI);} // new value approaching obstruction.
      else
      {LD.setx1(x1 + ld);} // new value no obstruction. }

     //////when it is green and the car passed the light there is a possibility that the car would reach the next red light.
      if(ld > vallight &&(s2==true || s3 == true) ){
       MyShape LLD = (MyShape) l.get(i);
      int lld = LLD.getx1(); 
       xxx0 = lld - vallight2; //distance from a traffic light.
      if(lld < vallight2){
      if(x <= dmin)
      {LLD.setx1(lld);} //putting the old value back.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int ppr = pprev.getx1();
      if( (ppr - lld) <= 1.5) ) //Checking the distane between two cars.
      {LLD.setx1(lld);} //putting the old value back.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction. 

     if(dmin < xxx0 && xxx0 < d)
      {double VII = (climit  / (d - dmin)) * (xxx0 - dmin);
      LLD.setx1(VII);} // new value approaching obstruction.
      else
      {LLD.setx1(x1 + lld);} // new value no obstruction. } }

     repaint();     
   } //end of the for loop.

  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { x1 = (x1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }
   repaint();
   checkw1  = w.getGreen();
   checks2 = s.getRed();
   checks3 = s.getYellow();

  ////////Now 3southcars would run until they find the red ligh//////////////


  //Checking the first car.
    {  MyShape DELL = (MyShape) l.get(0);
      int dll = DELL.gety1(); 
       y0 = dll - vallight; //distance from a traffic light.
     //if the distance of y is less than the traffic light
     if(dll < vallight){
      if(y0 <= dmin)
      {DELL.setx1(dll);} //putting the old value back.
      else
      {DELL.setx1(dll + y1);} // new value no obstruction.

      if(dmin < y0 && y0 < d)
      {double vi= (climit  / (d - dmin)) * (y0 - dmin);
       DELL.setx1(vi);} // new value no obstruction.
      else
      {DELL.setx1(y1 + dll);} // new value approaching obstruction. }
      repaint();

     //////when it is green and the car passed the light to the other site keep up the same speed.
      if(dll > vallight )
       { DELL.sety1(climit); } 
      repaint();

//Checking the rest of the cars.
   for(int mi = 0; mi < a4; mi++)
   { 
    MyShape DAR = (MyShape) l.get(mi);
      int dar = DAR.gety1(); 
       y = dar - vallight; //distance from a traffic light.
      if(y <= dmin)
      {DAR.sety1(dar);} //putting the old value back.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(mi-1);
      int pr = prev.gety1();
      if( (pr - dar) <= 1.5) ) //Checking the distane between two cars.
      {DAR.sety1(dar);} //putting the old value back.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

     if(dmin < y && y < d)
      {double VI = (climit  / (d - dmin)) * (y - Dmin);
       DAR.sety1(VI);} // new value no obstruction.
      else
      {DAR.sety1(y1 + dar);} // new value no obstruction.

     /////when it is green and the car passed the light to the other site keep up the same speed.
      if(dar > vallight )
       { DAR.sety1(climit); } 
    repaint();
   }

  checkw1  = w.getGreen();
  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { y1 = (y1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }  }//End of the block.

   checkw1  = w.getGreen();
   checks2 = s.getRed();
   checks3 = s.getYellow();  


  }//End if.

 }//End of While Loop.

x1 = (x1 + velocity);
   int vallight = w.getval(); /////////////MUST CODE
   int x0 = 0; 
   int x = 0;
//Light is green and the cars will start increasing their velocity.
 while(checke1 == true)
 { 
  if(westcars == true) 
  {  //Checking the first car.
   MyShape DL = (MyShape) l.get(0);
      int dl = DL.getx1(); 
       x0 = dl - vallight; //distance from a traffic light.
      if(x0 <= dmin)
      {DL.setx1(dl);} //putting the old value back.
      else
      {DL.setx1(x1);} // new value no obstruction.

      if(dmin < x && x < d)
      {double vi= (climit  / (d - dmin)) * (x - Dmin);
       DL.setx1(vi);} // new value no obstruction.
      else
      {DL.setx1(x1);} // new value no obstruction.

//Checking the rest of the cars.
   for(int i = 1; i < a1; i++)
   { 
    MyShape LD = (MyShape) l.get(i);
      int ld = LD.getx1(); 
       x = ld - vallight; //distance from a traffic light.
      if(x <= dmin)
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1);} // new value no obstruction.

      MyShape prev = (MyShape) l.get(i-1);
      int pr = prev.getx1();
      if( (pr - ld) <= 1.5) ) //Checking the distane between two cars.
      {LD.setx1(ld);} //putting the old value back.
      else
      {LD.setx1(x1);} // new value no obstruction.

     if(dmin < x && x < d)
      {double VI = (climit  / (d - dmin)) * (x - Dmin);
      LD.setx1(VI);} // new value no obstruction.
      else
      {LD.setx1(x1);} // new value no obstruction.
   }

   repaint();
  checke1  = e.getGreen();
  int tseconds = d1.getSeconds();

  if(t2seconds < tseconds && checktime < 5)
   { x1 = (x1 + (velocity * 2) );
    checktime++;}

   else
   { x1 = climit;  }

  }//End if.

 }//End of While Loop.


  //Different story for a yellow light or a red light.

  int x = x1; //Setting the car to stay in the same position.

  int v = 0;
  int val = 0;
  int val2 = 0;
 Iterator IT = l.iterator();
 while(checkw2 == true || checkw3 == true)
 {  velocity = 2.4; //Initializing the velocity back to origninal value;

   if(westcars == true)
   { westcars = false; }
   else if (southcars1 == false)
   { southcars1 = true};
   else
   { westcars = true; }

  MyShape temp = (MyShape) l.get(0);

  v = temp.getx1();
  if( () - v == 1.5 )
  shape.setx1(x); 

  //Other cars need to check for two obstructions: The light and the car in front of it.  

  for(int n = 1; n < a1; n++)
  {
    MyShape shape = (MyShape) l.get(n);
     val = shape.getx1();
    MyShape sh = (MyShape) l.get(n-1);
     val2 = sh.getx1();

    //Vi = 0 for x less or equal to Dmin. 

    if( (() - (val) == 1.5 ) || (val2 - val == 1.5 ) ) 
    shape.setx1(x); 

   }

   repaint();

   checkw2 = w.getRed();
   checkw3 = w.getYellow();
   checke2 = w.getRed();
   checke3 = w.getYellow();

  }//END OF WHILE LOOP.

//Checking the ligh for the second half of Running cars from the South.



Iterator iter = l.iterator();

 while(checks1 == true)
 {    
    y1 = (y1 + velocity);

   for(int b = a2; b < a && iter.hasNext(); b++)
   { ((MyShape)it.next()).sety1(y1); 

   }

   checks1  = s.getGreen();

   repaint();

 }//End of While Loop.




int y = y1; //Setting the car coming from the south to stay in the same position.

int V= 0;
  int VAL = 0;
  int VAL2 = 0;

 while(checks2 == true || checks3 == true)
 {
  //Since this is  the first car Running no need to check for more.
 //First car have only one obstruction the light.

  MyShape temp = (MyShape) l.get(a2);

  V = temp.gety1();
  if( () - V == 5 )
  shape.sety1(y); 

  //Other cars need to check for two obstructions: The light and the car in front of it.  

  for(int K = a2; k < a; K++)
  {
    MyShape shape = (MyShape) l.get(K);
     VAL = shape.gety1();
    MyShape sh = (MyShape) l.get(K-1);
     VAL2 = sh.gety1();

    if( (() - (VAL)  == 5 ) || (VAL2 - VAL == 5 ) ) 
    shape.sety1(y); 

   }
   repaint();

  checks2 = s.getRed();
 checks3 = s.getYellow();

  }//END OF WHILE LOOP.



} //End of method DrawCars2.

}// End of the class.

Recommended Answers

All 12 Replies

Dounia
We have the same problem in a different situation... i would like to create a traffic simulation about road traffic for my simulation subject and my problem is i'm just a newbie in java i didnt know what im gonna do right now, it's obvious that i cannot make it own my own so i gonna beg you that please can i have your program for the salvation of my simulation course if not then its the end. I will present the simulation project this february 27, 2008 and basically i need your help before the schedule date. my email address is lordonin@gmail.com advance thanks if lean your hand for helping me for my project.

:rolleyes:

Hi,

Could you please help?? I am tired and confused. I don't what to do any more! I am trying to write a traffic simulation and i wrote some interface and some classes but the main one that has a big chunk of code seems to me like nightmare. Certainly it is confusing and doesn't make sense. I have three lights that have green yellow and red. I wrote three classes westligh, southlight and eastlight implementing threads so that there a pause for each of them. I have to code when a car is running it has to stop for any obstruction such a car infront of it, or a red light. The light have different timing such the first one 30 second for red 5 for yellow and 45 for green. the next cross light has 50 for red 5 for yellow and 25 for green. The simulation is run base on the time given by the user. Now i though i can use the Date class. Still how to tell if it is first second do this and if it is the second second do that...... I will paste my confusing code for class Car.java. Please help me with this and tell me what could i improve and how to use the Date class and this the wright way to move a car? Thank you very much and Here is the code:

That's a massive amount of code for people to go through. I think you are going to have to help us out by being more specific regarding what you've tried, what works, what doesn't, does it run, does it crash, under what conditions, etc., and try to point us to a certain part of the code. Also, and particularly in a program as long as this, code tags are a must:

[code=JAVA] // paste your code here

[/code]
Otherwise all the indentation goes away. Adding the "=JAVA" is useful on a long program like this since it adds line numbers people can refer to. Here's a code beautifier link that makes things indent nicely too if it doesn't already:
http://www.prettyprinter.de/

VernonDozier, that post was from 2004. Lordonin just drug it out of the dust bin to beg for code.

Ah, missed that!:$

ezzaral, do you have any traffic simulation program?i really need it.

commented: Read the rules -1

im desperate now, i could not make it own my own because im just newbie.i dont have deep experience with java.

can someone help me with my huge problem.

No one will give you a program no matter how desperate you are. You actually need to do the work, not us. Read the rules.

in fact, the more desperate you are the less likely we are to help you as the more we want you to fail and prevent you from polluting the job market with your non-existent skills, causing all real programmers to look bad and get more work trying to clean up after you.

hi dounia...

can you help me in my project??? im a newbie here.. my problem is also like this... i dont know what to do... we should pass it on october 16,2008.... :( how should i start?? i have 2 only 2 days left...

hi dounia...

can you help me in my project??? im a newbie here.. my problem is also like this... i dont know what to do... we should pass it on this thursday....:( how should i start?? i have 2 only 2 days left...

hi dounia...

can you help me in my project??? im a newbie here.. my problem is also like this... i dont know what to do... we should pass it on this thursday....:( how should i start?? i have 2 only 2 days left...

well ...

maybe you didn't see this comming, but you kind of shouldl... this original thread is ancient, it was dug up for the same reason as you are doing now, begging for code. just read the remarks that were left as comment to him.

what to do is install java, making an analysis of the what how when who questions, start coding it, and, if by any chance, you come across a problem you can't fix yourselve, start a new thread (yup, new) with the relevant code and error messages.

that way we can help you. I doubt this assignment was given today, so you have only yourself to thank for the fact that just little time remains. but always remember, we will not hand over the complete code to you, especially if you don't show any effort yourself.. this is because:
1. you wouldn't learn a thing
2. we don't have the time, we have projects of our own to work on

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.