From f93d74f54adb6752806d41beb219b9bc3fbf0a45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Fri, 30 Sep 2022 16:18:33 +0200
Subject: [PATCH] Java 17 DrawingThread.

---
 pom.xml                               | 10 ++++----
 src/main/java/lab/DrawingThread.java  | 37 +++++++++++++++++++++++++++
 src/main/java/lab/GameController.java | 20 +--------------
 src/main/java/lab/World.java          |  6 ++---
 4 files changed, 45 insertions(+), 28 deletions(-)
 create mode 100755 src/main/java/lab/DrawingThread.java

diff --git a/pom.xml b/pom.xml
index 8bdcd5d..6bd5f99 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,24 +3,24 @@
 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>vsb-cs-java1</groupId>
-	<artifactId>lab06</artifactId>
+	<artifactId>lab08</artifactId>
 	<version>0.0.1-SNAPHOST</version>
 	<packaging>jar</packaging>
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<maven.compiler.source>11</maven.compiler.source>
-		<maven.compiler.target>11</maven.compiler.target>
+		<maven.compiler.source>17</maven.compiler.source>
+		<maven.compiler.target>17</maven.compiler.target>
 	</properties>
 	<dependencies>
 		<dependency>
 			<groupId>org.openjfx</groupId>
 			<artifactId>javafx-controls</artifactId>
-			<version>11</version>
+			<version>17.0.2</version>
 		</dependency>
 		<dependency>
 			<groupId>org.openjfx</groupId>
 			<artifactId>javafx-fxml</artifactId>
-			<version>11</version>
+			<version>17.0.2</version>
 		</dependency>
 		<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
 		<dependency>
diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java
new file mode 100755
index 0000000..5d6c0f0
--- /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 001db37..1b2763c 100644
--- a/src/main/java/lab/GameController.java
+++ b/src/main/java/lab/GameController.java
@@ -60,7 +60,7 @@ public class GameController {
 		this.world = new World(canvas.getWidth(), canvas.getHeight());
 		this.world.setGameListener(new GameListenerImpl());
 		//Draw scene on a separate thread to avoid blocking UI.
-		animationTimer = new AnimationTimerImpl();
+		animationTimer = new DrawingThread(canvas, world);
 		angleSlider.valueProperty().addListener(this::angleChanged);
 		world.setCannonAngle(angleSlider.getValue());
 		
@@ -74,10 +74,6 @@ public class GameController {
 		animationTimer.stop();
 	}
 	
-	private void drawScene(double deltaT) {
-		world.draw(canvas);
-		world.simulate(deltaT);
-	}
 	
 	@FXML
 	private void firePressed() {
@@ -139,20 +135,6 @@ public class GameController {
 		scoreList.setItems(FXCollections.observableList(highScores));
 	}
 	
-	private final class AnimationTimerImpl extends AnimationTimer {
-		private Long previous;
-
-		@Override
-		public void handle(long now) {
-			if (previous == null) {
-				previous = now;
-			} else {
-				drawScene((now - previous)/1e9);
-				previous = now;
-			}
-		}
-	}
-
 	private class GameListenerImpl implements GameListener {
 
 		@Override
diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java
index 9fb2e23..65dc08f 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 {
@@ -45,9 +44,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);
 		}
-- 
GitLab