From 8f09adb4232e8a4afb86c3708abbbbb4c868ede6 Mon Sep 17 00:00:00 2001 From: PiotrMakarewicz Date: Mon, 21 Dec 2020 12:15:43 +0100 Subject: [PATCH] added simulation canvas --- src/sample/AnimalTile.java | 26 +++++++++++++++ src/sample/AnimalTileFactory.java | 23 +++++++++++++ src/sample/Main.java | 29 ++++++++++++++++- src/sample/SimulationCanvas.java | 32 +++++++++++++++++++ src/sample/SimulationWindowController.java | 4 +++ src/sample/TerrainTile.java | 19 +++++++++++ src/sample/TerrainTileFactory.java | 22 +++++++++++++ src/sample/Tile.java | 7 ++++ .../InvalidStartingParametersException.java | 7 ++++ src/simulation/Jungle.java | 11 ++++--- src/simulation/Simulation.java | 28 +++++++++------- 11 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 src/sample/AnimalTile.java create mode 100644 src/sample/AnimalTileFactory.java create mode 100644 src/sample/SimulationCanvas.java create mode 100644 src/sample/SimulationWindowController.java create mode 100644 src/sample/TerrainTile.java create mode 100644 src/sample/TerrainTileFactory.java create mode 100644 src/sample/Tile.java create mode 100644 src/simulation/InvalidStartingParametersException.java diff --git a/src/sample/AnimalTile.java b/src/sample/AnimalTile.java new file mode 100644 index 0000000..eb5bd6e --- /dev/null +++ b/src/sample/AnimalTile.java @@ -0,0 +1,26 @@ +package sample; + +import javafx.scene.image.Image; + +public enum AnimalTile implements Tile{ + ANIMAL1("images/animal-1.png"), + ANIMAL2("images/animal-2.png"), + ANIMAL3("images/animal-3.png"), + ANIMAL4("images/animal-4.png"), + ANIMAL5("images/animal-5.png"), + ANIMAL6("images/animal-6.png"), + ANIMAL7("images/animal-7.png"), + ANIMAL8("images/animal-8.png"), + ANIMAL9("images/animal-9.png"), + ANIMAL10("images/animal-10.png"); + + private final String imagePath; + + AnimalTile(String imagePath){ + this.imagePath = imagePath; + } + + public Image getImage(){ + return new Image(this.imagePath); + } +} diff --git a/src/sample/AnimalTileFactory.java b/src/sample/AnimalTileFactory.java new file mode 100644 index 0000000..49656dc --- /dev/null +++ b/src/sample/AnimalTileFactory.java @@ -0,0 +1,23 @@ +package sample; + +import simulation.Simulation; + +public class AnimalTileFactory{ + private Simulation simulation; + AnimalTileFactory(Simulation simulation){ + this.simulation = simulation; + } + public TerrainTile getTile(int x, int y){ + if (simulation.plantBoard.isPlanted(x,y)){ + if (simulation.jungle.contains(x,y)) + return TerrainTile.JUNGLEPLANTED; + else return TerrainTile.SAVANNAHPLANTED; + } + else{ + if(simulation.jungle.contains(x,y)) + return TerrainTile.JUNGLE; + else return TerrainTile.SAVANNAH; + } + } +} + diff --git a/src/sample/Main.java b/src/sample/Main.java index d6945e3..af9e3cb 100644 --- a/src/sample/Main.java +++ b/src/sample/Main.java @@ -3,19 +3,46 @@ import javafx.application.Application; import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; +import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.Button; +import javafx.scene.paint.Color; import javafx.stage.Stage; import simulation.Simulation; +import simulation.SimulationErrorException; public class Main extends Application { @Override public void start(Stage stage) throws Exception{ - + Simulation s1 = new Simulation("Symulacja",50,50,0.2,12,22,13,200); + displaySimulation(s1,stage); } + public void displaySimulation(Simulation simulation, Stage stage) throws InterruptedException, SimulationErrorException { + Group root = new Group(); + double width = simulation.getWidth()*10; + double height = simulation.getHeight()*10; + Scene simulationScene = new Scene(root, width, height, Color.BLACK); + + final SimulationCanvas canvas = new SimulationCanvas(simulation); + + root.getChildren().add(canvas); + stage.setScene(simulationScene); + stage.setResizable(false); + simulation.start(); + stage.show(); + + for (int i = 0; i<100; i++){ + canvas.update(); + Thread.sleep(1000); + simulation.simulateOneDay(); + } + + } public static void main(String[] args) { launch(args); diff --git a/src/sample/SimulationCanvas.java b/src/sample/SimulationCanvas.java new file mode 100644 index 0000000..2443ca5 --- /dev/null +++ b/src/sample/SimulationCanvas.java @@ -0,0 +1,32 @@ +package sample; + +import javafx.scene.canvas.Canvas; +import simulation.Simulation; + +public class SimulationCanvas extends Canvas { + private final Simulation simulation; + private final TerrainTileFactory terrainTileFactory; + private final AnimalTileFactory animalTileFactory; + private final int tileSize = 5; + SimulationCanvas(Simulation simulation){ + super(simulation.getWidth()*10,simulation.getHeight()*10); + this.simulation = simulation; + this.terrainTileFactory = new TerrainTileFactory(simulation); + this.animalTileFactory = new AnimalTileFactory(simulation); + } + public void update(){ + for (int i = 0; i