JavaFX - 几何转换 动画



  • 淡入淡出

    以下是在 JavaFX 中演示淡入淡出过渡的程序。将此代码保存在名称为的文件中FadeTransitionExample.java.
    
    import javafx.animation.FadeTransition; 
    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 FadeTransitionExample 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(100.0f); 
          
          //Setting the color of the circle 
          circle.setFill(Color.BROWN); 
          
          //Setting the stroke width of the circle 
          circle.setStrokeWidth(20); 
           
          //Creating the fade Transition 
          FadeTransition fadeTransition = new FadeTransition(Duration.millis(1000)); 
          
          //Setting the node for Transition 
          fadeTransition.setNode(circle); 
          
          //Setting the property fromValue of the transition (opacity) 
          fadeTransition.setFromValue(1.0); 
          
          //Setting the property toValue of the transition (opacity) 
          fadeTransition.setToValue(0.3); 
          
          //Setting the cycle count for the transition 
          fadeTransition.setCycleCount(50); 
          
          //Setting auto reverse value to false 
          fadeTransition.setAutoReverse(false); 
      
          //Playing the animation 
          fadeTransition.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("Fade 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 FadeTransitionExample.java 
    java FadeTransitionExample
    
    执行时,上述程序会生成一个 JavaFX 窗口,如下所示。
    淡入淡出
  • 填充过渡

    以下是在 JavaFX 中演示填充转换的程序。将此代码保存在名称为的文件中FillTransitionExample.java.
    
    import javafx.animation.FillTransition; 
    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 FillTransitionExample 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(100.0f); 
          
          //Setting the color of the circle 
          circle.setFill(Color.BROWN); 
          
          //Setting the stroke width of the circle 
          circle.setStrokeWidth(20); 
           
          //Creating the fill Transition 
          FillTransition fillTransition = new FillTransition(Duration.millis(1000)); 
          
          //Setting the shape for Transition 
          fillTransition.setShape(circle); 
          
          //Setting the from value of the transition (color) 
          fillTransition.setFromValue(Color.BLUEVIOLET);  
          
          //Setting the toValue of the transition (color) 
          fillTransition.setToValue(Color.CORAL); 
          
          //Setting the cycle count for the transition 
          fillTransition.setCycleCount(50); 
          
          //Setting auto reverse value to false 
          fillTransition.setAutoReverse(false);  
          
          //Playing the animation 
          fillTransition.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("Fill 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 FillTransitionExample.java 
    java FillTransitionExample
    
    执行时,上述程序会生成一个 JavaFX 窗口,如下所示。
    填充过渡
  • 笔画过渡

    以下是在 JavaFX 中演示 Stoke Transition 的程序。将此代码保存在名称为的文件中StrokeTransitionExample.java.
    
    import javafx.animation.StrokeTransition; 
    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 StrokeTransitionExample 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(100.0f); 
          
          //Setting the color of the circle 
          circle.setFill(Color.BROWN); 
          
          //Setting the stroke width of the circle 
          circle.setStrokeWidth(20); 
           
          //creating stroke transition  
          StrokeTransition strokeTransition = new StrokeTransition(); 
          
          //Setting the duration of the transition 
          strokeTransition.setDuration(Duration.millis(1000)); 
          
          //Setting the shape for the transition 
          strokeTransition.setShape(circle); 
          
          //Setting the fromValue property of the transition (color) 
          strokeTransition.setFromValue(Color.BLACK); 
          
          //Setting the toValue property of the transition (color) 
          strokeTransition.setToValue(Color.BROWN); 
           
          //Setting the cycle count for the transition 
          strokeTransition.setCycleCount(50); 
          
          //Setting auto reverse value to false 
          strokeTransition.setAutoReverse(false); 
          
          //Playing the animation 
          strokeTransition.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("Stroke 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 StrokeTransitionExample.java 
    java StrokeTransitionExample 
    
    执行时,上述程序会生成一个 JavaFX 窗口,如下所示。
    笔画过渡