有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

  1. # 1 楼答案

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