JavaFX - 縮放轉換



顧名思義,縮放是指增加或減小物件的大小。在計算機圖形學中,透過對物件(或影像)應用縮放轉換,可以在指定的時間內增加或減小其大小。

此轉換也可以應用於各種 JavaFX 節點,例如 2D 形狀、3D 形狀、文字以及應用程式上的任何其他元素。

JavaFX 中的縮放轉換

縮放轉換在 JavaFX 中使用ScaleTransition類執行,該類屬於javafx.animation包。此類包含各種屬性,可幫助在 JavaFX 應用程式上播放此動畫:

  • byX - 指定此 ScaleTransition 從起始位置開始增加的停止 X 縮放值。

  • byY - 指定此 ScaleTransition 從起始位置開始增加的停止 Y 縮放值。

  • byZ - 指定此 ScaleTransition 從起始位置開始增加的停止 Z 縮放值。

  • duration - 此 ScaleTransition 的持續時間。

  • fromX - 指定此 ScaleTransition 的起始 X 縮放值。

  • fromY - 指定此 ScaleTransition 的起始 Y 縮放值。

  • fromZ - 指定此 ScaleTransition 的起始 Z 縮放值。

  • node - 此 ScaleTransition 的目標節點。

  • toX - 指定此 ScaleTransition 的停止 X 縮放值。

  • toY - 此 ScaleTransition 的停止 Y 縮放值。

  • toZ - 此 ScaleTransition 的停止 Z 縮放值。

示例

以下是演示 JavaFX 中縮放轉換的程式。將此程式碼儲存在名為ScaleTransitionExample.java的檔案中。

import javafx.animation.ScaleTransition; 
import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 
         
public class ScaleTransitionExample extends Application {  
   @Override 
   public void start(Stage stage) {      
      //Drawing a Circle 
      Circle circle = new Circle(); 
      
      //Setting the position of the circle 
      circle.setCenterX(300.0f); 
      circle.setCenterY(135.0f); 
      
      //Setting the radius of the circle 
      circle.setRadius(50.0f); 
      
      //Setting the color of the circle 
      circle.setFill(Color.BROWN); 
      
      //Setting the stroke width of the circle 
      circle.setStrokeWidth(20); 
       
      //Creating scale Transition 
      ScaleTransition scaleTransition = new ScaleTransition(); 
      
      //Setting the duration for the transition 
      scaleTransition.setDuration(Duration.millis(1000)); 
      
      //Setting the node for the transition 
      scaleTransition.setNode(circle); 
      
      //Setting the dimensions for scaling 
      scaleTransition.setByY(1.5); 
      scaleTransition.setByX(1.5); 
      
      //Setting the cycle count for the translation 
      scaleTransition.setCycleCount(50); 
      
      //Setting auto reverse value to true 
      scaleTransition.setAutoReverse(false); 
      
      //Playing the animation 
      scaleTransition.play(); 
         
      //Creating a Group object  
      Group root = new Group(circle); 
         
      //Creating a scene object  
      Scene scene = new Scene(root, 600, 300); 
      
      //Setting title to the Stage 
      stage.setTitle("Scale transition 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 ScaleTransitionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ScaleTransitionExample

輸出

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

Scale Transition
廣告