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

second gui version (draw random tiles 20 * 20)

parent db999af7
No related branches found
No related tags found
No related merge requests found
package controller; package controller;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
import main.MainApp; import main.MainApp;
import view.GridTileCanvas;
public class RootLayoutController { public class RootLayoutController {
@FXML @FXML
GridPane gridPane; public GridTileCanvas gridTilePane;
private MainApp mainApp; private MainApp mainApp;
/** /**
* Is called by the main application to give a reference back to itself. * Is called by the main application to give a reference back to itself.
* *
* @param mainApp * @param mainApp the main app
*/ */
public void setMainApp(MainApp mainApp) { public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp; this.mainApp = mainApp;
} }
} }
...@@ -7,6 +7,9 @@ import javafx.scene.Scene; ...@@ -7,6 +7,9 @@ import javafx.scene.Scene;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.TileGrid;
import model.Tile;
import model.TileGame;
import java.io.IOException; import java.io.IOException;
...@@ -14,9 +17,14 @@ public class MainApp extends Application { ...@@ -14,9 +17,14 @@ public class MainApp extends Application {
private Stage primaryStage; private Stage primaryStage;
private BorderPane rootLayout; private BorderPane rootLayout;
private TileGame tileGame = new TileGame();
public MainApp() {
}
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp"); this.primaryStage.setTitle("Wang tiles");
initRootLayout(); initRootLayout();
} }
......
package model; package model;
import view.PointType;
public enum CardinalDirection { public enum CardinalDirection {
NORTH, EAST, SOUTH, WEST; NORTH(PointType.NORTH_WEST_CORNER, PointType.NORTH_EAST_CORNER),
EAST(PointType.NORTH_EAST_CORNER, PointType.SOUTH_EAST_CORNER),
SOUTH(PointType.SOUTH_EAST_CORNER, PointType.SOUTH_WEST_CORNER),
WEST(PointType.NORTH_WEST_CORNER, PointType.SOUTH_WEST_CORNER);
private final PointType firstPoint;
private final PointType secondPoint;
CardinalDirection(PointType firstPoint, PointType secondPoint) {
this.firstPoint = firstPoint;
this.secondPoint = secondPoint;
}
public PointType getFirstPoint() {
return firstPoint;
}
public PointType getSecondPoint() {
return secondPoint;
}
} }
...@@ -2,12 +2,12 @@ package model; ...@@ -2,12 +2,12 @@ package model;
import java.util.Iterator; import java.util.Iterator;
public class GridIterator<T> implements Iterator<T> { public class GridIterator implements Iterator<Tile> {
private int rowIndex; private int rowIndex;
private int columnIndex; private int columnIndex;
private final Grid<T> grid; private final TileGrid grid;
GridIterator(Grid grid) { GridIterator(TileGrid grid) {
this.rowIndex = 0; this.rowIndex = 0;
this.columnIndex = 0; this.columnIndex = 0;
this.grid = grid; this.grid = grid;
...@@ -19,8 +19,8 @@ public class GridIterator<T> implements Iterator<T> { ...@@ -19,8 +19,8 @@ public class GridIterator<T> implements Iterator<T> {
} }
@Override @Override
public T next() { public Tile next() {
final T result = grid.getSquare(rowIndex, columnIndex); final Tile result = grid.getTile(rowIndex, columnIndex);
columnIndex = (columnIndex +1) % grid.getNumberOfColumns(); columnIndex = (columnIndex +1) % grid.getNumberOfColumns();
if(columnIndex == 0){ if(columnIndex == 0){
rowIndex++; rowIndex++;
......
package model;
import javafx.scene.paint.Color;
import java.util.List;
import java.util.Random;
public class RandomTileGenerator {
private final List<Color> colors;
private final Random randomGenerator;
public RandomTileGenerator(List<Color> colors, Random randomGenerator) {
this.colors = colors;
this.randomGenerator = randomGenerator;
}
private Color randomColor(){
return colors.get(randomGenerator.nextInt(colors.size()));
}
public Tile nextTile(){
Color[] colorsOfTile = new Color[4];
for(int index = 0; index < 4; index++)
colorsOfTile[index] = randomColor();
return new Tile(colorsOfTile);
}
}
package model;
import javafx.scene.paint.Color;
import java.util.List;
import java.util.Random;
public class TileGame {
private final TileGrid tileGrid;
public TileGame(TileGrid tileGrid) {
this.tileGrid = tileGrid;
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.WHITE, Color.BLACK, Color.RED), new Random());
}
public TileGame() {
this(new TileGrid(10, 10));
}
public TileGrid getTileGrid() {
return tileGrid;
}
}
package model; package model;
import javafx.scene.paint.Color;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Grid<T> implements Iterable<T> { public class TileGrid implements Iterable<Tile> {
private final int numberOfRows; private final int numberOfRows;
private final int numberOfColumns; private final int numberOfColumns;
private final T[][] squares; private final Tile[][] tiles;
/** /**
* Creates a new {@code Grid} instance given the number of rows and columns. * Creates a new {@code Grid} instance given the number of rows and columns.
...@@ -15,14 +19,14 @@ public class Grid<T> implements Iterable<T> { ...@@ -15,14 +19,14 @@ public class Grid<T> implements Iterable<T> {
* @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are * @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are
* less than or equal to 0 * less than or equal to 0
*/ */
public Grid(int numberOfRows, int numberOfColumns) { public TileGrid(int numberOfRows, int numberOfColumns) {
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns; this.numberOfColumns = numberOfColumns;
this.squares = allocateMatrix(); this.tiles = allocateMatrix();
} }
private T[][] allocateMatrix() { private Tile[][] allocateMatrix() {
return (T[][]) new Object[getNumberOfRows()][getNumberOfColumns()]; return new Tile[getNumberOfRows()][getNumberOfColumns()];
} }
...@@ -32,12 +36,12 @@ public class Grid<T> implements Iterable<T> { ...@@ -32,12 +36,12 @@ public class Grid<T> implements Iterable<T> {
* @return an Iterator. * @return an Iterator.
*/ */
@Override @Override
public Iterator<T> iterator() { public Iterator<Tile> iterator() {
return new GridIterator<>(this); return new GridIterator(this);
} }
public T getSquare(int rowIndex, int columnIndex) { public Tile getTile(int rowIndex, int columnIndex) {
return squares[rowIndex][columnIndex]; return tiles[rowIndex][columnIndex];
} }
public int getNumberOfRows() { public int getNumberOfRows() {
...@@ -47,4 +51,12 @@ public class Grid<T> implements Iterable<T> { ...@@ -47,4 +51,12 @@ public class Grid<T> implements Iterable<T> {
public int getNumberOfColumns() { public int getNumberOfColumns() {
return numberOfColumns; return numberOfColumns;
} }
public void fillWithRandomTiles(List<Color> colors, Random randomGenerator){
RandomTileGenerator randomTileGenerator = new RandomTileGenerator(colors, randomGenerator);
for(int row = 0; row < numberOfRows; row++)
for(int column = 0; column < numberOfColumns; column++){
tiles[row][column] = randomTileGenerator.nextTile();
}
}
} }
package view;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import model.CardinalDirection;
import model.Tile;
import model.TileGrid;
import java.util.List;
import java.util.Random;
public class GridTileCanvas extends Canvas {
private static final double TILE_WIDTH = 50;
private static final double TILE_HEIGHT = 50;
private static final int NUMBER_OF_ROWS = 20;
private static final int NUMBER_OF_COLUMNS = 20;
public GridTileCanvas() {
this.setWidth(TILE_WIDTH * NUMBER_OF_COLUMNS);
this.setHeight(TILE_HEIGHT * NUMBER_OF_ROWS);
TileGrid tileGrid = new TileGrid(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS);
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.GREEN, Color.BLACK, Color.RED), new Random());
drawGridTile(tileGrid);
}
public void drawGridTile(TileGrid tileGrid){
for(int row = 0; row < tileGrid.getNumberOfRows(); row++)
for(int column = 0; column < tileGrid.getNumberOfColumns(); column++){
drawTile(tileGrid.getTile(row, column), column, row);
}
}
public void drawTile(Tile tile, int row, int column){
for(CardinalDirection cardinalDirection: CardinalDirection.values()){
drawTriangle(column, row, cardinalDirection, tile.getColor(cardinalDirection));
}
}
private double getXPosition(int column, PointType pointType){
return (column + pointType.getXPosition()) * TILE_WIDTH;
}
private double getYPosition(int row, PointType pointType){
return (row + pointType.getYPosition()) * TILE_HEIGHT;
}
private void drawTriangle(int row, int column, CardinalDirection cardinalDirection, Color color){
double xFirstPoint = getXPosition(column, cardinalDirection.getFirstPoint());
double yFirstPoint = getYPosition(row, cardinalDirection.getFirstPoint());
double xSecondPoint = getXPosition(column, cardinalDirection.getSecondPoint());
double ySecondPoint = getYPosition(row, cardinalDirection.getSecondPoint());
double xCenter = getXPosition(column, PointType.CENTER);
double yCenter = getYPosition(row, PointType.CENTER);
GraphicsContext graphicsContext = getGraphicsContext2D();
graphicsContext.setFill(color);
graphicsContext.fillPolygon(new double[]{xFirstPoint, xSecondPoint, xCenter},
new double[]{yFirstPoint, ySecondPoint, yCenter}, 3);
}
}
package view;
public enum PointType {
NORTH_WEST_CORNER(0,0),
NORTH_EAST_CORNER(1,0),
SOUTH_WEST_CORNER(0,1),
SOUTH_EAST_CORNER(1,1),
CENTER(0.5,0.5);
private final double xPosition;
private final double yPosition;
PointType(double xPosition, double yPosition) {
this.xPosition = xPosition;
this.yPosition = yPosition;
}
public double getXPosition() {
return xPosition;
}
public double getYPosition() {
return yPosition;
}
}
...@@ -3,17 +3,16 @@ ...@@ -3,17 +3,16 @@
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<BorderPane prefHeight="620.0" <?import view.GridTileCanvas?>
prefWidth="620.0" <BorderPane prefHeight="900.0"
prefWidth="900.0"
stylesheets="@DarkTheme.css" 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.RootLayoutController">
<top> <GridTileCanvas xmlns="http://javafx.com/javafx"
<GridPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:id="gridPane" fx:id="gridTilePane"
prefHeight="600.0" prefWidth="600.0"> height="1000.0" width="1000.0">
</GridPane> </GridTileCanvas>
</top>
</BorderPane> </BorderPane>
\ 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