public void initialiseCells() { for (int row = 0; row < grid.getRows(); row++) { for (int column = 0; column < grid.getColumns(); column++) { initialiseCell(new Coordinates(row, column)); } } } private void initialiseCell(Coordinates coordinates) { Cell cell = new Cell(); addAlivePropertyListener(cell); addClickEventHandler(cell); grid.add(cell, coordinates); } private void addAlivePropertyListener(Cell cell) { cell.aliveProperty() .addListener(newValue -> setAliveStyle(cell, newValue)); } private void setAliveStyle(Cell cell, boolean isAlive) { List<String> styleClass = cell.getStyleClass(); if (isAlive) { styleClass.add(ALIVE_STYLE_CLASS); } else { styleClass.remove(ALIVE_STYLE_CLASS); } } private void addClickEventHandler(Cell cell) { cell.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> cell.toggleAlive()); } public void resetCells() { for (int row = 0; row < grid.getRows(); row++) { for (int column = 0; column < grid.getRows(); column++) { resetCell(new Coordinates(row, column)); } } } private void resetCell(Coordinates coordinates) { Cell cell = grid.get(coordinates); cell.aliveProperty().setValue(false); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter