📜  JavaFX动画的旋转变换

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

JavaFX旋转过渡

此过渡用于在节点上应用旋转过渡。它将在指定的持续时间内沿三个轴中的任何一个旋转节点。

RotateTransition由类javafx.animation.RotateTransition表示。我们只需要实例化此类即可生成适当的RotateTransition。

物产

下表描述了类的属性及其设置方法。

Property Description Setter Methods
axis This is a object type property of the class Point3D. This represents the axis of rotate transition. setAxis(Point3D value)
byAngle This is a double type property. This represents the angle by which the object will be rotated. setByAngle(double value)
duration This is the object type property of the class Duration. This represents the duration of the rotate transition. setDuration(Duration value)
fromAngle It is a double type property. It represents the start Angle of the rotate transition. setFromAngle(double value)
node It is an object type property of the class Node. It represents the node on which the rotate transition to be applied. setNode(Node value)
toAngle It is a double type property. It represents the stop angle value for the rotate transition. setToAngle(double value)

建设者

该类中有三个构造函数。

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

在下面的示例中,我们制作了一个沿Z轴旋转360度的矩形。

    package application;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Rotate_Transition extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
    
        //Creating Rectangle 
        Rectangle rect = new Rectangle(200,100,200,200);
        rect.setFill(Color.LIMEGREEN);
        rect.setStroke(Color.HOTPINK);
        rect.setStrokeWidth(5);
        
        //Instantiating RotateTransition class 
        RotateTransition rotate = new RotateTransition();
        
        //Setting Axis of rotation 
        rotate.setAxis(Rotate.Z_AXIS);
        
        // setting the angle of rotation 
        rotate.setByAngle(360);
        
        //setting cycle count of the rotation 
        rotate.setCycleCount(500);
        
        //Setting duration of the transition 
        rotate.setDuration(Duration.millis(1000));
        
        //the transition will be auto reversed by setting this to true 
        rotate.setAutoReverse(true);
            
        //setting Rectangle as the node onto which the 
// transition will be applied
        rotate.setNode(rect);
        
        //playing the transition 
        rotate.play();
        
        //Configuring Group and Scene 
        Group root = new Group();
        root.getChildren().add(rect);
        Scene scene = new Scene(root,600,400,Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Rotate Transition example");
        primaryStage.show();
        
    }
    public static void main(String[] args) {
        launch(args);
    }

}

输出: