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 c94ee8b5e8b68ccaab4d4c0718cb9da3cc59af5f..7ed50c35fc919c24ce2a4005b8b102bb62547418 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 0000000000000000000000000000000000000000..889d6a1525bf0e419a03681359103201ee90e569 --- /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 c45aa390eb2764617432b913606fc69bed8d265b..0000000000000000000000000000000000000000 --- 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); - - } -}