如何连续制作2个动画运行
How to make 2 animations run in succession
我是 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,程序停止,当它开始 运行ning 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();
}
}
我是 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,程序停止,当它开始 运行ning 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();
}
}