Javafx 简明教程
JavaFX - Pause Transition
暂停转换是一种由 JavaFX 提供的转换,它会在继续按顺序执行其他转换前,暂停动画一段时间。
Pause transition is a type of transition offered by JavaFX, which pauses the animation for a while before resuming another transition in a sequential order.
可以通过两种方式对 JavaFX 节点应用多个转换:顺序地或并行地。当顺序应用时,你还可以暂停动画一段时间,然后再应用另一个转换。这有助于在需要以更清晰的方式查看所应用转换的情况下。
Multiple transitions can be applied on a JavaFX node in two ways: sequentially and parallelly. When applied sequentially, you can also pause the animation for a while before applying another transition. This helps in scenarios where you need to see the transitions applied in a more clear way.
让我们在本章中进一步了解这种转换。
Let us learn more about this transition further in this chapter.
Pause Transition
PauseTransition 类属于 javafx.animation 包,并在按顺序对 JavaFX 节点应用多个转换时使用。此类在每次转换后都会暂停动画指定持续时间。应用于该节点的所有转换都是此类及其节点属性的子转换(前提是未指定它们自己的节点属性)。
PauseTransition class belongs to the javafx.animation package and is used while applying multiple transitions on a JavaFX node in a sequential order. This class pauses the animation after each transition for a specified duration. All the transitions applied on the node are child transitions of this class that inherit its node property (only if their own node property is not specified).
Example
以下是演示 JavaFX 中暂停转换的程序。将此代码保存在名为 PauseTransitionExample.java 的文件中。
Following is the program which demonstrates Pause Transition in JavaFX. Save this code in a file with the name PauseTransitionExample.java.
import javafx.animation.PauseTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class PauseTransitionExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Circle
Circle circle = new Circle();
//Setting the position of the circle
circle.setCenterX(150.0f);
circle.setCenterY(135.0f);
//Setting the radius of the circle
circle.setRadius(50.0f);
//Setting the color of the circle
circle.setFill(Color.BROWN);
//Setting the stroke width of the circle
circle.setStrokeWidth(20);
//Creating a Pause Transition
PauseTransition pauseTransition = new PauseTransition();
//Setting the duration for the transition
pauseTransition.setDuration(Duration.millis(1000));
//Creating Translate Transition
TranslateTransition translateTransition = new TranslateTransition();
//Setting the duration for the transition
translateTransition.setDuration(Duration.millis(1000));
//Setting the node of the transition
translateTransition.setNode(circle);
//Setting the value of the transition along the x axis
translateTransition.setByX(300);
//Setting the cycle count for the stroke
translateTransition.setCycleCount(5);
//Setting auto reverse value to true
translateTransition.setAutoReverse(false);
//Creating scale Transition
ScaleTransition scaleTransition = new ScaleTransition();
//Setting the duration for the transition
scaleTransition.setDuration(Duration.millis(1000));
//Setting the node for the transition
scaleTransition.setNode(circle);
//Setting the dimensions for scaling
scaleTransition.setByY(1.5);
scaleTransition.setByX(1.5);
//Setting the cycle count for the translation
scaleTransition.setCycleCount(5);
//Setting auto reverse value to true
scaleTransition.setAutoReverse(false);
//Applying Sequential transition to the circle
SequentialTransition sequentialTransition = new SequentialTransition(
circle, translateTransition, pauseTransition, scaleTransition );
//Playing the animation
sequentialTransition.play();
//Creating a Group object
Group root = new Group(circle);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Pause transition example");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls PauseTransitionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PauseTransitionExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
On executing, the above program generates a JavaFX window as shown below.
