Thursday, December 12, 2019

Javafx animation

Yes, there is progress under the sun: Oracle released its
two toolkits, AWT and Swing in 1995 and 1997 respectively.
These were presented as tools for making user interfaces.
In 2007, came javafx, capable of facilitating animation.

Below, animation with awt/swing advancing the box at every
second. It is jagged. For comparison, an animation today
runs between 24 and 60 frames per second!!



The code below - using javafx - rotates a polygon. As is,
I find it dizzying and recommend changing the duration of
one rotation to 5 seconds. Javafx makes it all straightforward.

import javafx.animation.RotateTransition;
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.Polygon;
import javafx.stage.Stage;
import javafx.util.Duration;
        
public class RotateTransitionExample extends Application {
   @Override
   public void start(Stage stage) {     
      //Creating a hexagon
      Polygon hexagon = new Polygon();       
     
      //Adding coordinates to the hexagon
      hexagon.getPoints().addAll(new Double[]{       
         200.0, 50.0,
         400.0, 50.0,
         450.0, 150.0,         
         400.0, 250.0,
         200.0, 250.0,                  
         150.0, 150.0,
      });
      //Setting the fill color for the hexagon
      hexagon.setFill(Color.BLUE);
      
      //Creating a rotate transition   
      RotateTransition rotateTransition = new RotateTransition();
     
      //Setting the duration for the transition
      rotateTransition.setDuration(Duration.millis(5000));
     
      //Setting the node for the transition
      rotateTransition.setNode(hexagon);      
     
      //Setting the angle of the rotation
      rotateTransition.setByAngle(360);
     
      //Setting the cycle count for the transition
      rotateTransition.setCycleCount(50);
     
      //Setting auto reverse value to false
      rotateTransition.setAutoReverse(false);
     
      //Playing the animation
      rotateTransition.play();
        
      //Creating a Group object  
      Group root = new Group(hexagon);
        
      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);  
      //Setting title to the Stage
      stage.setTitle("Rotate 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);
   }
}

https://www.tutorialspoint.com/javafx/javafx_animations.htm

No comments: