Java汽车动画平滑过弯

3
我正在尝试让一张汽车图片沿着一条路径移动。汽车必须以恒定的速度“行驶”,并且看起来平滑。我可以让汽车沿着点列表移动,但我不知道如何使汽车以恒定速度平滑移动(现在每2秒钟汽车移动到列表中的下一个点),以及如何进行硬编码转向。有谁能帮忙吗?
代码
Track类,在其中加载轨道底图。
public class Track{

BufferedImage track;
Point trackPosition;
static final Point TRACK_POS = new Point(0, 0);
static final Point SENSOR_POS = new Point(250, 70);

public Track(){
    try {
        track = ImageIO.read(Roundabout.class.getResource("track.png"));
    } catch (Exception ex) {
        System.out.println("Problem loading track image: " + ex);
    }
    trackPosition=new Point(TRACK_POS.x,TRACK_POS.y);
}

  public void paint (Graphics g)
{
    g.drawImage(track,TRACK_POS.x, TRACK_POS.y, null);
}
}

汽车类
public class Car extends JComponent implements Vehicle{

 BufferedImage car;
 Point carPosition;
 static final Point START_POS = new Point(10, 150);

 int counter=0;


public Car(){
    try {
        car = ImageIO.read(Car.class.getResource("beetle_red.gif"));
    } catch (Exception e) {
        System.out.println("Problem loading car images: " + e);
    }

    carPosition = new Point(START_POS.x, START_POS.y);

}

public void paint (Graphics g)
{

  g.drawImage(car,carPosition.x, carPosition.y, null); //original paint


}

  public Point getCarPosition() {
    return new Point(carPosition.x,carPosition.y);
}

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();    
 }
}

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)));

}

环形路口(主类)

public class Roundabout extends JFrame{

Track track=new Track();
TrafficLight trafficLight=new TrafficLight();
Car car=new Car();
ArrayList<Car> cars = new ArrayList<>();


byte[] array=new byte[]{0,2,1,1}; //test byte array


class Surface extends JPanel {

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.blue);

    Dimension size = getSize();
    Insets insets = getInsets();

    int w = size.width - insets.left - insets.right;
    int h = size.height - insets.top - insets.bottom;

    /* Draw the track first */
    track.paint(g);

    /* Draw a car */
    car.paint(g);
    cars.add(car); //add to list

    trafficLight.paint(g);

}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}
}

public Roundabout(){
    initUI();
}

private void initUI() {

    setTitle("Roundabout");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(new Surface());

    setSize(580, 550);
    setLocationRelativeTo(null);
    moveCar();


}


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            Roundabout roundabout=new Roundabout();
            roundabout.setVisible(true);
        }
    });

}

public void moveCar() {
    Runnable helloRunnable = new Runnable() {
        public void run() {

           car.update();
           repaint();


        }
    };

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleAtFixedRate(helloRunnable, 0, 2, TimeUnit.SECONDS);
}
}

JFramepaintComponent(...)方法吗?你如何在Roundabout类中覆盖它!!!对于JComponent,重写此方法而不是paint(...) - nIcE cOw
我该如何修复这个问题,你能帮我回答我的原始帖子中的问题吗? - Sybren
请给我一些时间,正在创建一个示例(即将推出...)。尝试使用 JLabel 而不是直接绘制。 - nIcE cOw
抱歉,我看错了问题,回答了你的问题。我的错 :( - nIcE cOw
1个回答

0

基本上,您需要沿着一条线段移动您的汽车,然后是另一条线段,以此类推...

这里有一个关于沿着直线运动的解释

相关代码,其中t在0到1的范围内(实际上t =所述时间内穿过的线的百分比):

x = xstart + (xend-xstart) * t y = ystart + (yend-ystart) * t

这都是数学。

为了确定每个线段需要巡航的速度(作为t的变化),您需要确定其长度。

数学很简单:

如果对于10px长的线,t +=(1/10)每秒1次,则其速度为每秒1像素。对于339像素长的线,您需要339秒和t + =(1/339)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接