diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java new file mode 100755 index 0000000000000000000000000000000000000000..5d6c0f0bf74b14fc4a6cc67a046bec0a0df363a9 --- /dev/null +++ b/src/main/java/lab/DrawingThread.java @@ -0,0 +1,37 @@ +package lab; + +import javafx.animation.AnimationTimer; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; + +public class DrawingThread extends AnimationTimer { + + private final Canvas canvas; + + private final GraphicsContext gc; + + private final World world; + + private long lasttime = -1; + + public DrawingThread(Canvas canvas, World world) { + this.canvas = canvas; + this.gc = canvas.getGraphicsContext2D(); + this.world = world; + } + + /** + * Draws objects into the canvas. Put you code here. + */ + @Override + public void handle(long now) { + gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); + world.draw(gc); + if (lasttime > 0) { + //time are in nanoseconds and method simulate expects seconds + world.simulate((now - lasttime) / 1e9); + } + lasttime = now; + } + +} diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java index c7b578b86edb768bfdc33c65e13c81466ecec266..a13d5192cb718e83d99735c0b14d569253de3d18 100644 --- a/src/main/java/lab/GameController.java +++ b/src/main/java/lab/GameController.java @@ -34,19 +34,7 @@ public class GameController { public void startGame() { this.world = new World(canvas.getWidth(), canvas.getHeight()); //Draw scene on a separate thread to avoid blocking UI. - animationTimer = new AnimationTimer() { - private Long previous; - - @Override - public void handle(long now) { - if (previous == null) { - previous = now; - } else { - drawScene((now - previous)/1e9); - previous = now; - } - } - }; + animationTimer = new DrawingThread(canvas, world); angleSlider.valueProperty().addListener(this::angleChanged); world.setCannonAngle(angleSlider.getValue()); @@ -60,11 +48,6 @@ public class GameController { animationTimer.stop(); } - private void drawScene(double deltaT) { - world.draw(canvas); - world.simulate(deltaT); - } - @FXML private void firePressed() { world.fire(); diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index 58d0489d942a63c58979f2388ca217364380b005..63f811442d9e86a773069679fce9bd94685fb372 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -3,7 +3,6 @@ package lab; import java.util.Random; import javafx.geometry.Point2D; -import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; public class World { @@ -44,9 +43,8 @@ public class World { return new Point2D(worldPoint.getX(), height - worldPoint.getY()); } - public void draw(Canvas canvas) { - GraphicsContext gc = canvas.getGraphicsContext2D(); - gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); + public void draw(GraphicsContext gc) { + gc.clearRect(0, 0, getWidth(), getHeight()); for(DrawableSimulable entity: entities) { entity.draw(gc); }