diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index c94ee8b5e8b68ccaab4d4c0718cb9da3cc59af5f..7c1e3bc0770fec772fa457ee73e91f4512d89a27 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -19,8 +19,9 @@ public class App extends Application { } private Canvas canvas; - private AnimationTimer animationTimer; - private Laboratory lab; + + private AnimationTimer timer; + @Override public void start(Stage primaryStage) { try { @@ -32,44 +33,21 @@ public class App extends Application { scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.resizableProperty().set(false); - primaryStage.setTitle("Java 1 - 3rd laboratory"); + primaryStage.setTitle("Java 1 - 4th laboratory"); primaryStage.show(); - lab = new Laboratory(canvas); - - //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.start(); //Exit program when main window is closed primaryStage.setOnCloseRequest(this::exitProgram); + timer = new DrawingThread(canvas); + timer.start(); + //Draw scene on a separate thread to avoid blocking UI. } catch (Exception e) { e.printStackTrace(); } } - /** - * Draws objects into the canvas. Put you code here. - * - *@return nothing - */ - private void drawScene(double deltaT) { - lab.draw(deltaT); - } private void exitProgram(WindowEvent evt) { - animationTimer.stop(); System.exit(0); } } \ No newline at end of file diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java new file mode 100755 index 0000000000000000000000000000000000000000..bac23778ca78f5778c11626505ba7a4420db604e --- /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) { + this.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) { + 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/Laboratory.java b/src/main/java/lab/Laboratory.java deleted file mode 100644 index c45aa390eb2764617432b913606fc69bed8d265b..0000000000000000000000000000000000000000 --- a/src/main/java/lab/Laboratory.java +++ /dev/null @@ -1,20 +0,0 @@ -package lab; - -import javafx.scene.canvas.Canvas; - -public class Laboratory { - - private World world; - private Canvas canvas; - - public Laboratory(Canvas canvas) { - this.canvas = canvas; - this.world = new World(canvas.getWidth(), canvas.getHeight()); - } - - public void draw(double deltaT) { - world.draw(canvas); - world.simulate(deltaT); - - } -} diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java index ce631f92042437d374ab6c311c2778450a88858c..c531c516d183793f4bc8eb3c381b6856c967e25f 100644 --- a/src/main/java/lab/World.java +++ b/src/main/java/lab/World.java @@ -1,7 +1,6 @@ package lab; import javafx.geometry.Point2D; -import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; public class World { @@ -26,9 +25,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, width, height); cannon.draw(gc); bulletAnimatted.draw(gc); for(Dragon dragon: dragons) {