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
| package test;
import javafx.animation.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Optional;
final public class Main extends Application {
final double width = 1000;
final double height = 800;
private Circle sprite;
private Transition currentAnimation;
private Timeline currentTimeline;
public static void main(final String... args) {
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
sprite = new Circle(10);
final var animationPane = new Pane() {
{
setMinWidth(width);
setMinHeight(height);
setPrefWidth(width);
setPrefHeight(height);
sprite.setFill(Color.RED);
sprite.setCenterX(0.5 * width);
sprite.setCenterY(0.1 * height);
getChildren().setAll(sprite);
}
};
final var transitionPropertiesButton = new Button("Transition - translate properties");
transitionPropertiesButton.setOnAction(event -> {
stopAndReset();
final var seg1 = new TranslateTransition();
seg1.setNode(sprite);
seg1.setToX(-0.4 * width);
seg1.setToY(0.4 * height);
seg1.setDuration(Duration.seconds(2));
final var seg2 = new TranslateTransition();
seg2.setNode(sprite);
seg2.setToX(0);
seg2.setToY(0.8 * height);
seg2.setDuration(Duration.seconds(2));
final var seg3 = new TranslateTransition();
seg3.setNode(sprite);
seg3.setToX(0.4 * width);
seg3.setToY(0.4 * height);
seg3.setDuration(Duration.seconds(2));
final var seg4 = new TranslateTransition();
seg4.setNode(sprite);
seg4.setToX(0);
seg4.setToY(0);
seg4.setDuration(Duration.seconds(2));
final var animation = new SequentialTransition();
animation.getChildren().setAll(seg1, seg2, seg3, seg4);
animation.setCycleCount(Animation.INDEFINITE);
currentAnimation = animation;
Platform.runLater(animation::play);
});
final var timelinePropertiesButton = new Button("Timeline - center properties");
timelinePropertiesButton.setOnAction(event -> {
stopAndReset();
final var timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.ZERO,
new KeyValue(sprite.centerXProperty(), 0.5 * width),
new KeyValue(sprite.centerYProperty(), 0.1 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(2),
new KeyValue(sprite.centerXProperty(), 0.1 * width),
new KeyValue(sprite.centerYProperty(), 0.5 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(4),
new KeyValue(sprite.centerXProperty(), 0.5 * width),
new KeyValue(sprite.centerYProperty(), 0.9 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(6),
new KeyValue(sprite.centerXProperty(), 0.9 * width),
new KeyValue(sprite.centerYProperty(), 0.5 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(8),
new KeyValue(sprite.centerXProperty(), 0.5 * width),
new KeyValue(sprite.centerYProperty(), 0.1 * height)));
timeline.setCycleCount(Timeline.INDEFINITE);
currentTimeline = timeline;
Platform.runLater(currentTimeline::play);
});
final var timelineTransformButton = new Button("Timeline - transform");
timelineTransformButton.setOnAction(event -> {
stopAndReset();
final var transform = new Translate();
sprite.getTransforms().setAll(transform);
final var timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.ZERO,
new KeyValue(transform.xProperty(), 0),
new KeyValue(transform.yProperty(), 0)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(2),
new KeyValue(transform.xProperty(), -0.4 * width),
new KeyValue(transform.yProperty(), 0.4 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(4),
new KeyValue(transform.xProperty(), 0),
new KeyValue(transform.yProperty(), 0.8 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(6),
new KeyValue(transform.xProperty(), 0.4 * width),
new KeyValue(transform.yProperty(), 0.4 * height)));
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(8),
new KeyValue(transform.xProperty(), 0),
new KeyValue(transform.yProperty(), 0)));
timeline.setCycleCount(Timeline.INDEFINITE);
currentTimeline = timeline;
Platform.runLater(currentTimeline::play);
});
final var stopButton = new Button("Stop");
stopButton.setOnAction(event -> stopAndReset());
final var toolBar = new ToolBar();
toolBar.getItems().setAll(transitionPropertiesButton, timelinePropertiesButton, timelineTransformButton, stopButton);
final var root = new BorderPane();
root.setTop(toolBar);
root.setCenter(animationPane);
final var scene = new Scene(root);
stage.setTitle("Test");
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
private void stopAndReset() {
Optional.ofNullable(currentAnimation)
.ifPresent(Transition::stop);
currentAnimation = null;
Optional.ofNullable(currentTimeline)
.ifPresent(Timeline::stop);
currentTimeline = null;
sprite.setTranslateX(0);
sprite.setTranslateY(0);
sprite.setCenterX(0.5 * width);
sprite.setCenterY(0.1 * height);
sprite.getTransforms().clear();
}
} |
Partager