JavaFX - 平移變換



平移變換隻是將物件移動到同一平面上的不同位置。您可以透過將平移座標 (tx, ty) 新增到原始座標 (X, Y) 來平移二維平面上的點;這將得到新的座標 (X', Y'),點將重新定位到該座標。

Translation

JavaFX 中的平移變換

在 JavaFX 中,使用平移變換,可以將節點從應用程式中的一個位置移動到另一個位置。此變換是在 JavaFX 節點上應用的,藉助於Translate 類,該類位於javafx.scene.transform包中。此類表示一個仿射物件,該物件重新定位 JavaFX 應用程式中的座標。

示例 1

以下是演示 JavaFX 中平移的程式。在這裡,我們建立了兩個位於同一位置且尺寸相同的圓形(節點),但顏色不同(棕色和暗藍色)。我們還在暗藍色的圓形上應用了平移。

將此程式碼儲存在名為TranslationExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.transform.Translate; 
import javafx.stage.Stage;  

public class TranslationExample extends Application { 
   @Override 
   public void start(Stage stage) {      
      //Drawing Circle1 
      Circle circle = new Circle(); 
      
      //Setting the position of the circle 
      circle.setCenterX(150.0f); 
      circle.setCenterY(135.0f); 
      
      //Setting the radius of the circle 
      circle.setRadius(100.0f); 
      
      //Setting the color of the circle 
      circle.setFill(Color.BROWN); 
      
      //Setting the stroke width of the circle 
      circle.setStrokeWidth(20); 
       
      //Drawing Circle2 
      Circle circle2 = new Circle(); 
      
      //Setting the position of the circle 
      circle2.setCenterX(150.0f); 
      circle2.setCenterY(135.0f); 
      
      //Setting the radius of the circle 
      circle2.setRadius(100.0f); 
      
      //Setting the color of the circle 
      circle2.setFill(Color.CADETBLUE); 
      
      //Setting the stroke width of the circle 
      circle2.setStrokeWidth(20); 
       
      //Creating the translation transformation 
      Translate translate = new Translate();       
      
      //Setting the X,Y,Z coordinates to apply the translation 
      translate.setX(300); 
      translate.setY(50); 
      translate.setZ(100);  
      
      //Adding transformation to circle2 
      circle2.getTransforms().addAll(translate); 
       
      //Creating a Group object  
      Group root = new Group(circle,circle2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Translation 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 TranslationExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslationExample 

輸出

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

Translate Transformation

示例 2

在以下示例中,我們演示了對 3D 形狀的平移。將此程式碼儲存在名為TranslationExample3D.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Cylinder; 
import javafx.scene.transform.Translate; 
import javafx.stage.Stage;  

public class TranslationExample3D extends Application { 
   @Override 
   public void start(Stage stage) {      
      Cylinder cy1 = new Cylinder(50, 100);
      Cylinder cy2 = new Cylinder(50, 100);
       
      //Creating the translation transformation 
      Translate translate = new Translate();       
      
      //Setting the X,Y,Z coordinates to apply the translation 
      translate.setX(200); 
      translate.setY(150); 
      translate.setZ(100);  
      
      //Adding transformation to circle2 
      cy2.getTransforms().addAll(translate); 
       
      //Creating a Group object  
      Group root = new Group(cy1,cy2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Translation 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 TranslationExample3D.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslationExample3D

輸出

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

Translate Transformation

如我們所見,使用圓柱體的原始位置,我們無法像在平移影像中那樣清楚地看到完整影像。因此,在類似情況下,平移變換變得必要。

廣告

© . All rights reserved.