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
广告

© . All rights reserved.