Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package view;
import controller.GameController;
import javafx.animation.FillTransition;
import javafx.beans.NamedArg;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import model.Cell;
import model.GrayGrid;
import model.Grid;
public class MatrixPane extends GridPane {
public final double tileWidth;
public final double tileHeight;
private final Integer numberOfColumns;
private final Integer numberOfRows;
private final Grid cellGrid;
private GameController controller;
public MatrixPane(@NamedArg("tileWidth") Double tileWidth,
@NamedArg("tileHeight") Double tileHeight,
@NamedArg("numberOfColumns") Integer numberOfColumns,
@NamedArg("numberOfRows") Integer numberOfRows) {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
// TODO replace by new ArrayGrid(numberOfRows, numberOfColumns)
cellGrid = new GrayGrid(numberOfRows, numberOfColumns);
initMatrix();
}
public void setGameController(GameController controller){
this.controller = controller;
}
private void initMatrix() {
for (int rowIndex= 0; rowIndex < numberOfRows ; rowIndex++) {
for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
addCellRectangle(cellGrid.getCell(rowIndex,columnIndex),rowIndex,columnIndex);
}
}
}
public Grid getGrid(){
return cellGrid;
}
private void addCellRectangle(Cell cell, int rowIndex, int columnIndex) {
Rectangle rectangleCell = new Rectangle(tileWidth,tileHeight);
rectangleCell.setStyle("-fx-stroke: black; -fx-stroke-width: 1");
addStatePropertyListener(cell, rectangleCell);
rectangleCell.setFill(cell.getColor());
addClickEventHandler(cell, rectangleCell);
add(rectangleCell, columnIndex, rowIndex);
}
private void addStatePropertyListener(Cell cell, Rectangle cellRectangle) {
cell.getColorProperty().addListener((observable, oldValue, newValue) ->
updateFill(cellRectangle, oldValue, newValue));
}
private void updateFill(Rectangle cellRectangle,Color cellColor, Color newCellColor) {
new FillTransition(Duration.millis(35),cellRectangle,cellColor,newCellColor).play();
}
private void addClickEventHandler(Cell cell, Rectangle cellRectangle) {
cellRectangle.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> controller.playHumanTurn(cell.getColor()));
}
}