JavaFX - 旋转变换



在旋转中,我们将对象绕其原点旋转特定角度θ (theta)。从下图可以看出,点 P(X, Y)位于距水平 X 坐标φ角度,距原点r距离处。

Rotation

JavaFX 中的旋转变换

旋转变换使用javafx.scene.transform包的Rotate类应用于 JavaFX 节点。在此变换中,仿射对象以指定角度旋转,并设置某些坐标作为锚点。

示例 1

以下是演示 JavaFX 中旋转变换的程序。在这里,我们在同一位置创建了 2 个矩形节点,具有相同的尺寸但颜色不同(Burlywood 和蓝色)。我们还在具有 Burlywood 颜色的矩形上应用旋转变换。

将此代码保存在名为RotationExample.java的文件中。

import javafx.application.Application; 
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; 
         
public class RotationExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing Rectangle1 
      Rectangle rectangle1 = new Rectangle(150, 75, 200, 150); 
      rectangle1.setFill(Color.BLUE); 
      rectangle1.setStroke(Color.BLACK);  
      
      //Drawing Rectangle2 
      Rectangle rectangle2 = new Rectangle(150, 75, 200, 150); 
      
      //Setting the color of the rectangle 
      rectangle2.setFill(Color.BURLYWOOD); 
      
      //Setting the stroke color of the rectangle 
      rectangle2.setStroke(Color.BLACK); 
       
      //creating the rotation transformation 
      Rotate rotate = new Rotate(); 
      
      //Setting the angle for the rotation 
      rotate.setAngle(20); 
      
      //Setting pivot points for the rotation 
      rotate.setPivotX(150); 
      rotate.setPivotY(225); 
        
      //Adding the transformation to rectangle2 
      rectangle2.getTransforms().addAll(rotate); 
        
      //Creating a Group object
      Group root = new Group(rectangle1, rectangle2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Rotation transformation 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 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls RotationExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls RotationExample

输出

执行后,上述程序生成一个如下所示的 javaFx 窗口。

Rotation Transformation

示例 2

在此示例中,我们尝试旋转一个 3D 对象,例如长方体。在这里,让我们创建两个长方体:一个表示其原始位置,另一个显示变换后的位置。

将此代码保存在名为RotationExample3D.java的文件中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate; 
import javafx.scene.transform.Translate; 
import javafx.stage.Stage; 
         
public class RotationExample3D extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Box 
      Box box1 = new Box();
      Box box2 = new Box();	  
      
      //Setting the properties of the Box 
      box1.setWidth(150.0); 
      box1.setHeight(150.0);   
      box1.setDepth(150.0);

      //Setting the properties of the Box 
      box2.setWidth(150.0); 
      box2.setHeight(150.0);   
      box2.setDepth(150.0);	  
       
      //Creating the translation transformation 
      Translate translate = new Translate();       
      translate.setX(200); 
      translate.setY(150); 
      translate.setZ(25);  
       
      Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS); 
      Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS); 
      Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS); 
      rxBox.setAngle(30); 
      ryBox.setAngle(50); 
      rzBox.setAngle(30); 
      box2.getTransforms().addAll(translate,rxBox, ryBox, rzBox); 
        
      //Creating a Group object  
      Group root = new Group(box1, box2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Transformation of a Box"); 
         
      //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 文件。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls RotationExample3D.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls RotationExample3D

输出

执行后,上述程序生成一个如下所示的 JavaFX 窗口。我们可以观察到长方体的原始位置并不完全在应用程序内;因此,需要应用变换。

Rotation Transformation
广告

© . All rights reserved.