Reputation: 324
I want my program to do something after an PathTransition
finishes. So I created a thread to run the animation, along with join() method to let the program to wait for this thread. Here is my trial code below
public class JavaFXApplication6 extends Application {
@Override
public void start(Stage primaryStage) {
ImageView iv = new ImageView(new Image("File:1.JPG"));
iv.setFitHeight(80);
iv.setFitWidth(50);
PathTransition pt = new PathTransition();
pt.setNode(iv);
pt.setCycleCount(1);
pt.setDuration(Duration.seconds(2));
pt.setPath(new Path(new MoveTo(0, 0), new LineTo(200, 200)));
AnchorPane root = new AnchorPane();
root.getChildren().add(iv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
Thread t = new Thread (){
public void run(){
pt.play();
}
};
t.start();
try {
t.join();
}catch(InterruptedException w){}
System.out.println("I should be printed after the animation!");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
However, the String printed out before the animation finishes. Why this could happen? Isn't is the deal that whatever after join() will wait for that thread to die?
If thread can't achieve this, what trick can i use to let program wait for animations, without using .setOnFinished()
?
Upvotes: 0
Views: 225
Reputation: 599
Accroding to https://docs.oracle.com/javafx/2/api/javafx/animation/Animation.html#play() :
Animation#play()
(PathTransition
inherits it) is an asynchronous call, meaning it probably goes on a new thread, not linked to the thread you started, which ends immediately after that call.
Upvotes: 2