import java.util.ArrayList; import javafx.animation.Animation; import javafx.animation.Interpolator; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class TimeLn extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) throws Exception { VBox root = new VBox(10); root.setAlignment(Pos.CENTER); Button button1 = new Button("Класс Timeline"); Button button2 = new Button("Класс KeyFrame"); Button button3 = new Button("Класс KeyValue"); root.getChildren().addAll(button1, button2, button3); Scene scene = new Scene(root, 400, 300); button1.setOnAction(event -> { newWindow(); }); button2.setOnAction(event -> { newWindow2(); }); button3.setOnAction(event -> { newWindow3(); }); stage.setTitle("Timeline"); stage.setScene(scene); stage.show(); } public void newWindow() { Stage window = new Stage(); Pane pane = new Pane(); Rectangle rect = new Rectangle(50.0, 50.0); rect.setFill(Color.RED); rect.relocate(50.0, 50.0); KeyFrame key1 = new KeyFrame(Duration.ZERO, new KeyValue(rect.opacityProperty(), 1.0)); KeyFrame key2 = new KeyFrame(Duration.seconds(2.0), new KeyValue(rect.opacityProperty(), 0.3)); Timeline tm = new Timeline(key1, key2); tm.setCycleCount(10); tm.setAutoReverse(true); tm.setOnFinished(event -> { System.out.println("Анимация завершена"); }); System.out.println(tm.getTargetFramerate()); // 60.0 tm.play(); Rectangle rect2 = new Rectangle(50.0, 50.0); rect2.setFill(Color.BLUE); rect2.relocate(50.0, 150.0); KeyFrame key3 = new KeyFrame(Duration.ZERO, new KeyValue(rect2.translateXProperty(), 0.0)); KeyFrame key4 = new KeyFrame(Duration.seconds(4.0), new KeyValue(rect2.translateXProperty(), 300.0)); /*Timeline tm2 = new Timeline(30.0, key3, key4); Timeline tm2 = new Timeline(30.0); System.out.println(tm2.getTargetFramerate()); // 30.0 */ Timeline tm2 = new Timeline(); tm2.getKeyFrames().addAll(key3, key4); tm2.setCycleCount(10); tm2.setAutoReverse(true); tm2.setOnFinished(event -> { System.out.println("Анимация2 завершена"); }); tm2.play(); pane.getChildren().addAll(rect, rect2); Scene scene = new Scene(pane, 500, 300); window.setScene(scene); window.setTitle("Timeline"); window.show(); window.setOnCloseRequest(event -> { if (tm != null) tm.stop(); if (tm2 != null) tm2.stop(); }); scene.setOnMouseClicked(event -> { if (tm2.getStatus() == Animation.Status.RUNNING) { tm2.stop(); } else { tm2.play(); } }); } public void newWindow2() { Stage window = new Stage(); Pane pane = new Pane(); Rectangle rect = new Rectangle(50.0, 50.0); rect.setFill(Color.RED); rect.relocate(50.0, 50.0); KeyFrame key1 = new KeyFrame(Duration.ZERO, "Первый кадр", new KeyValue(rect.opacityProperty(), 1.0), new KeyValue(rect.translateXProperty(), 0.0)); KeyFrame key2 = new KeyFrame(Duration.seconds(4.0), //"Последний кадр", event -> { System.out.println("key2 OnFinished"); }, new KeyValue(rect.opacityProperty(), 0.3), new KeyValue(rect.translateXProperty(), 300.0)); /*System.out.println(key1.getName()); System.out.println(key2.getName()); System.out.println(key1.getTime()); System.out.println(key1.getValues()); System.out.println(key1.getOnFinished());*/ Timeline tm = new Timeline(key1, key2); tm.setCycleCount(10); tm.setAutoReverse(true); tm.play(); Rectangle rect2 = new Rectangle(50.0, 50.0); rect2.setFill(Color.BLUE); rect2.relocate(50.0, 150.0); KeyFrame key3 = new KeyFrame(Duration.ZERO, new KeyValue(rect2.translateXProperty(), 0.0)); ArrayList list = new ArrayList<>(); list.add(new KeyValue(rect2.translateXProperty(), 150.0)); KeyFrame key4 = new KeyFrame(Duration.seconds(2.0), "Средний кадр", event -> { System.out.println("key4 OnFinished"); }, list ); KeyFrame key5 = new KeyFrame(Duration.seconds(4.0), event -> { System.out.println("key5 OnFinished"); }, new KeyValue(rect2.translateXProperty(), 300.0)); Timeline tm2 = new Timeline(); tm2.getKeyFrames().addAll(key3, key4, key5); tm2.setCycleCount(10); tm2.setAutoReverse(true); tm2.setOnFinished(event -> { System.out.println("Анимация2 завершена"); }); tm2.play(); pane.getChildren().addAll(rect, rect2); Scene scene = new Scene(pane, 500, 300); window.setScene(scene); window.setTitle("KeyFrame"); window.show(); window.setOnCloseRequest(event -> { if (tm != null) tm.stop(); if (tm2 != null) tm2.stop(); }); } public void newWindow3() { Stage window = new Stage(); Pane pane = new Pane(); Rectangle rect = new Rectangle(50.0, 50.0); rect.setFill(Color.RED); rect.relocate(50.0, 50.0); KeyValue kv1 = new KeyValue(rect.translateXProperty(), 0.0); KeyValue kv2 = new KeyValue(rect.translateXProperty(), 300.0, Interpolator.EASE_OUT); KeyFrame key1 = new KeyFrame(Duration.ZERO, kv1); KeyFrame key2 = new KeyFrame(Duration.seconds(3.0), kv2); /*System.out.println(kv2.getEndValue()); System.out.println(kv2.getInterpolator()); System.out.println(kv2.getTarget().getValue());*/ Timeline tm = new Timeline(key1, key2); tm.setCycleCount(10); tm.setAutoReverse(true); tm.play(); pane.getChildren().addAll(rect); Scene scene = new Scene(pane, 500, 300); window.setScene(scene); window.setTitle("KeyValue"); window.show(); window.setOnCloseRequest(event -> { if (tm != null) tm.stop(); }); } }