简体   繁体   English

如何使用侦听器“ currentTimeProperty”? JavaFX的

[英]How I can use the listener “currentTimeProperty” ? JavaFX

I want to add a listener to "pathTransition.currentTimeProperty()" and when the Time is 250ms show a println(). 我想向“ pathTransition.currentTimeProperty()”添加一个侦听器,并且当时间为250ms时显示一个println()。

I tried this: 我尝试了这个:

pathTransition2.currentTimeProperty().addListener(new ChangeListener<Duration>() {

                @Override
                public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                    if(newValue==Duration.millis(250))
                        System.out.println("250ms");
                }
                });

But nothing happens ... 但是什么也没发生...

I'm doing something wrong ? 我做错了吗?

PD: Here is the full code PD:这是完整的代码

For one, your example isn't showing the problem, the "full code" does. 例如,您的示例未显示问题,“完整代码”显示了问题。 You are adding a listener over and over again in the while loop. 您将在while循环中一次又一次地添加一个侦听器。

Besides of that, you can't check for newValue==Duration.millis(250) because if you output the newValue variable you get eg this: 除此之外,您无法检查newValue == Duration.millis(250),因为如果输出newValue变量,则会得到例如:

341.6666666666667 ms
378.5 ms
411.8333333333333 ms
441.1666666666667 ms
473.0 ms
487.1666666666667 ms
494.8333333333333 ms
505.1666666666667 ms
521.1666666666666 ms
537.1666666666666 ms

You need to check for a delta around the 250 ms. 您需要检查250毫秒左右的增量。

Here's your modified code. 这是您修改的代码。 The problem you have is that the currentTimeProperty goes up first and down once it reaches 1000, up when it reaches 0 etc. 您遇到的问题是currentTimeProperty达到1000时首先上升和下降,达到0时上升。

Here's a modified example of your code that works: 这是可以正常工作的代码修改示例:

import static javafx.animation.Animation.INDEFINITE;
import javafx.animation.PathTransition;
import javafx.animation.PathTransitionBuilder;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author Jose
 */
public class Listener extends Application {

    long prevTime = System.currentTimeMillis();

    @Override
    public void start(Stage primaryStage) {

        Circle circle = new Circle(50);

        Path path2 = PathBuilder.create().elements(new MoveTo(0, 0), new LineTo(0, 80)).build();
        path2.setFill(Color.RED);
        path2.setStroke(Color.RED);
        path2.setStrokeWidth(1);
        path2.setLayoutX(0);
        path2.setLayoutY(0);

        PathTransition pathTransition2 = PathTransitionBuilder.create().duration(javafx.util.Duration.millis(1000)).cycleCount(INDEFINITE).autoReverse(true).path(path2).node(circle).build();

        pathTransition2.currentTimeProperty().addListener(new ChangeListener<Duration>() {

            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {

                long currTime = System.currentTimeMillis();
                if (Double.compare((currTime - prevTime), 250) > 0) {
                    System.out.println("delta: " + (currTime - prevTime) + ", old value: " + oldValue + ", new value: " + newValue);
                    prevTime = currTime;
                }

            }
        });

        pathTransition2.play();

        StackPane root = new StackPane();
        root.getChildren().addAll(path2, circle);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

As values you'll get something like this: 作为值,您将获得如下内容:

delta: 251, old value: 174.33333333333334 ms, new value: 190.33333333333334 ms
delta: 256, old value: 430.3333333333333 ms, new value: 446.1666666666667 ms
delta: 256, old value: 686.3333333333334 ms, new value: 702.3333333333334 ms
delta: 256, old value: 942.3333333333334 ms, new value: 958.3333333333334 ms
delta: 256, old value: 801.6666666666666 ms, new value: 785.6666666666666 ms
delta: 256, old value: 545.6666666666666 ms, new value: 529.6666666666666 ms
delta: 256, old value: 289.6666666666667 ms, new value: 273.6666666666667 ms
delta: 256, old value: 33.666666666666664 ms, new value: 17.666666666666668 ms
delta: 256, old value: 222.33333333333334 ms, new value: 238.33333333333334 ms
delta: 272, old value: 478.3333333333333 ms, new value: 510.3333333333333 ms
delta: 256, old value: 750.3333333333334 ms, new value: 766.3333333333334 ms
delta: 256, old value: 993.6666666666666 ms, new value: 977.6666666666666 ms
delta: 256, old value: 753.5 ms, new value: 721.6666666666666 ms
delta: 256, old value: 481.6666666666667 ms, new value: 465.6666666666667 ms
delta: 256, old value: 225.66666666666666 ms, new value: 209.66666666666666 ms
delta: 256, old value: 30.5 ms, new value: 46.5 ms
delta: 256, old value: 286.5 ms, new value: 302.3333333333333 ms
delta: 256, old value: 542.5 ms, new value: 558.5 ms
delta: 256, old value: 798.5 ms, new value: 814.3333333333334 ms
delta: 256, old value: 945.5 ms, new value: 929.6666666666666 ms
delta: 256, old value: 689.6666666666666 ms, new value: 673.5 ms
delta: 256, old value: 433.5 ms, new value: 417.5 ms
delta: 256, old value: 177.5 ms, new value: 161.5 ms

You can create a BooleanBinding and observe that: 您可以创建一个BooleanBinding并观察到:

Duration quarterSecond = Duration.millis(250);
BooleanBinding afterQuarterSecond = Bindings.createBooleanBinding(() -> 
    pathTransition2.getCurrentTime().greaterThanOrEqualTo(quarterSecond), 
    pathTransition2.currentTimeProperty());
afterQuarterSecond.addListener((obs, wasAfter, isNowAfter) -> {
    if (isNowAfter) {
        System.out.println("250 ms...");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM