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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package scrabble;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
public class Grid extends Region {
private static final short CELL_NUMBER = 225;
private static final short CELL_PER_COLUMN = 15;
private static final short CELL_PER_ROW = 15;
private final Group gridContainer = new Group();
private final List<Cell> cells = new ArrayList<>(CELL_NUMBER);
public Grid() {
super();
setId("grid");
getStyleClass().add("scrabble-grid");
IntStream.range(0, CELL_PER_ROW)
.forEach(row -> IntStream.range(0, CELL_PER_COLUMN)
.forEach(column -> {
final String cellId = String.format("%d-%d", column, row);
final Cell cell = new Cell();
cell.setId(cellId);
cell.getProperties().put("column", column);
cell.getProperties().put("row", row);
cells.add(cell);
}));
gridContainer.setId("grid-container");
gridContainer.getChildren().setAll(cells);
gridContainer.addEventFilter(MouseEvent.MOUSE_PRESSED, mousePressedHandler);
getChildren().add(gridContainer);
}
@Override
protected void layoutChildren() {
super.layoutChildren();
final double width = getWidth();
final double height = getHeight();
final Insets insets = getInsets();
final double areaX = insets.getLeft();
final double areaY = insets.getTop();
final double areaW = Math.max(0, width - (insets.getLeft() + insets.getRight()));
final double areaH = Math.max(0, height - (insets.getTop() + insets.getBottom()));
// System.out.println(areaW + " " + areaH);
if (areaW == 0 || areaH == 0) {
return;
}
final double cellSide = Math.min(areaW, areaH) / CELL_PER_COLUMN;
IntStream.range(0, CELL_PER_ROW)
.forEach(row -> IntStream.range(0, CELL_PER_COLUMN)
.forEach(column -> {
final int index = row * CELL_PER_COLUMN + column;
final Cell cell = cells.get(index);
final double x = column * cellSide;
final double y = row * cellSide;
cell.resizeRelocate(x, y, cellSide, cellSide);
}));
final double gridX = areaX + (areaW - gridContainer.getBoundsInParent().getWidth()) / 2.0;
final double gridY = areaY + (areaH - gridContainer.getBoundsInParent().getHeight()) / 2.0;
gridContainer.relocate(gridX, gridY);
// System.out.println(gridContainer.getLayoutX() + " --- " + gridContainer.getLayoutY());
}
private final EventHandler<MouseEvent> mousePressedHandler = mouseEvent -> {
final double mouseX = mouseEvent.getX();
final double mouseY = mouseEvent.getY();
final Bounds gridBounds = gridContainer.getBoundsInLocal();
final int column = (int) (CELL_PER_COLUMN * mouseX / gridBounds.getWidth());
final int row = (int) (CELL_PER_ROW * mouseY / gridBounds.getHeight());
System.out.println(column + " " + row);
final int index = row * CELL_PER_COLUMN + column;
final Cell cell = cells.get(index);
System.out.println(cell.getId() + " " + cell.getProperties().get("column") + " " + cell.getProperties().get("row"));
};
public static class Cell extends Region {
public Cell() {
super();
setId("cell");
getStyleClass().add("scrabble-cell");
setManaged(false);
}
}
} |
Partager