Javafx 简明教程
JavaFX - Translate Transition
一般来说,Translate 动效只将应用程序中的某个对象从一个位置移至另一个位置。这通常是通过指定新坐标点或此对象需要移动到的距离来完成的。
In general, the Translate transition simply moves an object from one location to another on an application. This is usually done by specifying the new coordinate points or the distance this object needs to be moved to.
JavaFX 允许您在各个节点上应用 translate 动效。例如,在少数几个应用中,在重要的日子里,会看到一些动画角色在应用程序屏幕上移动;例如圣诞老人在圣诞节那天。这可以通过平移动效来执行。
JavaFX allows you to apply the translate transition on various nodes. For instance, in few applications on important days, some animated characters are seen moving around the application screen; like Santa Claus on Christmas. That can be performed using translation transition.
Translate Transition in JavaFX
JavaFX 中的 Translate 动效使用 javafx.animation 包的 TranslateTransition 类在指定的时间段内移动 JavaFX 节点。此类包含多个属性,如下所列,以提高所述动画的质量。
The Translate transition in JavaFX moves the JavaFX nodes for a specified duration using the TranslateTransition class of javafx.animation package. This class contains various properties which are listed below to improve the quality of the said animation.
-
byX − Specifies the incremented stop X coordinate value, from the start, of this TranslateTransition.
-
byY − Specifies the incremented stop Y coordinate value, from the start, of this TranslateTransition.
-
byZ − Specifies the incremented stop Z coordinate value, from the start, of this TranslateTransition.
-
duration − The duration of this TranslateTransition
-
fromX − Specifies the start X coordinate value of this TranslateTransition.
-
fromY − Specifies the start Y coordinate value of this TranslateTransition.
-
fromZ − Specifies the start Z coordinate value of this TranslateTransition.
-
node − The target node of this TranslateTransition.
-
toX − Specifies the stop X coordinate value of this TranslateTransition.
-
toY − The stop Y coordinate value of this TranslateTransition.
-
toZ − The stop Z coordinate value of this TranslateTransition.
Example
以下是演示 JavaFX 中转换过渡的程序。将此代码保存在名称为 TranslateTransitionExample.java 的文件中。
Following is the program which demonstrates Translate Transition in JavaFX. Save this code in a file with the name TranslateTransitionExample.java.
import javafx.animation.TranslateTransition;
import javafx.application.Application;
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 TranslateTransitionExample 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(100.0f);
//Setting the color of the circle
circle.setFill(Color.BROWN);
//Setting the stroke width of the circle
circle.setStrokeWidth(20);
//Creating Translate Transition
TranslateTransition translateTransition = new TranslateTransition();
//Setting the duration of the transition
translateTransition.setDuration(Duration.millis(1000));
//Setting the node for the transition
translateTransition.setNode(circle);
//Setting the value of the transition along the x axis.
translateTransition.setByX(300);
//Setting the cycle count for the transition
translateTransition.setCycleCount(50);
//Setting auto reverse value to false
translateTransition.setAutoReverse(false);
//Playing the animation
translateTransition.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("Translate 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 TranslateTransitionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateTransitionExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
On executing, the above program generates a JavaFX window as shown below.
