Java沿点列表设置汽车动画
我试着让汽车图像跟随一系列的点。在当前情况下,汽车每两秒钟“跳”到列表中的下一个点。我怎样才能沿着列表中的点平稳、匀速地移动汽车?还有如何让汽车转弯?我正在考虑对回合进行硬编码,但我不知道该怎么做,以及如何将其添加到更新方法中
汽车类中的更新方法
public void update(){
repaint();
if(counter < Lane.firstLane.size()){
carPosition.x = Lane.firstLane.get(counter).x;
carPosition.y= Lane.firstLane.get(counter).y;
System.out.println("Pos: "+getCarPosition());
counter++;
}
else{
System.out.println("Destination reached");
}
repaint();
}
移动汽车的线程:
public void moveCar() {
Runnable helloRunnable = new Runnable() {
public void run() {
car.update();
repaint();
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 2000, TimeUnit.MILLISECONDS);
}
车道等级:
public class Lane {
public static List<Point> firstLane = new ArrayList<>(Arrays.asList(new Point(10,135),new Point(124,190),new Point(363,190),new Point(469,210)));
}
共 (0) 个答案