-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbouncinglogo.java
More file actions
167 lines (130 loc) · 4.26 KB
/
bouncinglogo.java
File metadata and controls
167 lines (130 loc) · 4.26 KB
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
///usr/bin/env echo '
/**** BOOTSTRAP jbang ****'>/dev/null
command -v jbang >/dev/null 2>&1 || curl -Ls https://sh.jbang.dev | bash -s app setup
exec `$SHELL -c "which jbang"` "$0" "$@" ; exit $?
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/
//DEPS org.openjfx:javafx-controls:17.0.2:${os.detected.jfxname}
//DEPS org.openjfx:javafx-graphics:17.0.2:${os.detected.jfxname}
//JAVA 11+
//FILES images/jbang-icon.png
import java.io.IOException;
import javafx.animation.AnimationTimer;
import javafx.animation.ScaleTransition;
import javafx.application.Application;
import javafx.scene.CacheHint;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.ColorInput;
import javafx.scene.effect.Effect;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
References:
https://www.tutorialspoint.com/javafx/javafx_images.htm
https://www.tutorialspoint.com/javafx/javafx_animations.htm
*/
public class bouncinglogo extends Application {
private Scene scene;
private ImageView logo;
private Image image;
private double t = 0;
private AnimationTimer timer;
private boolean movingUP, movingLeft;
int xspeed = 3;
int yspeed = 5;
Image loadImage(String name) throws IOException {
return new Image(getClass().getResource(name).openStream());
}
@Override
public void start(Stage stage) throws IOException {
//Creating an image
image = loadImage("jbang-icon.png");
//Setting the image view
logo = new ImageView(image);
//Setting the position of the image
logo.setX(300);
logo.setY(300);
//setting the fit height and width of the image view
// imageView.setFitHeight(455);
// imageView.setFitWidth(500);
logo.setFitHeight(200);
logo.setFitWidth(200);
//Setting the preserve ratio of the image view
logo.setPreserveRatio(true);
grow();
//Creating a Group object
Group root = new Group(logo);
//Creating a scene object
scene = new Scene(root, 1200, 800);
//Setting title to the Stage
stage.setTitle("JBang for a better java");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
private void updateImage() {
ColorAdjust effect = new ColorAdjust();
effect.setSaturation(Math.random());
effect.setHue(Math.random());
effect.setContrast(Math.random());
logo.setEffect(effect);
logo.setCache(true);
logo.setCacheHint(CacheHint.SPEED);
}
private void grow() {
//Creating scale Transition
ScaleTransition scaleTransition = new ScaleTransition();
scaleTransition.setOnFinished(e -> {
updateImage();
move();
});
//Setting the duration for the transition
scaleTransition.setDuration(Duration.millis(1500));
//Setting the node for the transition
scaleTransition.setNode(logo);
//Setting the dimensions for scaling
scaleTransition.setByY(0.5);
scaleTransition.setByX(0.5);
//Setting the cycle count for the translation
// scaleTransition.setCycleCount(3);
scaleTransition.setCycleCount(1);
//Setting auto reverse value to true
scaleTransition.setAutoReverse(false);
//Playing the animation
scaleTransition.play();
}
private void move() {
timer = new AnimationTimer() {
public void handle(long now) {
t += 0.02;
if (t > 0.02) { // the lower the fastest
logo.setY(logo.getY()+yspeed);
logo.setX(logo.getX()+xspeed);
checkhitbox();
t = 0;
}
}
};
timer.start();
}
private void checkhitbox() {
if(logo.getX()+logo.getFitWidth() >= scene.getWidth() || logo.getX()<=0) {
xspeed *= -1;
updateImage();
}
if(logo.getY()+logo.getFitHeight() >= scene.getHeight() || logo.getY()<=0) {
yspeed *= -1;
updateImage();
}
}
public static void main(String args[]) {
launch(args);
}
}