我想用一个“汽车”演员和一个修复生成位置创建一个 Gridworld:
package gridworld.blatt3;
import gridworld.framework.actor.*;
import gridworld.framework.grid.Grid;
import gridworld.framework.grid.Location;
public class Traffic {
public static void main(String[] args) {
TrafficWorld world = new TrafficWorld();
System.out.println("Anwendung startet...");
Location loc1 = new Location(0,0);
Grid.put(loc1, new Car());
world.show();
}
}
package gridworld.framework.actor;
import gridworld.framework.actor.bugs.Flower;
import gridworld.framework.grid.Grid;
import gridworld.framework.grid.Location;
import java.awt.*;
public class Car extends AbstractActor {
//Schrittgröße//
int speed=5;
int sideLenght=50;
int step = 0;
//Richtung //
public Car () {
setDirection(Location.EAST);
setColor(Color.BLUE);
}
public void act () {
move();
}
public void move() {
int speedCount=0;
//Springt immer um 'speed'-Schritte //
do {
step++;
speedCount++;
if (step < sideLenght) {
System.out.println("Step: "+step);
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
moveTo(next);
}
else {
System.out.println("car drives off the grid");
removeSelfFromGrid();
break;
}
} while (speedCount!=speed);
}
}
package gridworld.framework.actor;
import gridworld.framework.grid.BoundedGrid;
public class TrafficWorld extends ActorWorld {
private static final int rowSize = 1;
private static final int colSize = 50;
public TrafficWorld() {
super(new BoundedGrid<Actor>(rowSize, colSize));
}
}
如何为“汽车”定义特定的生成位置?例如,我希望 Actors 在网格的左上角生成,我创建了一个 Location 对象“loc1”以与 Grid.put() 一起使用,但出现错误:java: non-static method put(gridworld.framework .grid.Location,E) 不能从静态上下文中引用
我正在使用 openjdk-15.0.1