From cfcfa80db00352f8e904a324a1c38c80b83b2f27 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Fri, 30 Sep 2022 11:36:24 +0200
Subject: [PATCH] Refactor and use AnimationTimer.

---
 src/main/java/lab/App.java           | 36 ++++++---------------------
 src/main/java/lab/DrawingThread.java | 37 ++++++++++++++++++++++++++++
 src/main/java/lab/Laboratory.java    | 20 ---------------
 src/main/java/lab/World.java         |  6 ++---
 4 files changed, 46 insertions(+), 53 deletions(-)
 create mode 100755 src/main/java/lab/DrawingThread.java
 delete mode 100644 src/main/java/lab/Laboratory.java

diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java
index c94ee8b..7c1e3bc 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 0000000..bac2377
--- /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 c45aa39..0000000
--- 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 ce631f9..c531c51 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) {
-- 
GitLab