Java动画汽车沿点列表

时间:2015-03-09 15:29:10

标签: 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);
}

Lane class:

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 个答案:

没有答案