From 74a8d91c512ba31c8171697dbaaa212ce1d6603a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Mon, 11 Oct 2021 13:36:28 +0200
Subject: [PATCH] Lab05

Use GameController
---
 README.md                             |  0
 src/main/java/lab/App.java            | 46 +++++++--------------------
 src/main/java/lab/GameController.java | 46 +++++++++++++++++++++++++++
 src/main/java/lab/Laboratory.java     | 20 ------------
 4 files changed, 58 insertions(+), 54 deletions(-)
 mode change 100644 => 100755 README.md
 create mode 100644 src/main/java/lab/GameController.java
 delete mode 100644 src/main/java/lab/Laboratory.java

diff --git a/README.md b/README.md
old mode 100644
new mode 100755
diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java
index c94ee8b..7ed50c3 100644
--- a/src/main/java/lab/App.java
+++ b/src/main/java/lab/App.java
@@ -1,6 +1,5 @@
 package lab;
 
-import javafx.animation.AnimationTimer;
 import javafx.application.Application;
 import javafx.scene.Group;
 import javafx.scene.Scene;
@@ -19,39 +18,25 @@ public class App extends Application {
 	}
 	
 	private Canvas canvas;
-	private AnimationTimer animationTimer;
-	private Laboratory lab;
+	private GameController controller;
 	@Override
 	public void start(Stage primaryStage) {
 		try {
 			//Construct a main window with a canvas.  
+			
+			
 			Group root = new Group();
-			canvas = new Canvas(800, 400);
+			canvas = new Canvas(800, 600);
 			root.getChildren().add(canvas);
-			Scene scene = new Scene(root, 800, 400);
-			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
+			Scene scene = new Scene(root);
+			
+			
 			primaryStage.setScene(scene);
 			primaryStage.resizableProperty().set(false);
-			primaryStage.setTitle("Java 1 - 3rd laboratory");
+			primaryStage.setTitle("Java 1 - 6th 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();
+			controller = new GameController(canvas);
+			controller.startGame();
 			//Exit program when main window is closed
 			primaryStage.setOnCloseRequest(this::exitProgram);
 		} catch (Exception e) {
@@ -59,17 +44,10 @@ public class App extends Application {
 		}
 	}
 	
-	/**
-	 * 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();
+		controller.stopGame();
 		System.exit(0);
 	}
 }
\ No newline at end of file
diff --git a/src/main/java/lab/GameController.java b/src/main/java/lab/GameController.java
new file mode 100644
index 0000000..889d6a1
--- /dev/null
+++ b/src/main/java/lab/GameController.java
@@ -0,0 +1,46 @@
+package lab;
+
+import javafx.animation.AnimationTimer;
+import javafx.scene.canvas.Canvas;
+
+public class GameController {
+
+	private World world;
+	private Canvas canvas;
+	private AnimationTimer animationTimer;
+	
+	public GameController(Canvas canvas) {
+		this.canvas = canvas;
+	}
+	
+	public void startGame() {
+		this.world = new World(canvas.getWidth(), canvas.getHeight());	
+		//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();
+	}
+
+
+	public void stopGame() {
+		animationTimer.stop();
+	}
+	
+	private void drawScene(double deltaT) {
+		world.draw(canvas);
+		world.simulate(deltaT);
+	}
+
+	
+}
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);
-		
-	}
-}
-- 
GitLab