2016-02-19 33 views
0

如何使用包含在其中一个构造函数参数中的数学模式中的变量“t”。 “t”对应于使图表向右移动的时间。KeyFrame - 动画

public void update(final Scene scene) { 

    final Group root = (Group) scene.getRoot(); 
    final Chart chart = new Chart(x -> Math.exp(-(Math.pow((x-t), 2))) 
            * Math.cos((2*Math.PI*(x-t))/l), 
            -1, 1, 0.01, 
            new Axes(1000, 1000, -1, 1, 0.1, -1, 1, 0.1) 
        ); 

    root.getChildren().add(chart); 
    Timeline timeLine = new Timeline(Timeline.INDEFINITE, 
            new KeyFrame(new Duration(1000), 
            x -> {}));  
    timeLine.setAutoReverse(true); 
    timeLine.play();    
} 

如果我可以在KeyFrame之内做到这一点,那可以解决我的问题。但不能。

while(t < 1) { 
    t+=0.05; 
    chart = new Chart(x -> Math.exp(-(Math.pow((x-t),2)))*Math.cos((2*Math.PI*(x-t))/l), 
         -1, 1, 0.01, new Axes(1000, 1000, 
           -1, 1, 0.1, -1, 1, 0.1) 
         ); 
} 
+0

那'Chart'类不眼熟。它当然不是'javafx.scene.chart.Chart' ......但是如果它设计得很好,它可以在不重新创建类的情况下分配数据......但是如果你需要访问时间,你应该使用['AnimationTimer' ](https://docs.oracle.com/javase/8/javafx/api/javafx/animation/AnimationTimer.html) – fabian

回答

0

可以lambda表达式内访问的唯一的局部变量final或有效最终变量。由于t被修改(t+=0.05),它既不是最终的也不是有效的最终结果。

所有你需要的是它的值复制到最终的变数:

while(t < 1) { 
    t+=0.05; 
    final double thisT = t ; 
    chart = new Chart(x -> Math.exp(-(Math.pow((x-thisT),2)))*Math.cos((2*Math.PI*(x-thisT))/l), 
         -1, 1, 0.01, new Axes(1000, 1000, 
           -1, 1, 0.1, -1, 1, 0.1) 
         ); 
}