gpt4 book ai didi

java - 如何让2个动画连续运行

转载 作者:行者123 更新时间:2023-12-04 01:02:37 25 4
gpt4 key购买 nike

我是 JavaFX 新手

我想让2个动画连续运行

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

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

import javafx.util.Duration;

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

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 = 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.play();

try {
Thread.sleep(2000);
} catch (Exception e) {System.out.println(e);}

animation2.play();
}

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

但是当我尝试运行它时,我不明白为什么我看不到 animation1,程序停止,当它开始运行 animation2 时,它出现了。

如何让2个动画连续运行

最佳答案

您可以使用SequentialTransition:

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();
}
}

关于java - 如何让2个动画连续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67742392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com