📜  JavaFX动画的平移变换

📅  最后修改于: 2020-10-14 03:27:57             🧑  作者: Mango

JavaFX翻译过渡

它在指定的持续时间内将节点从一个位置转换到另一位置。通过保持定期更新节点的translateX和translateY属性来完成转换。

过渡的速度取决于周期数,过渡将在指定的持续时间内进行。

在JavaFX中,TranslateTransition由类javafx.animation.TranslateTransition表示。我们需要实例化此类,以便将适当的Translate Transition应用于对象。

物产

下表描述了该类的属性以及setter方法。

Property Description Setter Method
byX This is a double type property. It represents the incremented X-coordinate value at which, the translation stops. setByX(double value)
byY This is a double type property. It represents the incremented Y-coordinate value at which, the translation stops. setByY(double value)
byZ This is a double type property. It represents the incremented Z-coordinate value at which, the translation stops. setByZ(double value)
duration This is an object type property of the class Duration . It represents the duration of Translate transition. setDuration(Duration value)
fromX This is a double type property. It represents the X coordinate value from which the translation starts. setFromX(double value)
fromY This is a double type property. It represents the Y coordinate value from which the translation starts. This is a double type property. It represents the X coordinate value from which the translation starts. setFromY(double value)
fromZ This is a double type property. It represents the Z coordinate value from which the translation starts. setFromZ(double value)
node This is an object type property of the class Node. It represents the node onto which, the scale transition is applied. setNode(Node node)
toX This is a double type property. It represents the stop X coordinate value of the translate transition. setToX(double value)
toY This is a double type property. It represents the stop Y coordinate value of the translate transition. setToY(double value)
toZ This is a double type property. It represents the stop Z coordinate value of the scale transition. setToZ(double value)

建设者

该类中有三个构造函数。

  • public TranslateTransition():使用默认参数创建TranslateTransition的新实例。
  • public TranslateTransition(Duration duration):使用指定的持续时间创建TranslateTransition的新实例。
  • public TranslateTransition(Duration duration,Node node):使用指定的持续时间和节点创建Translate Transition的新实例。

例:

在下面的示例中,我们制作了一个圆,将自己在X方向平移400。

package application;
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 Translate_Transition extends Application{

@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating the circle 
Circle cir = new Circle(50,100,50);

//setting color and stroke of the cirlce
cir.setFill(Color.RED);
cir.setStroke(Color.BLACK);

//Instantiating TranslateTransition class 
TranslateTransition translate = new TranslateTransition();

//shifting the X coordinate of the centre of the circle by 400 
translate.setByX(400);

//setting the duration for the Translate transition 
translate.setDuration(Duration.millis(1000));

//setting cycle count for the Translate transition 
translate.setCycleCount(500);

//the transition will set to be auto reversed by setting this to true 
translate.setAutoReverse(true);

//setting Circle as the node onto which the transition will be applied
translate.setNode(cir);

//playing the transition 
translate.play();

//Configuring Group and Scene 
Group root = new Group();
root.getChildren().addAll(cir);
Scene scene = new Scene(root,500,200,Color.WHEAT);
primaryStage.setScene(scene);
primaryStage.setTitle("Translate Transition example");
primaryStage.show();

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

}

输出: