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

New version with the draw method not in the Tile

parent dc9bf4f2
No related branches found
No related tags found
No related merge requests found
Showing
with 126 additions and 99 deletions
package controller;
import javafx.fxml.FXML;
import javafx.scene.paint.Color;
import model.RandomWangConstrainedTileGenerator;
import view.GridTileCanvas;
import java.util.List;
import java.util.Random;
public class GridController {
@FXML
public GridTileCanvas gridTileCanvas;
public void initialize() {
gridTileCanvas.initializeGrid(new RandomWangConstrainedTileGenerator(List.of(Color.BLUE, Color.RED, Color.BLACK, Color.GREEN),
new Random(0)));
}
public void draw() {
gridTileCanvas.drawGrid();
}
}
package controller;
import javafx.fxml.FXML;
import main.MainApp;
import view.GridTileCanvas;
public class MainPaneController {
@FXML
public GridTileCanvas gridTileCanvas;
private MainApp mainApp;
public void initializeGrid() {
gridTileCanvas.initializeGrid();
}
}
package main; package main;
import controller.MainPaneController; import controller.GridController;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
...@@ -39,9 +39,9 @@ public class MainApp extends Application { ...@@ -39,9 +39,9 @@ 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.
MainPaneController controller = loader.getController(); GridController controller = loader.getController();
controller.initializeGrid(); controller.initialize();
controller.draw();
primaryStage.show(); primaryStage.show();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -7,7 +7,7 @@ import java.util.List; ...@@ -7,7 +7,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
public class SquareGrid implements Iterable<Square> { public class Grid implements Iterable<Square> {
private final int numberOfRows; private final int numberOfRows;
private final int numberOfColumns; private final int numberOfColumns;
private final Square[][] squares; private final Square[][] squares;
...@@ -20,7 +20,7 @@ public class SquareGrid implements Iterable<Square> { ...@@ -20,7 +20,7 @@ public class SquareGrid implements Iterable<Square> {
* @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 SquareGrid(int numberOfRows, int numberOfColumns) { public Grid(int numberOfRows, int numberOfColumns) {
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns; this.numberOfColumns = numberOfColumns;
squares = new Square[getNumberOfRows()][getNumberOfColumns()]; squares = new Square[getNumberOfRows()][getNumberOfColumns()];
...@@ -64,7 +64,7 @@ public class SquareGrid implements Iterable<Square> { ...@@ -64,7 +64,7 @@ public class SquareGrid implements Iterable<Square> {
return new SquareIterator(this); return new SquareIterator(this);
} }
public Optional<SquareTile> getTile(int rowIndex, int columnIndex) { public Optional<Tile> getTile(int rowIndex, int columnIndex) {
return getSquare(rowIndex, columnIndex).getSquareTile(); return getSquare(rowIndex, columnIndex).getSquareTile();
} }
...@@ -79,10 +79,9 @@ public class SquareGrid implements Iterable<Square> { ...@@ -79,10 +79,9 @@ public class SquareGrid implements Iterable<Square> {
return numberOfColumns; return numberOfColumns;
} }
public void fillWithRandomTiles(List<Color> colors, Random randomGenerator){ public void fill(TileGenerator tileGenerator){
RandomWangTileGenerator randomTileGenerator = new RandomWangTileGenerator(colors, randomGenerator);
for(Square square : this) for(Square square : this)
square.add(randomTileGenerator.nextTileWithNeighborhoodConstraint(square)); square.add(tileGenerator.nextTile(square));
} }
public boolean hasTile(int row, int column) { public boolean hasTile(int row, int column) {
......
...@@ -6,12 +6,12 @@ import java.util.List; ...@@ -6,12 +6,12 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
public class RandomWangTileGenerator implements SquareTileGenerator{ public class RandomWangConstrainedTileGenerator implements TileGenerator {
private final List<Color> colors; private final List<Color> colors;
private final Random randomGenerator; private final Random randomGenerator;
public RandomWangTileGenerator(List<Color> colors, Random randomGenerator) { public RandomWangConstrainedTileGenerator(List<Color> colors, Random randomGenerator) {
this.colors = colors; this.colors = colors;
this.randomGenerator = randomGenerator; this.randomGenerator = randomGenerator;
} }
...@@ -35,13 +35,13 @@ public class RandomWangTileGenerator implements SquareTileGenerator{ ...@@ -35,13 +35,13 @@ public class RandomWangTileGenerator implements SquareTileGenerator{
return new WangTile(colorsOfTile); return new WangTile(colorsOfTile);
} }
@Override @Override
public SquareTile nextTileWithNeighborhoodConstraint(Square square) { public Tile nextTile(Square square) {
Color[] colorsOfTile = new Color[4]; Color[] colorsOfTile = new Color[4];
for(CardinalDirection side : CardinalDirection.values()){ for(CardinalDirection side : CardinalDirection.values()){
Optional<SquareTile> optionalSquareTile = square.getNeighboringSquare(side); Optional<Tile> optionalSquareTile = square.getNeighboringSquare(side);
if(optionalSquareTile.isPresent()){ if(optionalSquareTile.isPresent()){
SquareTile neighboringSquareTile = optionalSquareTile.get(); Tile neighboringTile = optionalSquareTile.get();
colorsOfTile[side.ordinal()] = neighboringSquareTile.getSideColor(side.oppositeDirection()); colorsOfTile[side.ordinal()] = neighboringTile.getSideColor(side.oppositeDirection());
} }
else { else {
colorsOfTile[side.ordinal()] = randomColor(); colorsOfTile[side.ordinal()] = randomColor();
......
package model;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class RotatedTile implements Tile {
@Override
public boolean isCompatibleWith(Tile neighbor, CardinalDirection side) {
return false;
}
@Override
public Tile rotation(RotationType rotation) {
return null;
}
@Override
public Color getSideColor(CardinalDirection side) {
return null;
}
}
...@@ -3,11 +3,11 @@ package model; ...@@ -3,11 +3,11 @@ package model;
import java.util.Optional; import java.util.Optional;
public class Square { public class Square {
private SquareTile squareTile; private Tile tile;
private Square[] neighboringSquares; private Square[] neighboringSquares;
private Square(SquareTile squareTile, Square[] neighboringSquares) { private Square(Tile tile, Square[] neighboringSquares) {
this.squareTile = squareTile; this.tile = tile;
this.neighboringSquares = neighboringSquares; this.neighboringSquares = neighboringSquares;
} }
...@@ -16,21 +16,21 @@ public class Square { ...@@ -16,21 +16,21 @@ public class Square {
} }
public Optional<SquareTile> getSquareTile(){ public Optional<Tile> getSquareTile(){
if(!hasTile()) if(!hasTile())
return Optional.empty(); return Optional.empty();
return Optional.of(squareTile); return Optional.of(tile);
} }
public void add(SquareTile tile) { public void add(Tile tile) {
this.squareTile = tile; this.tile = tile;
} }
public boolean hasTile() { public boolean hasTile() {
return squareTile != null; return tile != null;
} }
public Optional<SquareTile> getNeighboringSquare(CardinalDirection side) { public Optional<Tile> getNeighboringSquare(CardinalDirection side) {
if (neighboringSquares[side.ordinal()] == null) if (neighboringSquares[side.ordinal()] == null)
return Optional.empty(); return Optional.empty();
return neighboringSquares[side.ordinal()].getSquareTile(); return neighboringSquares[side.ordinal()].getSquareTile();
......
package model;
public class SquareGridCoordinates {
private int rowIndex;
private int columnIndex;
public SquareGridCoordinates(int rowIndex, int columnIndex) {
this.rowIndex = rowIndex;
this.columnIndex = columnIndex;
}
public SquareGridCoordinates(){
this(0, 0);
}
}
...@@ -5,9 +5,9 @@ import java.util.Iterator; ...@@ -5,9 +5,9 @@ import java.util.Iterator;
public class SquareIterator implements Iterator<Square> { public class SquareIterator implements Iterator<Square> {
private int rowIndex; private int rowIndex;
private int columnIndex; private int columnIndex;
private final SquareGrid grid; private final Grid grid;
SquareIterator(SquareGrid grid) { SquareIterator(Grid grid) {
this.rowIndex = 0; this.rowIndex = 0;
this.columnIndex = 0; this.columnIndex = 0;
this.grid = grid; this.grid = grid;
......
package model;
public interface SquareTileGenerator {
SquareTile nextTile();
SquareTile nextTileWithNeighborhoodConstraint(Square square);
SquareTile nextUniformTile();
}
...@@ -3,9 +3,8 @@ package model; ...@@ -3,9 +3,8 @@ package model;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
public interface SquareTile { public interface Tile {
void draw(GraphicsContext graphicsContext, double x, double y, double width, double height); boolean isCompatibleWith(Tile neighbor, CardinalDirection side);
boolean isCompatibleWith(SquareTile neighbor, CardinalDirection side); Tile rotation(RotationType rotation);
SquareTile rotation(RotationType rotation);
Color getSideColor(CardinalDirection side); Color getSideColor(CardinalDirection side);
} }
package model;
public interface TileGenerator {
Tile nextTile(Square square);
}
...@@ -6,7 +6,7 @@ import javafx.scene.paint.Color; ...@@ -6,7 +6,7 @@ import javafx.scene.paint.Color;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class WangTile implements SquareTile { public class WangTile implements Tile {
private static final int NUMBER_OF_SIDES = 4; private static final int NUMBER_OF_SIDES = 4;
private final Color[] colors = new Color[NUMBER_OF_SIDES]; private final Color[] colors = new Color[NUMBER_OF_SIDES];
...@@ -34,33 +34,17 @@ public class WangTile implements SquareTile { ...@@ -34,33 +34,17 @@ public class WangTile implements SquareTile {
return Arrays.hashCode(colors); return Arrays.hashCode(colors);
} }
@Override
public void draw(GraphicsContext graphicsContext, double x, double y, double width, double height) {
for(CardinalDirection side: CardinalDirection.values()){
drawSideTriangle(graphicsContext, x, y, width, height, side);
}
}
@Override @Override
public boolean isCompatibleWith(SquareTile neighbor, CardinalDirection side) { public boolean isCompatibleWith(Tile neighbor, CardinalDirection side) {
return false; return false;
} }
@Override @Override
public SquareTile rotation(RotationType rotation) { public Tile rotation(RotationType rotation) {
return null; return null;
} }
private void drawSideTriangle(GraphicsContext graphicsContext, double x, double y, double width, double height,
CardinalDirection side){
List<PointType> cornerTypes = PointType.trianglePointTypes(side);
double[] xPoints = new double[3];
double[] yPoints = new double[3];
for(int index = 0; index < 3; index++){
xPoints[index] = x + (cornerTypes.get(index).getXPosition() * width);
yPoints[index] = y + (cornerTypes.get(index).getYPosition() * height);
}
graphicsContext.setFill(getSideColor(side));
graphicsContext.fillPolygon(xPoints, yPoints, 3);
}
} }
...@@ -16,7 +16,8 @@ public class GridTileCanvas extends Canvas { ...@@ -16,7 +16,8 @@ public class GridTileCanvas extends Canvas {
private final double tileHeight; private final double tileHeight;
private final Integer numberOfColumns; private final Integer numberOfColumns;
private final Integer numberOfRows; private final Integer numberOfRows;
private final SquareGrid tileGrid; private final Grid tileGrid;
private final GraphicsContext graphicsContext;
public GridTileCanvas(@NamedArg("tileWidth") Double tileWidth, public GridTileCanvas(@NamedArg("tileWidth") Double tileWidth,
@NamedArg("tileHeight") Double tileHeight, @NamedArg("tileHeight") Double tileHeight,
...@@ -28,22 +29,47 @@ public class GridTileCanvas extends Canvas { ...@@ -28,22 +29,47 @@ public class GridTileCanvas extends Canvas {
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
this.setWidth(tileWidth * numberOfColumns); this.setWidth(tileWidth * numberOfColumns);
this.setHeight(tileHeight * numberOfRows); this.setHeight(tileHeight * numberOfRows);
tileGrid = new SquareGrid(numberOfRows, numberOfColumns); tileGrid = new Grid(numberOfRows, numberOfColumns);
graphicsContext = getGraphicsContext2D();
} }
public void initializeGrid(){ public void drawGrid(){
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.GREEN, Color.BLACK, Color.RED), new Random());
drawGridTile(tileGrid); drawGridTile(tileGrid);
} }
public void drawGridTile(SquareGrid tileGrid){ public void clear(){
graphicsContext.clearRect(0, 0, getWidth(), getHeight());
}
public void initializeGrid(TileGenerator tileGenerator){
tileGrid.fill(tileGenerator);
}
public void drawGridTile(Grid tileGrid){
GraphicsContext graphicsContext = getGraphicsContext2D(); GraphicsContext graphicsContext = getGraphicsContext2D();
for(int row = 0; row < numberOfRows; row++) for(int row = 0; row < numberOfRows; row++)
for(int column = 0; column < numberOfColumns; column++){ for(int column = 0; column < numberOfColumns; column++){
Optional<SquareTile> tile = tileGrid.getTile(row, column); Optional<Tile> tile = tileGrid.getTile(row, column);
if(tile.isPresent()) if(tile.isPresent())
tile.get().draw(graphicsContext, column * tileWidth, drawTile(tile.get(), column * tileWidth, row * tileHeight);
row * tileHeight, tileWidth, tileHeight); }
}
private void drawTile(Tile tile, double x, double y) {
for(CardinalDirection side: CardinalDirection.values()){
drawSideTriangle(tile, x, y, side);
}
}
private void drawSideTriangle(Tile tile, double x, double y, CardinalDirection side){
List<PointType> cornerTypes = PointType.trianglePointTypes(side);
double[] xPoints = new double[3];
double[] yPoints = new double[3];
for(int index = 0; index < 3; index++){
xPoints[index] = x + (cornerTypes.get(index).getXPosition() * tileWidth);
yPoints[index] = y + (cornerTypes.get(index).getYPosition() * tileHeight);
} }
graphicsContext.setFill(tile.getSideColor(side));
graphicsContext.fillPolygon(xPoints, yPoints, 3);
} }
} }
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<AnchorPane 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.MainPaneController"> fx:controller="controller.GridController">
<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">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment