This post covers two different types of JavaFX Animation Example.

JavaFX has great media library and that is used to create beautiful animation.You can also customize and use features to create awesome JavaFX Animation.

JavaFX Animation Example 1

This example uses timeline to play the animation.And different shapes are used to give effect of animation. FadeTransition also used make animation beautiful.X coordinate is changes with time and set for arc which creates movement of shape.To make it more beautiful drop shadow effect also given.

Here is output that you will get when you run code given below:

Source Code to get Above animation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package puretechy.javafxAnimation;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.shape.*;
 
public class AnimatedManDemo extends Application
{
        public static void main(String[] args) {
               launch(args);
           }
        @Override
           public void start(Stage primaryStage)
           {
 
               Pane p=new Pane();
               Pane p1=new Pane();
               Pane hb=new Pane();
               Group objects = new Group();
 
 Scene scene = new Scene(objects, 1700, 650,Color.BLACK);
 
        Line line = new Line();
        line.setStartX(20);
        line.setStartY(480);
        line.setEndX(60);
        line.setEndY(480);
        line.setStroke(Color.WHITE);
        line.setStrokeWidth(5);
        p.getChildren().add(line);
 
         Line line1 = new Line(40, 460, 40, 500);
         line1.setStroke(Color.WHITE);
        line1.setStrokeWidth(5);
        p.getChildren().add(line1);
 
         Line line2 = new Line(20,540 ,40, 500);
         line2.setStroke(Color.WHITE);
        line2.setStrokeWidth(5);
        p.getChildren().add(line2);
 
         Line line3 = new Line(60,540 ,40, 500);
         line3.setStroke(Color.WHITE);
        line3.setStrokeWidth(5);
        p.getChildren().add(line3);
 
         Circle circle=new Circle(40, 440, 20, Color.WHITE);
         circle.setStrokeType(StrokeType.INSIDE);
         circle.setStroke(Color.WHITE);
         circle.setStrokeWidth(1);
         p.getChildren().add(circle);
 
           double x=0,y=0;
           for(int i=0;i<100;i++)
           {
                 x=x+15;
                 y= Math.random()*300;
                 Circle s1=new Circle(x, y, 2, Color.WHITE);
 
                 s1.setStrokeType(StrokeType.INSIDE);
                 s1.setStroke(Color.WHITE);
                 s1.setStrokeWidth(0.1);
               p1.getChildren().add(s1);
           }
 
           DropShadow ds1 = new DropShadow();
           ds1.setOffsetY(1);
           ds1.setOffsetX(1);
           ds1.setColor(Color.LIGHTGREEN);
 
           double radiusX=0;
           x=0;
           for(int i=0;i<50;i++)
           {
 
                 y= Math.random()*300;
                 y=y+400;
                 radiusX= Math.random()*100;
                Arc arc = new Arc(x, y, radiusX, 50, 0, 100);
                x=x+30;
           arc.setFill(Color.GREEN);
           arc.setType(ArcType.OPEN);
           arc.setStrokeWidth(1);
           arc.setStroke(Color.CHARTREUSE);
           arc.setStrokeType(StrokeType.INSIDE);
           arc.setStrokeWidth(10);
           arc.setEffect(ds1);
           hb.getChildren().add(arc);
 
 
           }
 
           objects.getChildren().add(p);
           objects.getChildren().add(hb);
           objects.getChildren().add(p1);
 
           FadeTransition ft = new FadeTransition(Duration.millis(200), p1);
           ft.setFromValue(1.0);
           ft.setToValue(0.5);
           ft.setCycleCount(Timeline.INDEFINITE);
           ft.setAutoReverse(true);
 
           ft.play();
 
 
           Timeline timeline = new Timeline();
           timeline.setCycleCount(Timeline.INDEFINITE);
           timeline.setAutoReverse(true);
           final KeyValue kv = new KeyValue( p.layoutXProperty(), 1500);
           final KeyFrame kf = new KeyFrame(Duration.millis(10000), kv);
           timeline.getKeyFrames().add(kf);
           timeline.play();
 
        primaryStage.setScene(scene);
            primaryStage.show();
 
           }
}
}

JavaFX Animation Example 2

This program uses Polygon objects to create triangle shape. Runnable is used to create thread. Fade Transition used to generate animation. Dropshdow is used to give more beautiful look.

Here is output that you will get when you run code given below:

Source Code to get Above animation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package puretechy.javafxAnimation;
 
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;
import javafx.util.Duration;
 
