java关键帧动画
我如何使用变量“t”,它在一个构造函数参数中包含的数学模式中。“t”对应使图表向右移动的时间
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)
);
}
# 1 楼答案
在lambda表达式中可以访问的唯一局部变量是
final
或有效的最终变量。因为t
被修改了(t+=0.05
),所以它既不是最终的,也不是有效的最终的您只需将其值复制到最终变量: