import javafx.animation.AnimationTimer; 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; public class Timer 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("Класс AnimationTimer"); root.getChildren().addAll(button1); Scene scene = new Scene(root, 400, 300); button1.setOnAction(event -> { newWindow(); }); stage.setTitle("AnimationTimer"); 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); long[] start = {System.nanoTime()}; AnimationTimer timer = new AnimationTimer() { @Override public void handle(long now) { long t = (now - start[0]) / 1000000; if (t > 40) { System.out.println("Кадр " + now + " - " + t); start[0] = now; } else { System.out.println(t); System.out.printf("%.3f\n", now / 1000000000.0); } if (rect.getTranslateX() > 300.0) { rect.setTranslateX(0.0); } else { rect.setTranslateX(rect.getTranslateX() + 1.0); } } }; timer.start(); pane.getChildren().addAll(rect); Scene scene = new Scene(pane, 500, 300); window.setScene(scene); window.setTitle("AnimationTimer"); window.show(); window.setOnCloseRequest(event -> { if (timer != null) timer.stop(); }); scene.setOnMouseClicked(event -> { timer.stop(); }); } }