JavaFX - 暫停轉換



暫停轉換是 JavaFX 提供的一種轉換型別,它會在暫停動畫一段時間後,以順序方式恢復另一個轉換。

可以透過兩種方式將多個轉換應用於 JavaFX 節點:順序和並行。順序應用時,您還可以暫停動畫一段時間,然後再應用另一個轉換。這有助於在您需要更清晰地檢視應用的轉換的場景中。

讓我們在本節中進一步瞭解這種轉換。

暫停轉換

PauseTransition 類屬於 javafx.animation 包,在以順序方式將多個轉換應用於 JavaFX 節點時使用。此類會在每個轉換後暫停動畫一段時間。應用於節點的所有轉換都是此類的子轉換,它們繼承其 node 屬性(僅當未指定其自身的 node 屬性時)。

示例

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

import javafx.animation.PauseTransition; 
import javafx.animation.ScaleTransition;  
import javafx.animation.SequentialTransition; 
import javafx.animation.TranslateTransition; 

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 PauseTransitionExample 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(50.0f); 
      
      //Setting the color of the circle 
      circle.setFill(Color.BROWN); 
      
      //Setting the stroke width of the circle 
      circle.setStrokeWidth(20);       
        
      //Creating a Pause Transition
      PauseTransition pauseTransition = new PauseTransition(); 
      
      //Setting the duration for the transition 
      pauseTransition.setDuration(Duration.millis(1000)); 
       
      //Creating Translate Transition 
      TranslateTransition translateTransition = new TranslateTransition();  
      
      //Setting the duration for the transition 
      translateTransition.setDuration(Duration.millis(1000));       
      
      //Setting the node of the transition 
      translateTransition.setNode(circle); 
      
      //Setting the value of the transition along the x axis 
      translateTransition.setByX(300); 
      
      //Setting the cycle count for the stroke 
      translateTransition.setCycleCount(5);  
      
      //Setting auto reverse value to true 
      translateTransition.setAutoReverse(false);  
       
      //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(5); 
      
      //Setting auto reverse value to true 
      scaleTransition.setAutoReverse(false);       
       
      //Applying Sequential transition to the circle 
      SequentialTransition sequentialTransition = new SequentialTransition(
         circle, translateTransition, pauseTransition, scaleTransition );
      
      //Playing the animation 
      sequentialTransition.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("Pause 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 PauseTransitionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls PauseTransitionExample 

輸出

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

Pause Transition
廣告