From 555cdf0301cd4706ea31ee129bd211a2e7e66eac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Fri, 30 Sep 2022 11:35:32 +0200
Subject: [PATCH] Pass GraphicsContext to World.

---
 src/main/java/lab/DrawingThread.java |  4 ++--
 src/main/java/lab/World.java         | 23 +++++++++++------------
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java
index af51eac..bac2377 100755
--- a/src/main/java/lab/DrawingThread.java
+++ b/src/main/java/lab/DrawingThread.java
@@ -17,7 +17,7 @@ public class DrawingThread extends AnimationTimer {
 	public DrawingThread(Canvas canvas) {
 		this.canvas = canvas;
 		this.gc = canvas.getGraphicsContext2D();
-		this.world = new World(canvas);
+		this.world = new World(canvas.getWidth(), canvas.getHeight());
 	}
 
 	/**
@@ -26,7 +26,7 @@ public class DrawingThread extends AnimationTimer {
 	@Override
 	public void handle(long now) {
 		gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
-		world.draw();
+		world.draw(gc);
 		if (lasttime > 0) {
 			//time are in nanoseconds and method simulate expects seconds
 			world.simulate((now - lasttime) / 1e9);
diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java
index c83521d..32e2906 100644
--- a/src/main/java/lab/World.java
+++ b/src/main/java/lab/World.java
@@ -1,28 +1,27 @@
 package lab;
 
 import javafx.geometry.Point2D;
-import javafx.scene.canvas.Canvas;
 import javafx.scene.canvas.GraphicsContext;
 
 public class World {
+	private double width;
+	private double height;
 	private BulletAnimated bulletAnimatted;
 	private Cannon cannon;
-	private final Canvas canvas;
-
-	public World(Canvas canvas) {
-		super();
-		this.canvas = canvas;
+	
+	public World(double width, double height) {
+		this.width = width;
+		this.height = height;
 		cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
 		bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
 	}
 
 	public Point2D getCanvasPoint(Point2D worldPoint) {
-		return new Point2D(worldPoint.getX(), canvas.getHeight() - worldPoint.getY());
+		return new Point2D(worldPoint.getX(), height - worldPoint.getY());
 	}
 
-	public void draw() {
-		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);
 	}
@@ -33,11 +32,11 @@ public class World {
 	}
 
 	public double getWidth() {
-		return canvas.getWidth();
+		return width;
 	}
 
 	public double getHeight() {
-		return canvas.getHeight();
+		return height;
 	}
 
 }
-- 
GitLab