JavaFX - 位移轉換



一般來說,位移轉換隻是將物件從應用程式中的一個位置移動到另一個位置。這通常是透過指定新的座標點或需要移動該物件的距離來完成的。

JavaFX 允許您將位移轉換應用於各種節點。例如,在一些應用程式中,重要日子會有動畫角色在應用程式螢幕上移動;例如聖誕節的聖誕老人。這可以使用位移轉換來實現。

JavaFX 中的位移轉換

JavaFX 中的位移轉換使用 `javafx.animation` 包的 `TranslateTransition` 類在指定持續時間內移動 JavaFX 節點。此類包含下面列出的各種屬性,以提高所述動畫的質量。

  • byX − 指定此 `TranslateTransition` 從起始位置增量的停止 X 座標值。

  • byY − 指定此 `TranslateTransition` 從起始位置增量的停止 Y 座標值。

  • byZ − 指定此 `TranslateTransition` 從起始位置增量的停止 Z 座標值。

  • duration − 此 `TranslateTransition` 的持續時間

  • fromX − 指定此 `TranslateTransition` 的起始 X 座標值。

  • fromY − 指定此 `TranslateTransition` 的起始 Y 座標值。

  • fromZ − 指定此 `TranslateTransition` 的起始 Z 座標值。

  • node − 此 `TranslateTransition` 的目標節點。

  • toX − 指定此 `TranslateTransition` 的停止 X 座標值。

  • toY − 此 `TranslateTransition` 的停止 Y 座標值。

  • toZ − 此 `TranslateTransition` 的停止 Z 座標值。

示例

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

import javafx.animation.TranslateTransition; 
import javafx.application.Application; 
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 TranslateTransitionExample extends Application { 
   @Override 
   public void start(Stage stage) {  
      //Drawing a Circle 
      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); 
       
      //Creating Translate Transition 
      TranslateTransition translateTransition = new TranslateTransition(); 
      
      //Setting the duration of the transition  
      translateTransition.setDuration(Duration.millis(1000)); 
      
      //Setting the node for the transition 
      translateTransition.setNode(circle); 
      
      //Setting the value of the transition along the x axis. 
      translateTransition.setByX(300); 
      
      //Setting the cycle count for the transition 
      translateTransition.setCycleCount(50); 
      
      //Setting auto reverse value to false 
      translateTransition.setAutoReverse(false); 
      
      //Playing the animation 
      translateTransition.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("Translate 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 TranslateTransitionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateTransitionExample 

輸出

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

Translate Transition
廣告