Skip to content
Snippets Groups Projects
Commit 094538f2 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Changed configuration such that the size of the grid and the dimension of...

Changed configuration such that the size of the grid and the dimension of tiles is defined in the fxml  file.
parent 1d767a80
Branches
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ import javafx.fxml.FXML; ...@@ -4,7 +4,7 @@ import javafx.fxml.FXML;
import main.MainApp; import main.MainApp;
import view.GridTileCanvas; import view.GridTileCanvas;
public class RootLayoutController { public class MainPaneController {
@FXML @FXML
public GridTileCanvas GridTileCanvas; public GridTileCanvas GridTileCanvas;
......
package main; package main;
import controller.RootLayoutController; import controller.MainPaneController;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.TileGrid;
import model.Tile;
import model.TileGame; import model.TileGame;
import java.io.IOException; import java.io.IOException;
public class MainApp extends Application { public class MainApp extends Application {
private Stage primaryStage; private Stage primaryStage;
private BorderPane rootLayout; private AnchorPane rootLayout;
private TileGame tileGame = new TileGame(); private TileGame tileGame = new TileGame();
...@@ -35,7 +33,7 @@ public class MainApp extends Application { ...@@ -35,7 +33,7 @@ public class MainApp extends Application {
public void initRootLayout() { public void initRootLayout() {
try { try {
// Load root layout from fxml file. // Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/RootLayout.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/GridCanvas.fxml"));
rootLayout = loader.load(); rootLayout = loader.load();
// Show the scene containing the root layout. // Show the scene containing the root layout.
...@@ -43,7 +41,7 @@ public class MainApp extends Application { ...@@ -43,7 +41,7 @@ public class MainApp extends Application {
primaryStage.setScene(scene); primaryStage.setScene(scene);
// Give the controller access to the main app. // Give the controller access to the main app.
RootLayoutController controller = loader.getController(); MainPaneController controller = loader.getController();
controller.setMainApp(this); controller.setMainApp(this);
primaryStage.show(); primaryStage.show();
......
package view; package view;
import javafx.beans.NamedArg;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
...@@ -10,15 +11,23 @@ import java.util.List; ...@@ -10,15 +11,23 @@ import java.util.List;
import java.util.Random; import java.util.Random;
public class GridTileCanvas extends Canvas { public class GridTileCanvas extends Canvas {
private static final double TILE_WIDTH = 50;
private static final double TILE_HEIGHT = 50; public final double tileWidth;
private static final int NUMBER_OF_ROWS = 20; private final double tileHeight;
private static final int NUMBER_OF_COLUMNS = 20; private final Integer numberOfColumns;
private final Integer numberOfRows;
public GridTileCanvas() {
this.setWidth(TILE_WIDTH * NUMBER_OF_COLUMNS); public GridTileCanvas(@NamedArg("tileWidth") Double tileWidth,
this.setHeight(TILE_HEIGHT * NUMBER_OF_ROWS); @NamedArg("tileHeight") Double tileHeight,
TileGrid tileGrid = new TileGrid(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS); @NamedArg("numberOfColumns") Integer numberOfColumns,
@NamedArg("numberOfRows") Integer numberOfRows) {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
this.setWidth(tileWidth * numberOfColumns);
this.setHeight(tileHeight * numberOfRows);
TileGrid tileGrid = new TileGrid(numberOfRows, numberOfColumns);
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.GREEN, Color.BLACK, Color.RED), new Random()); tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.GREEN, Color.BLACK, Color.RED), new Random());
drawGridTile(tileGrid); drawGridTile(tileGrid);
} }
...@@ -37,10 +46,10 @@ public class GridTileCanvas extends Canvas { ...@@ -37,10 +46,10 @@ public class GridTileCanvas extends Canvas {
} }
private double getXPosition(int column, PointType pointType){ private double getXPosition(int column, PointType pointType){
return (column + pointType.getXPosition()) * TILE_WIDTH; return (column + pointType.getXPosition()) * tileWidth;
} }
private double getYPosition(int row, PointType pointType){ private double getYPosition(int row, PointType pointType){
return (row + pointType.getYPosition()) * TILE_HEIGHT; return (row + pointType.getYPosition()) * tileHeight;
} }
private void drawTriangle(int row, int column, CardinalDirection side, Color color){ private void drawTriangle(int row, int column, CardinalDirection side, Color color){
......
...@@ -4,15 +4,26 @@ ...@@ -4,15 +4,26 @@
<?import view.GridTileCanvas?> <?import view.GridTileCanvas?>
<BorderPane prefHeight="900.0" <?import java.lang.Double?>
prefWidth="900.0" <?import java.lang.Integer?>
stylesheets="@DarkTheme.css" <AnchorPane stylesheets="@DarkTheme.css"
xmlns="http://javafx.com/javafx" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:controller="controller.RootLayoutController"> fx:controller="controller.MainPaneController">
<GridTileCanvas xmlns="http://javafx.com/javafx" <GridTileCanvas xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:id="GridTileCanvas" fx:id="GridTileCanvas">
height="1000.0" width="1000.0"> <tileHeight>
<Double fx:value="5"/>
</tileHeight>
<tileWidth>
<Double fx:value="5"/>
</tileWidth>
<numberOfColumns>
<Integer fx:value="100"/>
</numberOfColumns>
<numberOfRows>
<Integer fx:value="100"/>
</numberOfRows>
</GridTileCanvas> </GridTileCanvas>
</BorderPane> </AnchorPane>
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment