JavaFX - 縮放變換



要更改物件的大小,可以使用縮放變換。在縮放過程中,您可以擴充套件或壓縮物件的尺寸。縮放可以透過將物件的原始座標乘以縮放因子來獲得所需的結果。

Scaling

示例

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

將此程式碼儲存在名為 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 ScalingExample.java 
java ScalingExample

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

Scaling Transformation
javafx_transformations.htm
廣告

© . All rights reserved.