简体   繁体   中英

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().

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

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.

Besides of that, you can't check for newValue==Duration.millis(250) because if you output the newValue variable you get eg this:

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.

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.

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:

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...");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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