Javafx 简明教程
JavaFX - Rotate Transition
可在 JavaFX 中应用动画更改对象的几何形状、属性、位置等。旋转过渡用于处理对象的旋转位置,同时保留其形状和属性。
Animations in JavaFX can be applied to change an object’s geometry, properties, position, etc. Rotate transition is used to deal with an object’s position by retaining its shape and properties.
某个对象以其中心点为轴,绕中心点旋转。这种类型的动画称为旋转过渡。通过旋转对象,您可以在播放对象时对其应用该旋转过渡。
An object is pivoted at its centre point and rotated around it. This type of animation is known as Rotate transition. Various software allow you to apply this rotate transition on an object by rotating it, while it is being displayed.
该类型的动画可以在对象进入应用程序或退出应用程序时应用。
This type of animation can be applied while entering the object into the application or exiting it.
Rotate Transition in JavaFX
JavaFX 中的旋转过渡使用包 javafx.animation 中的 RotateTransition 类应用。该操作通过指定起始值、结束值和过渡持续时间完成。RotateTransition 类具有以下属性 −
Rotate Transition in JavaFX is applied using the RotateTransition class in the javafx.animation package. This is done by specifying the starting value, the ending value and the duration of the transition. The RotateTransition class has the following properties −
-
axis − Specifies the axis of rotation for this RotateTransition.
-
node − The target node of this RotateTransition.
-
byAngle − Specifies the incremented stop angle value, from the start, of this RotateTransition.
-
fromAngle − Specifies the start angle value for this RotateTransition.
-
toAngle − Specifies the stop angle value for this RotateTransition.
-
duration − The duration of this RotateTransition.
Example
以下是 JavaFX 中旋转过渡演示的程序。将此代码保存在名为 RotateTransitionExample.java 的文件中。
Following is the program which demonstrates Rotate Transition in JavaFX. Save this code in a file with the name RotateTransitionExample.java.
import javafx.animation.RotateTransition;
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.Polygon;
import javafx.stage.Stage;
import javafx.util.Duration;
public class RotateTransitionExample extends Application {
@Override
public void start(Stage stage) {
//Creating a hexagon
Polygon hexagon = new Polygon();
//Adding coordinates to the hexagon
hexagon.getPoints().addAll(new Double[]{
200.0, 50.0,
400.0, 50.0,
450.0, 150.0,
400.0, 250.0,
200.0, 250.0,
150.0, 150.0,
});
//Setting the fill color for the hexagon
hexagon.setFill(Color.BLUE);
//Creating a rotate transition
RotateTransition rotateTransition = new RotateTransition();
//Setting the duration for the transition
rotateTransition.setDuration(Duration.millis(1000));
//Setting the node for the transition
rotateTransition.setNode(hexagon);
//Setting the angle of the rotation
rotateTransition.setByAngle(360);
//Setting the cycle count for the transition
rotateTransition.setCycleCount(50);
//Setting auto reverse value to false
rotateTransition.setAutoReverse(false);
//Playing the animation
rotateTransition.play();
//Creating a Group object
Group root = new Group(hexagon);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Rotate 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 RotateTransitionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls RotateTransitionExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
On executing, the above program generates a JavaFX window as shown below.