JavaFX - 變換



變換無非是透過應用某些規則將圖形轉換成其他東西。這些規則允許你應用各種型別的變換,例如保持物體的形狀的同時移動其位置,根據角度旋轉物體,改變物體的尺寸等。

使用 JavaFX,你可以對單個節點或節點組應用變換。你也可以一次對 JavaFX 節點應用單一型別的變換或多種變換。所有這些變換都由各種類表示,這些類都是Transform類的子類,並且屬於包javafx.scene.transform

序號 變換和描述
1 旋轉

在旋轉中,我們將物體圍繞其原點旋轉特定角度θ (theta)

2 縮放

要更改物件的大小,可以使用縮放變換。

3 平移

將物件移動到螢幕上的不同位置。

4 切變

使物體形狀傾斜的變換稱為切變變換。

Transform 類在 JavaFX 節點上實現仿射變換。仿射變換隻不過是保留源物件在輸出物件中的點、直線以及這些直線平行性的變換型別。這些變換可以藉助擴充套件 Transform 類的Affine類應用於 JavaFX 節點。

多種變換

你可以透過兩種方式在 JavaFX 中對節點應用多種變換。你可以一次應用一種變換,或者將幾種變換組合在一起並一次應用它們。下面的程式是一個示例,它同時對矩形執行旋轉、縮放平移變換。

將此程式碼儲存到名為:

MultipleTransformationsExample.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.scene.transform.Scale; 
import javafx.scene.transform.Translate; 
import javafx.stage.Stage; 
         
public class MultipleTransformationsExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Rectangle
      Rectangle rectangle = new Rectangle(50, 50, 100, 75); 
      
      //Setting the color of the rectangle 
      rectangle.setFill(Color.BURLYWOOD); 
      
      //Setting the stroke color of the rectangle 
      rectangle.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); 
       
      //Creating the scale transformation 
      Scale scale = new Scale(); 
      
      //Setting the dimensions for the transformation 
      scale.setX(1.5); 
      scale.setY(1.5); 
      
      //Setting the pivot point for the transformation 
      scale.setPivotX(300); 
      scale.setPivotY(135); 
       
      //Creating the translation transformation 
      Translate translate = new Translate();       
      
      //Setting the X,Y,Z coordinates to apply the translation 
      translate.setX(250); 
      translate.setY(0); 
      translate.setZ(0); 
       
      //Adding all the transformations to the rectangle 
      rectangle.getTransforms().addAll(rotate, scale, translate); 
        
      //Creating a Group object  
      Group root = new Group(rectangle); 
      
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Multiple transformations"); 
         
      //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 MultipleTransformationsExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls MultipleTransformationsExample 

輸出

執行後,上述程式將生成如下所示的 JavaFX 視窗。

Multiple Transformation

三維物件的變換

JavaFX 允許你沿三個座標執行變換。但是,為了顯示具有三個維度(長度、寬度和深度)的物件,JavaFX 使用稱為 Z 緩衝的概念。

Z 緩衝,也稱為深度緩衝,是計算機圖形中一種用於儲存三維物件深度的緩衝區。這確保虛擬物件的透檢視與真實物件相同:前景表面會阻塞對背景表面的檢視(就像眼睛看到的那樣)。

如果要建立三維效果變換,請將所有三個座標 x、y 和 z 與 x 軸和 y 軸一起指定給變換建構函式。並且,為了能夠在 JavaFX 中看到三維物件和變換效果,使用者必須啟用透視攝像機。

示例

下面是一個示例,它旋轉和平移一個三維立方體。

將此程式碼儲存到名為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 box = new Box();  
      
      //Setting the properties of the Box 
      box.setWidth(150.0); 
      box.setHeight(150.0);   
      box.setDepth(150.0);       
       
      //Creating the translation transformation 
      Translate translate = new Translate();       
      translate.setX(400); 
      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); 
      box.getTransforms().addAll(translate,rxBox, ryBox, rzBox); 
        
      //Creating a Group object  
      Group root = new Group(box); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a cylinder"); 
         
      //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 視窗。

Transformation on 3d Objects
廣告