-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAnimatedShadowTest.java
More file actions
80 lines (64 loc) Β· 3.05 KB
/
AnimatedShadowTest.java
File metadata and controls
80 lines (64 loc) Β· 3.05 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
package eu.iamgio.animatedtest;
import eu.iamgio.animated.binding.Animated;
import eu.iamgio.animated.binding.presets.AnimatedDropShadow;
import eu.iamgio.animated.common.Curve;
import eu.iamgio.animated.transition.AnimatedLabel;
import eu.iamgio.animated.transition.AnimationPair;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import static eu.iamgio.animatedtest.TestUtil.*;
// This demo animates a button's shadow radius and color on pressed and hover events.
// An animated text (via AnimatedSwitcher) flags the button's status as well.
public class AnimatedShadowTest extends Application {
private static final int RADIUS_PRESSED = 100, RADIUS_HOVER = 60, RADIUS_DEFAULT = 20;
private static final Color COLOR_PRESSED = Color.SLATEBLUE, COLOR_HOVER = Color.CRIMSON, COLOR_DEFAULT = Color.TEAL;
private static final String TEXT_PRESSED = "PRESSED", TEXT_HOVER = "HOVER", TEXT_DEFAULT = "";
public void start(Stage primaryStage) {
// Setup scene
Pane root = new Pane();
Scene scene = new Scene(root, SCENE_WIDTH, SCENE_HEIGHT);
// Create button
Button button = new Button("Animated shadow");
center(button, scene);
// Setup animated text label
AnimatedLabel label = new AnimatedLabel(AnimationPair.fade().setSpeed(1, 2));
label.setLabelFactory(text -> {
Label newLabel = new Label(text);
newLabel.prefWidthProperty().bind(scene.widthProperty());
newLabel.setAlignment(Pos.CENTER);
newLabel.setStyle("-fx-font-size: 18; -fx-padding: 60");
return newLabel;
});
// Create effect
DropShadow shadow = new DropShadow();
shadow.setRadius(0);
button.setEffect(shadow);
// Set listeners
button.pressedProperty().addListener((observable, oldValue, isPressed) -> {
shadow.setRadius(isPressed ? RADIUS_PRESSED : RADIUS_HOVER);
shadow.setColor(isPressed ? COLOR_PRESSED : COLOR_HOVER);
label.setText(isPressed ? TEXT_PRESSED : TEXT_HOVER);
});
button.hoverProperty().addListener((observable, oldValue, isHover) -> {
shadow.setRadius(isHover ? RADIUS_HOVER : RADIUS_DEFAULT);
shadow.setColor(isHover ? COLOR_HOVER : COLOR_DEFAULT);
label.setText(isHover ? TEXT_HOVER : TEXT_DEFAULT);
});
// Setup animation
Animated animated = new Animated(button, new AnimatedDropShadow.Color(), new AnimatedDropShadow.Radius())
.custom(settings -> settings.withDuration(Duration.millis(200)).withCurve(Curve.EASE_OUT_SINE));
root.getChildren().addAll(label, animated);
// Show
primaryStage.setTitle("AnimatedDropShadow");
primaryStage.setScene(scene);
primaryStage.show();
}
}