由于我对编程还很陌生,因此我正在为实践进行简单的交通模拟。到目前为止,我已经拥有了所有想做的工作,但是当一辆汽车停在红灯处时,它后面的汽车就撞上了它,并彼此叠放。
这是我给汽车打电话的通用代码:
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
}
)
这基本上是我的CarsRight.java
课:
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
}
}
}
再一次,我不知道如何解决这个问题:/
答案 0 :(得分:1)
我查看了您的代码,并能够解决您的问题。您需要汽车之间的相对距离,否则它们会像现在一样堆积。
我对<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="card-body">
<div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ss11" aria-expanded="false" aria-controls="ss11">
ss1 : science
</button>
</div>
<div class="collapse" id="ss11">
<div class="card card-body">
<div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ss12013_20141" aria-expanded="false" aria-controls="ss12013_20141">
2013/20141
</button>
</div>
<div class="collapse" id="ss12013_20141">
<p>hello lindsay lowhay</p>
</div>
<div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ss12014-20151" aria-expanded="false" aria-controls="ss12014-20151">
2014/20151
</button>
</div>
<div class="collapse" id="ss12014-20151">
<p>hello lindsay lowhay</p>
</div>
</div>
</div>
<div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ss12" aria-expanded="false" aria-controls="ss12">
ss1 : art
</button>
</div>
<div class="collapse" id="ss12">
<div class="card card-body">
<div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ss12013-20142" aria-expanded="false" aria-controls="ss12013-20142">
2013/20142
</button>
</div>
<div class="collapse" id="ss12013-20142">
<p>hello lindsay lowhay</p>
</div>
<div>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ss12014_20152" aria-expanded="false" aria-controls="ss12014_20152">
2014/20152
</button>
</div>
<div class="collapse" id="ss12014_20152">
<p>hello lindsay lowhay</p>
</div>
</div>
</div>
</div>
流程进行了更改。您可以对其他汽车进行类似的更改。
CarsRight
中的类MoveForLoopRight
:
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
让我知道进展如何。谢谢!