Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使两个动画连续运行_Java_Javafx - Fatal编程技术网

Java 如何使两个动画连续运行

Java 如何使两个动画连续运行,java,javafx,Java,Javafx,我是JavaFX新手 我想让两个动画连续运行 我试着创建两个圆圈,我让第一个(水)圆圈播放动画1,然后让第二个(红色)圆圈播放动画2。 但如果只是这样,两个动画将同时播放 我尝试向其中添加Thread.sleep(): 但是当我试着运行它时,我不明白为什么我看不到动画1,程序停止,当它开始运行动画2时,它显示出来 我应该怎么做才能使两个动画连续运行您可以使用顺序转换: public class TestAnimation2 extends Application{ @Override

我是JavaFX新手

我想让两个动画连续运行

我试着创建两个圆圈,我让第一个(水)圆圈播放动画1,然后让第二个(红色)圆圈播放动画2。 但如果只是这样,两个动画将同时播放

我尝试向其中添加Thread.sleep():

但是当我试着运行它时,我不明白为什么我看不到动画1,程序停止,当它开始运行动画2时,它显示出来


我应该怎么做才能使两个动画连续运行

您可以使用
顺序转换

public class TestAnimation2 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = new Pane();
        pane.setMinSize(1800, 900);
        Group root = new Group(pane);
        Scene mainScene = new Scene(root);

        Circle cir1 = new Circle(100, 100, 30);
        cir1.setFill(Color.AQUA);
        pane.getChildren().add(cir1);

        TranslateTransition animation1 = new TranslateTransition();
        animation1.setToX(300);
        animation1.setToY(0);
        animation1.setDuration(Duration.seconds(2));
        animation1.setNode(cir1);



        Circle cir2 = new Circle(100, 200, 30);
        cir2.setFill(Color.RED);
        pane.getChildren().add(cir2);

        TranslateTransition animation2 = new TranslateTransition();
        animation2.setToX(300);
        animation2.setToY(0);
        animation2.setDuration(Duration.seconds(2));
        animation2.setNode(cir2);

        primaryStage.setScene(mainScene);
        primaryStage.setResizable(false);
        primaryStage.show();

        SequentialTransition animation = new SequentialTransition(animation1, animation2);
        animation.play();  

    }

    public static void main(String[] args) {
        launch();
    }
}
或者,您可以使用第一个动画的
onFinished
处理程序来播放第二个动画:

public class TestAnimation2 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = new Pane();
        pane.setMinSize(1800, 900);
        Group root = new Group(pane);
        Scene mainScene = new Scene(root);

        Circle cir1 = new Circle(100, 100, 30);
        cir1.setFill(Color.AQUA);
        pane.getChildren().add(cir1);

        TranslateTransition animation1 = new TranslateTransition();
        animation1.setToX(300);
        animation1.setToY(0);
        animation1.setDuration(Duration.seconds(2));
        animation1.setNode(cir1);



        Circle cir2 = new Circle(100, 200, 30);
        cir2.setFill(Color.RED);
        pane.getChildren().add(cir2);

        TranslateTransition animation2 = new TranslateTransition();
        animation2.setToX(300);
        animation2.setToY(0);
        animation2.setDuration(Duration.seconds(2));
        animation2.setNode(cir2);

        primaryStage.setScene(mainScene);
        primaryStage.setResizable(false);
        primaryStage.show();

        animation1.setOnFinished(event -> animation2.play());

        animation1.play();

    }

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

不要阻塞JavaFX应用程序线程;相反,考虑一个<代码>顺序转换,它按顺序顺序播放<代码>动画< /代码>。
public class TestAnimation2 extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane = new Pane();
        pane.setMinSize(1800, 900);
        Group root = new Group(pane);
        Scene mainScene = new Scene(root);

        Circle cir1 = new Circle(100, 100, 30);
        cir1.setFill(Color.AQUA);
        pane.getChildren().add(cir1);

        TranslateTransition animation1 = new TranslateTransition();
        animation1.setToX(300);
        animation1.setToY(0);
        animation1.setDuration(Duration.seconds(2));
        animation1.setNode(cir1);



        Circle cir2 = new Circle(100, 200, 30);
        cir2.setFill(Color.RED);
        pane.getChildren().add(cir2);

        TranslateTransition animation2 = new TranslateTransition();
        animation2.setToX(300);
        animation2.setToY(0);
        animation2.setDuration(Duration.seconds(2));
        animation2.setNode(cir2);

        primaryStage.setScene(mainScene);
        primaryStage.setResizable(false);
        primaryStage.show();

        animation1.setOnFinished(event -> animation2.play());

        animation1.play();

    }

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