JavaFx动画遵循动态路径

时间:2016-03-24 22:34:39

标签: java javafx

我有一个ArrayList,其中每个粒子的x和y位置随时间变化。

我想使用JavaFx为它的轨迹制作动画。我已经尝试过使用Timeline等等,如果我为粒子指定了开始和结束位置,它似乎工作正常。但是,我希望粒子遵循随时间变化的路径。我想解决方案可能是制作一个KeyValues数组,它首先通过运行粒子的轨迹来填充。然后时间轴为这些KeyValues设置动画。这会有用吗?还有,有一个更清洁的解决方案 - 我敢肯定有,但找不到任何东西!我很感激任何指导,我是Java新手:)

// returns time elapsed since start of program  
public double getTime(){
    return (System.currentTimeMillis() - startTime) / 1000.0;
}   

public startButton(){

    // defining origin
    DoubleProperty x  = new SimpleDoubleProperty(250);
    DoubleProperty y  = new SimpleDoubleProperty(150);

    // generate array of KeyValue for ONE particle in fireworks 
    do { 
    fireworks = manager.getFireworks(getTime());    // fireworks is an ArrayList<Particle>, getFireworks is updated based on time input.
    double[] pos = fireworks.get(0).getPosition();  // gets the first Particle's position

    // generate KeyValue Array (is this possible?) containing 
    // KeyValue(x, pos[0]), KeyValue(x, pos[0 + dt].... etc...
    // basically a trajectory for the particle while it's alive

    } while (fireworks.get(0).isAlive(getTime()));

    Timeline timeline = new Timeline();
    KeyFrame kf = new KeyFrame(Duration.millis(10),           
                      //...insert KeyValue array here....//
    // add KeyFrame to timeline
    timeline.setCycleCount(1);

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            GraphicsContext gc = canvas.getGraphicsContext2D();
            drawLaunchTube();
            gc.setFill(Color.FORESTGREEN);
            gc.fillOval(x.doubleValue(), y.doubleValue(), 10, 10);
        }
    };
    timer.start();
    timeline.play();
}

0 个答案:

没有答案