JavaFX animations 🎬 with source code






package javafx_animation;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.animation.*;
import javafx.scene.Group;
import javafx.util.Duration;
/**
 *
 * @author wajid
 */
public class JavaFX_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
       Circle circle = new Circle(50);
        circle.setFill(new LinearGradient(0, 0, 1, 1, true, CycleMethod.REFLECT,
                new Stop(0, Color.DEEPSKYBLUE), new Stop(1, Color.DARKBLUE)));
       
    DropShadow shadow = new DropShadow(30, Color.DARKBLUE);
    shadow.setSpread(0.5);
    circle.setEffect(shadow);
    
    Group root = new Group(circle);
    Scene scene = new Scene(root, 600, 400, Color.LIGHTGOLDENRODYELLOW);
    primaryStage.setScene(scene);
    primaryStage.show();
    
    
    PathTransition path = new PathTransition();
    path.setNode(circle);
    path.setDuration(Duration.seconds(4));
    path.setPath(new Path(
                            new MoveTo(100, 200),
    
                            new CubicCurveTo(300, 50, 500, 350, 500, 200)
    ));
    path.setInterpolator(Interpolator.EASE_BOTH);
    path.setCycleCount(Animation.INDEFINITE);
    path.setAutoReverse(true);
    
    RotateTransition rotate = new RotateTransition(Duration.seconds(2), circle);
    rotate.setByAngle(360);
    rotate.setInterpolator(Interpolator.LINEAR);
    rotate.setCycleCount(Animation.INDEFINITE);
    ScaleTransition scale = new ScaleTransition(Duration.seconds(1.5), circle);
    scale.setByY(0.5);
    scale.setByX(0.5);
    scale.setAutoReverse(true);
    scale.setCycleCount(Animation.INDEFINITE);
    
    ParallelTransition combo = new ParallelTransition(path, rotate, scale);
    combo.play();
    
    path.currentTimeProperty().addListener((obs, old, time) -> 
            shadow.setRadius(60 + Math.abs(Math.sin(time.toSeconds() * Math.PI)) * 60));
    
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Comments

Popular posts from this blog

JavaFX UI /UX Dashboard

JavaFX code for responsive layout