Search code examples
javaooparraylistgraphics2d

How can I stop these cars from hitting each other?


I'm working on a simple traffic simulation for practice, as I'm fairly new to programming. I've gotten all I want to work so far, but when a car stops at a red light, the cars behind it just run into it and stack on top of each other.

Here is the general code where I call the cars:

private ArrayList<CarsRight> carsright = new ArrayList<CarsRight>();

public IntersectionSimulation() {
    for (int i = 0; i < 4; i++) {
        carsright.add(new CarsRight(i * -250));
    }
}

public void paint(Graphics g) {
    for (CarsRight cr : carsright) {
        Graphics2D g2d = (Graphics2D) g;
        cr.paint(g2d);
        tl.paint(g2d, mkl.b);   //tl is the traffic light (either green or red)
                                //mkl.b checks whether or not the spacebar is being pressed and changes the light accordingly
    }
}

public void move() {
    for (CarsRight cr : carsright) {
        cr.move(mkl.b);     //mkl.b checks whether or not the spacebar is being pressed and makes the cars move or stop accordingly
    }
)

And here is basically my CarsRight.java class:

public class CarsRight extends JPanel {
    int x;
    private int y = 330;
    private int a;
    private int speed = 20;
    int rgb = colorCode();
    private int height = 40;
    private int length = 100;

    public CarsRight(int i) {
        this.x = i;
    }

    void move(boolean b) {
        if (b == true) {
            x++;
            a = speed;
            x = x + a;
        }   //move if the light is green
        else {
            if (x > 300) {
                x++;
                a = speed;
                x = x + a;
            }
            else if (x < 250) {
                x++;
                a = speed;
                x = x + a;
            }
        }   //if the car is before the light, move up to the light, and if the car is past the light, keep going
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paint(g2d);

        for (int d = 0; d < 20; d++) {
            if (rgb == 1) {
                Color c = new Color(255, 255, 255);
                g2d.setColor(c);
            } else if (rgb == 2) {
                Color c = new Color(0, 0, 0);
                g2d.setColor(c);
            } else if (rgb == 3) {
                Color c = new Color(179, 179, 179);
                g2d.setColor(c);
            } else if (rgb == 4) {
                Color c = new Color(187, 10, 48);
                g2d.setColor(c);
            } else {
                Color c = new Color(182, 177, 169);
                g2d.setColor(c);
            }       //random color generator for car

            g2d.fillRoundRect(x, y, length, height, 10, 20);    //prints car
        }
    }
}

Once again, I have no clue how to approach this :/


Solution

  • I looked at your code and was able to solve your problem. You need relative distance between cars without which they would stack-up as it is right now.

    I made changes to CarsRight flow. You can do similar changes for other cars.

    MoveForLoopRight class in IntersectionSimulation:

    public class MoveForLoopRight extends IntersectionSimulation {
            public void run() {
                // It is important to know the relative distance between each car. If not, your cars would stack on each other.
                int distance = 0;
                for (int i = 0; i< carsright.size(); i++) {
                    CarsRight car = carsright.get(i);
                    if (car.getX() <= 300) { // If they are before signal, move them till the line maintaining the distance
                        car.move(mkl.b, distance); // move method needs distance between cars
                        distance = distance + car.getLength() + 50; // 50 is the gap between each car
                    } else {
                        car.move(mkl.b,  0); // these cars crossed the signal. They can move freely and don't need any distance
                    }
                }
            }
        }
    

    CarsRight.java changes:

    @Override
    public int getX() {
        return x;
    }
    
    public int getLength() {
        return length;
    }
    
    void move(boolean b, int distance) {
        if (b == true) {
            x++;
            a = speed;
            x = x + a;
        }
        else {
            if (x > 300) {
                x++;
                a = speed;
                x = x + a;
            }
            else if (x < (250 - distance)) {
                x++;
                a = speed;
                x = x + a;
            }
        }
    }
    

    Let me know how it went. Thanks!