Our Feeds

Saturday, 14 January 2017

Ajith KP

Snow Falling Animation in JavaFX

Hi GuyZ,
          This tutorial will show how to create snow falling effect in JavaFX. The first step is creating snow particles. The best shape to create snow particles is circle. So, first we have to create some circles to make them snow particles.

Download Sourcehttps://github.com/ajithkp560/SnowFallingAnimationJavaFX

JavaFX Animation

Code to initialize circle

        Circle c[] = new Circle[2000];
        for (int i = 0; i < 2000; i++) {
            c[i] = new Circle(1, 1, 1);
            c[i].setRadius(random.nextDouble() * 3);
            Color color = Color.rgb(255, 255, 255, random.nextDouble());
            c[i].setFill(color);
            root.getChildren().add(c[i]);
        }

Yeah, we have initialized the circles. Next step is animating them to fall from top to bottom.

Animation code to fall from top to bottom

        Animation fall = TranslateTransitionBuilder.create()
                .node(c)
                .fromY(-200)
                .toY(534+200) //WIndow height = 534
                .toX(random.nextDouble() * c.getCenterX())
                .duration(Duration.seconds(time))
                .onFinished(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        Raining(c);
                    }
                }).build();

Full Source Code

import java.util.Random;
import javafx.animation.Animation;
import javafx.animation.TranslateTransitionBuilder;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author Ajith Kp [ajithkp560] http://www.terminalcoders.blogspot.com
 * http://fb.com/ajithkp560
 *
 */
public class SnowFallingEffect extends Application {

    Random random = new Random();
    AnchorPane root = new AnchorPane();

    @Override
    public void start(Stage primaryStage) {
        Circle c[] = new Circle[2000];
        ImageView background = new ImageView(new Image(getClass().getResource("Backgroundx.png").toString()));
        root.getChildren().add(background);

        for (int i = 0; i < 2000; i++) {
            c[i] = new Circle(1, 1, 1);
            c[i].setRadius(random.nextDouble() * 3);
            Color color = Color.rgb(255, 255, 255, random.nextDouble());
            c[i].setFill(color);
            root.getChildren().add(c[i]);
            Raining(c[i]);
        }
        Scene scene = new Scene(root, 950, 534);
        primaryStage.setTitle("SnowFalling Animation : Ajith Kp");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void Raining(Circle c) {
        c.setCenterX(random.nextInt(950));//Window width = 950
        int time = 10 + random.nextInt(50);
        Animation fall = TranslateTransitionBuilder.create()
                .node(c)
                .fromY(-200)
                .toY(534+200) //WIndow height = 534
                .toX(random.nextDouble() * c.getCenterX())
                .duration(Duration.seconds(time))
                .onFinished(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent t) {
                        Raining(c);
                    }
                }).build();
        fall.play();
    }

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

}



3 comments

Write comments
Unknown
AUTHOR
2 July 2017 at 11:22 delete

This information is of great help. Thanks a lot for writing about it.Browse anime sexy clips and subscribe to be notified of new clips.
Anime Fan Youtube Videos, collection all anime funny, anime funniest video, amv funny , amv sexy, anime sexy and anime Naruto.
amv naruto

Reply
avatar
Anonymous
AUTHOR
30 November 2020 at 09:27 delete

Hello, I cant import class TranslateTransitionBuilder in intellij IDEA. Can you help me?

Reply
avatar
Anonymous
AUTHOR
13 December 2022 at 22:27 delete

Looks very nice. Thank you.

Reply
avatar