My scene consists only of an ImageView, displaying an image. I would like to fade the image to black (assigned color of the scene), then after some time, fade from black to the image again. I found the FadeTransition very fitting for this purpose. This is a piece of my code:
// fade to black transition
FadeTransition ft1 = new FadeTransition(Duration.millis(2000), myImageView);
ft1.setFromValue(1.0);
ft1.setToValue(0.0);
ft1.play();
// fade from black transition
FadeTransition ft2 = new FadeTransition(Duration.millis(2000), myImageView);
ft2.setFromValue(0.0);
ft2.setToValue(1.0);
ft2.play();
My problem is that ft1.play()
is asynchronous, so the code below will start being executed before ft1.play()
is exited. As the result I see only the second transition. How can I wait for the first transition to end and then to launch the second transition? I cannot put the thread to sleep in between because it's the main javafx thread (tried and didn't work).
I tried using the onFinishedProperty() method with the combination of a busy-waiting on a flag, but I get stuck in the while loop forever. Here is my code for that:
boolean isTransitionPlaying;
FadeTransition ft = new FadeTransition(Duration.millis(2000), iv);
ft.setFromValue(1.0);
ft.setToValue(0.0);
ft.onFinishedProperty().set(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
transitionPlaying = false;
}
});
transitionPlaying = true;
ft.play();
while (transitionPlaying == true)
{
// busy wait
System.out.println("still waiting...");
}
FadeTransition ft2 = new FadeTransition(Duration.millis(2000), iv);
ft2.setFromValue(0.0);
ft2.setToValue(1.0);
ft2.play();
How is waiting done properly? Thank you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…