From 73867e2bcdf7ef7f94b4e8c6438a3fe8021cd210 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Mon, 29 Aug 2022 11:42:05 +0200
Subject: [PATCH] Use Java version 17.

Use AnimationTimer instead of common Thread.
---
 pom.xml                              |  8 ++++----
 src/main/java/lab/App.java           | 27 +++++++++++----------------
 src/main/java/lab/DrawingThread.java | 27 +++++++++++++++++++++++++++
 src/main/java/module-info.java       |  1 +
 4 files changed, 43 insertions(+), 20 deletions(-)
 create mode 100644 src/main/java/lab/DrawingThread.java

diff --git a/pom.xml b/pom.xml
index a346419..06892d2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,19 +8,19 @@
 	<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/App.java b/src/main/java/lab/App.java
index 8ff0082..858f494 100644
--- a/src/main/java/lab/App.java
+++ b/src/main/java/lab/App.java
@@ -1,7 +1,7 @@
 package lab;
 
+import javafx.animation.AnimationTimer;
 import javafx.application.Application;
-import javafx.application.Platform;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.canvas.Canvas;
@@ -20,6 +20,7 @@ public class App extends Application {
 	}
 	
 	private Canvas canvas;
+	private AnimationTimer timer;
 	
 	@Override
 	public void start(Stage primaryStage) {
@@ -37,25 +38,19 @@ 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();
+			//graphic context is used for a painting
+			GraphicsContext gc = canvas.getGraphicsContext2D();
+		
+			timer = new DrawingThread(gc);
+			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();
-		// put your code here
-		// gc.setFill(Color.AQUA);
-		// gc.setStroke(Color.BLACK);
+	@Override
+	public void stop() throws Exception {
+		timer.stop();
+		super.stop();
 	}
 	
 	private void exitProgram(WindowEvent evt) {
diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java
new file mode 100644
index 0000000..5d52cfd
--- /dev/null
+++ b/src/main/java/lab/DrawingThread.java
@@ -0,0 +1,27 @@
+package lab;
+
+import javafx.animation.AnimationTimer;
+import javafx.scene.canvas.GraphicsContext;
+import javafx.scene.paint.Color;
+
+public class DrawingThread extends AnimationTimer {
+
+	private final GraphicsContext gc;
+
+	public DrawingThread(GraphicsContext gc) {
+		this.gc = gc;
+	}
+
+	/**
+	  * Draws objects into the canvas. Put you code here. 
+	 */
+	@Override
+	public void handle(long now) {
+		// put your code here
+		gc.setFill(Color.AQUA);
+		gc.setStroke(Color.BLACK);
+		gc.fillOval(10, 10, 20, 20);
+
+	}
+
+}
diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java
index 7006c78..736971c 100644
--- a/src/main/java/module-info.java
+++ b/src/main/java/module-info.java
@@ -1,6 +1,7 @@
 module lab01 {
     requires transitive javafx.controls;
     requires javafx.fxml;
+    requires javafx.base;
     opens lab to javafx.fxml;
     exports lab;
 }
\ No newline at end of file
-- 
GitLab