JavaFX - 縮放變換



在縮放變換過程中,您可以擴充套件或壓縮物件的尺寸。縮放可以透過將物件的原始座標乘以縮放因子來實現,從而獲得所需的結果。

Scaling

JavaFX 中的縮放變換

在 JavaFX 中,縮放變換用於更改物件的大小。這是使用javafx.scene.transform包中的Scale類應用的。Scale 類表示一個仿射物件,它保留了原始節點的點、線和平行性。此物件按特定因子縮放座標。

讓我們來看一些示例,演示 JavaFX 中二維和三維物件的縮放變換。

示例 1

以下程式演示了 JavaFX 中的縮放。在這裡,我們在相同位置建立了兩個圓形(節點),尺寸相同,但顏色不同(淺褐色和藍色)。我們還在藍色圓形上應用了縮放變換。

將此程式碼儲存在名為ScalingExample.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.Scale; 
import javafx.stage.Stage; 
         
public class ScalingExample extends Application {  
   @Override 
   public void start(Stage stage) {      
      //Drawing Circle1 
      Circle circle1 = new Circle(300, 135, 50);   
      
      //Setting the color of the circle 
      circle1.setFill(Color.BLUE);        
      
      //Setting the stroke width of the circle 
      circle1.setStrokeWidth(20);       
       
      //Drawing Circle2 
      Circle circle2 = new Circle(300, 135, 50); 
      
      //Setting the color of the circle 
      circle2.setFill(Color.BURLYWOOD); 
      
      //Setting the stroke width of the circle 
      circle2.setStrokeWidth(20);
      
      //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); 
       
      //Adding the scale transformation to circle1 
      circle1.getTransforms().addAll(scale); 
         
      //Creating a Group object  
      Group root = new Group(circle1, circle2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Scaling 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 ScalingExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ScalingExample

輸出

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

Scaling Transformation

示例 2

現在,讓我們嘗試在一個三維物件(例如球體)上應用縮放變換。在這裡,我們將嘗試建立兩個球體:一個為原始大小,另一個在應用縮放後調整大小。

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

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.scene.shape.Sphere; 
import javafx.scene.transform.Scale; 
import javafx.stage.Stage; 
         
public class ScalingExample3D extends Application {  
   @Override 
   public void start(Stage stage) {      
      //Drawing Sphere1 
      Sphere sphere1 = new Sphere(100);  
      sphere1.setTranslateX(200); 
      sphere1.setTranslateY(150);
	  
      //Drawing Circle2 
      Sphere sphere2 = new Sphere(100);
	  sphere2.setTranslateX(200); 
      sphere2.setTranslateY(150);
      
      //Creating the scale transformation 
      Scale scale = new Scale(); 
      
      //Setting the dimensions for the transformation 
      scale.setX(0.5); 
      scale.setY(0.5); 
      
      //Setting the pivot point for the transformation 
      scale.setPivotX(300); 
      scale.setPivotY(150); 
       
      //Adding the scale transformation to circle1 
      sphere1.getTransforms().addAll(scale); 
         
      //Creating a Group object  
      Group root = new Group(sphere1, sphere2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Scaling 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 ScalingExample3D.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ScalingExample3D

輸出

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

Scaling Transformation
廣告
© . All rights reserved.