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.