public class AnimationDemoFX extends Application {
       @Override
       public void start(Stage primaryStage) {
              try {
                     BorderPane root = new BorderPane();
                     Scene scene = new Scene(root,500,400,Color.ROYALBLUE);
                     primaryStage.setScene(scene);
                     primaryStage.setTitle("PureTechy.com");
              Group g = new Group();
 
 
               DropShadow ds1 = new DropShadow();
                      ds1.setOffsetY(4.0f);
                      ds1.setOffsetX(4.0f);
                      ds1.setColor(Color.YELLOW);
 
                      Circle c = new Circle();
                      c.setEffect(ds1);
                      c.setCenterX(50);
                      c.setCenterY(150);
                      c.setRadius(30.0f);
                      c.setFill(Color.ORANGE);
                    //  c.setCache(true);
 
                      DropShadow ds2 = new DropShadow();
                      ds2.setOffsetY(4.0f);
                      ds2.setOffsetX(4.0f);
                      ds2.setColor(Color.WHITE);
 
                      Circle c1 = new Circle();
                      c1.setEffect(ds2);
                      c1.setCenterX(50);
                      c1.setCenterY(150);
                      c1.setRadius(30.0f);
                      c1.setFill(Color.BLACK);
 
                      FadeTransition ft = new FadeTransition(Duration.millis(5000), c1);
                           ft.setFromValue(1.0);//Specifies the start opacity value for this FadeTransition
                           ft.setToValue(0);
                           ft.setCycleCount(Timeline.INDEFINITE);
                           ft.setAutoReverse(true);
                           scene.setFill(Color.DARKRED);
                           //ft.play();
                           g.getChildren().add(c);
                         g.getChildren().add(c1);     
 
 
               Polygon triangle = new Polygon();
               triangle.getPoints().setAll(
                             00d, 400d,
                             150d, 400d,
                             70d, 350d
                         );
 
               triangle.setStroke(Color.DARKGREEN);
                  triangle.setStrokeWidth(4);
                  triangle.setStrokeLineCap(StrokeLineCap.ROUND);
                  triangle.setFill(Color.FORESTGREEN.deriveColor(0, 1.2, 1, 0.6));
 
 
                  Polygon triangle1 = new Polygon();
                      triangle1.getPoints().setAll(
                                    70d, 400d,
                                    350d, 400d,
                                    210d, 270d
                                );
 
                      triangle1.setStroke(Color.DARKGREEN);
                         triangle1.setStrokeWidth(4);
                         triangle1.setStrokeLineCap(StrokeLineCap.ROUND);
                         triangle1.setFill(Color.FORESTGREEN.deriveColor(0, 1.2, 1, 0.6));
 
                         Polygon triangle2 = new Polygon();
                             triangle2.getPoints().setAll(
                                           330d, 400d,
                                           500d, 400d,
                                           415d, 300d
                                       );
 
                             triangle2.setStroke(Color.DARKGREEN);
                                triangle2.setStrokeWidth(4);
                                triangle2.setStrokeLineCap(StrokeLineCap.ROUND);
                                triangle2.setFill(Color.FORESTGREEN.deriveColor(0, 1.2, 1, 0.6));
 
                  g.getChildren().add(triangle);
                  g.getChildren().add(triangle1);
                  g.getChildren().add(triangle2);
                  Line l1=new Line(50,50,70,70);
                     Line l2=new Line(70,70,90,50);
 
                     Line l3=new Line(350,150,380,180);
                     Line l4=new Line(380,180,410,150);
           final Button runButton = new Button("Run");
 
        runButton.setOnAction(new EventHandler()
        {
 
 
              boolean b=true;
                     @Override
                     public void handle(ActionEvent arg0) {
                           // TODO Auto-generated method stub
 
                           Thread t = new Thread(new Runnable() {
                                  @Override
                                  public void run()
                                  {
 
                                         while (true)
                                         {
 
 
                                                Platform.runLater(new Runnable()
                                                {
                                                       @Override    
                                                       public void run()
                                                       {
                                                              ft.play();
 
                                         if(b)
                                         {
                                                b=false;
                                                l1.setStartY(47);
                                                l2.setEndY(47);
 
 
                                                l3.setStartY(147);
                                                l4.setEndY(147);
                                         }
                                         else
 
                                         {
                                                b=true;
                                                l1.setStartY(50);
                                                l2.setEndY(50);
 
                                                l3.setStartY(150);
                                                l4.setEndY(150);
                                         }
 
 
 
 
 
 
                                                       }
                                                });
                                                try {
                                                       Thread.sleep(250);
                                                } catch (InterruptedException ex) {
                                                       break;
                                                }
                                         }
                                  }
                           });
                           t.setName("Runnable Time Updater");
                           t.setDaemon(true);
                           t.start();
 
                     }
        });
 
                     g.getChildren().add(l1);
                   g.getChildren().add(l2);
                   g.getChildren().add(l3);
                   g.getChildren().add(l4);
                   //   g.getChildren().add(cubicCurve2);
                   //   g.getChildren().add(cubicCurve3);
                      g.getChildren().add(runButton);
 
                      root.getChildren().add(g);
                      primaryStage.show();
 
              } catch(Exception e) {
                     e.printStackTrace();
              }
       }
 
       public static void main(String[] args) {
              launch(args);
       }
}
}

Hope You Enjoyed the post of JavaFX Animation.Let us know your views in comments section.
You might be interested in other JavaFX Examples.Check Out Image Editor and Sine Wave using cubic curve.

Save Lots of $$ with Udemy coupon code 100% Off !

About

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

%d bloggers like this: