From f319b003e78a8321e8632f6cead7c67fa186fb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Fri, 23 Sep 2022 14:30:02 +0200 Subject: [PATCH] Use AnimationTimer. --- src/main/java/lab/App.java | 26 ++++++------------- src/main/java/lab/Bullet.java | 34 +++++++++++++++++++++++++ src/main/java/lab/DrawingThread.java | 32 +++++++++++++++++++++++ src/main/java/lab/World.java | 38 ++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 18 deletions(-) create mode 100644 src/main/java/lab/Bullet.java create mode 100644 src/main/java/lab/DrawingThread.java create mode 100644 src/main/java/lab/World.java diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 210b19f..60a6231 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -1,10 +1,10 @@ package lab; +import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; import javafx.stage.Stage; import javafx.stage.WindowEvent; @@ -19,6 +19,7 @@ public class App extends Application { } private Canvas canvas; + private AnimationTimer timer; @Override public void start(Stage primaryStage) { @@ -36,27 +37,16 @@ public class App extends Application { //Exit program when main window is closed primaryStage.setOnCloseRequest(this::exitProgram); - - //Draw scene on a separate thread to avoid blocking UI. - new Thread(this::drawScene).start(); + timer = new DrawingThread(canvas); + timer.start(); } catch (Exception e) { e.printStackTrace(); } } - - /** - * Draws objects into the canvas. Put you code here. - * - *@return nothing - */ - private void drawScene() { - //graphic context is used for a painting - GraphicsContext gc = canvas.getGraphicsContext2D(); - int timeout = 10; - while (!Routines.isEndOfThreadRequestedByJavaVM()) { - Routines.sleep(timeout); - double deltaT = timeout / 1000.; - } + @Override + public void stop() throws Exception { + timer.stop(); + super.stop(); } private void exitProgram(WindowEvent evt) { diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java new file mode 100644 index 0000000..6710390 --- /dev/null +++ b/src/main/java/lab/Bullet.java @@ -0,0 +1,34 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class Bullet { + private World world; + private Point2D velocity; + private double size; + private Point2D position; + public Bullet(World world, Point2D startPosition, Point2D velocity, double size) { + super(); + this.world = world; + this.velocity = velocity; + this.size = size; + this.position = startPosition; + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.BROWN); + Point2D p = world.getCanvasPoint(position); + gc.fillOval(p.getX() - size /2, p.getY() - size/2, size, size); + gc.restore(); + } + + public void simulate(double deltaT) { + // TODO Auto-generated method stub + position = position.add(velocity.multiply(deltaT)); + } + + +} diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java new file mode 100644 index 0000000..4a7519f --- /dev/null +++ b/src/main/java/lab/DrawingThread.java @@ -0,0 +1,32 @@ +package lab; + +import javafx.animation.AnimationTimer; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; + +public class DrawingThread extends AnimationTimer { + + private final GraphicsContext gc; + private final World world; + private long lastTime; + + public DrawingThread(Canvas canvas) { + this.gc = canvas.getGraphicsContext2D(); + this.world = new World(canvas.getWidth(), canvas.getHeight()); + } + + /** + * Draws objects into the canvas. Put you code here. + */ + @Override + public void handle(long now) { + if (lastTime > 0) { + double deltaT = (now - lastTime) / 1e9; + world.simulate(deltaT); + } + world.draw(gc); + lastTime= now; + + } + +} diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java new file mode 100644 index 0000000..8cef4e9 --- /dev/null +++ b/src/main/java/lab/World.java @@ -0,0 +1,38 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class World { + + private double width; + + private double height; + + private Bullet bullet; + + + public World(double width, double height) { + this.width = width; + this.height = height; + this.bullet = new Bullet(this, new Point2D(10, 10), new Point2D(10, 30), 15); + } + + public Point2D getCanvasPoint(Point2D in) { + Point2D result = new Point2D(in.getX(), height - in.getY()); + return result; + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.LIGHTGREY); + gc.fillRect(0, 0, width, height); + gc.restore(); + bullet.draw(gc); + } + + public void simulate(double deltaT) { + this.bullet.simulate(deltaT); + } +} -- GitLab