From a101a49c13781875d962a328479cb4f1177fd981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Mon, 19 Sep 2022 14:04:24 +0200 Subject: [PATCH] Pass canvas to DrawingThread --- src/main/java/lab/App.java | 5 +---- src/main/java/lab/DrawingThread.java | 15 +++++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 858f494..df4c9d7 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -38,10 +38,7 @@ public class App extends Application { //Exit program when main window is closed primaryStage.setOnCloseRequest(this::exitProgram); - //graphic context is used for a painting - GraphicsContext gc = canvas.getGraphicsContext2D(); - - timer = new DrawingThread(gc); + timer = new DrawingThread(canvas); timer.start(); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java index 5d52cfd..63badd7 100644 --- a/src/main/java/lab/DrawingThread.java +++ b/src/main/java/lab/DrawingThread.java @@ -1,15 +1,18 @@ package lab; import javafx.animation.AnimationTimer; +import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; -import javafx.scene.paint.Color; public class DrawingThread extends AnimationTimer { + private final Canvas canvas; private final GraphicsContext gc; - public DrawingThread(GraphicsContext gc) { - this.gc = gc; + public DrawingThread(Canvas canvas) { + this.canvas = canvas; + this.gc = canvas.getGraphicsContext2D(); + } /** @@ -18,9 +21,9 @@ public class DrawingThread extends AnimationTimer { @Override public void handle(long now) { // put your code here - gc.setFill(Color.AQUA); - gc.setStroke(Color.BLACK); - gc.fillOval(10, 10, 20, 20); + //gc.setFill(Color.AQUA); + //gc.setStroke(Color.BLACK); + //gc.fillOval(10, 10, 20, 20); } -- GitLab