9.2 with array for better performance

generic-observer
Selebrator 7 years ago
parent 3f080299c6
commit f5407d8ae6

@ -1,8 +1,6 @@
package _9._2; package _9._2;
import javafx.application.Application; import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
@ -15,48 +13,40 @@ import java.util.Optional;
public class Lights extends Application { public class Lights extends Application {
private static final int DEFAULT_SIZE = 4; private static final int DEFAULT_SIZE = 4;
private static final int GAP = 2;
private static final Paint ON = Color.YELLOW; private static final Paint ON = Color.YELLOW;
private static final Paint OFF = Color.WHITE; private static final Paint OFF = Color.WHITE;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) {
final int size = Optional.ofNullable(this.getParameters().getNamed().get("size")) final int size = Optional.ofNullable(this.getParameters().getNamed().get("size"))
.map(Integer::parseInt).orElse(DEFAULT_SIZE); .map(Integer::parseInt).orElse(DEFAULT_SIZE);
Rectangle[][] positions = new Rectangle[size][size];
GridPane grid = new GridPane(); GridPane grid = new GridPane();
Scene scene = new Scene(grid, 600, 600); Scene scene = new Scene(grid, 600, 600);
scene.setFill(Color.BLACK); primaryStage.setScene(scene);
grid.setHgap(GAP); primaryStage.setTitle("Lights");
grid.setVgap(GAP);
for(int x = 0; x < size; x++) { for(int row = 0; row < size; row++) {
for(int y = 0; y < size; y++) { for(int column = 0; column < size; column++) {
Rectangle rec = new Rectangle(150, 150); Rectangle rec = new Rectangle();
rec.heightProperty().bind(scene.heightProperty().subtract(size * GAP).divide(size));
rec.widthProperty().bind(scene.widthProperty().subtract(size * GAP).divide(size));
rec.setFill(OFF); rec.setFill(OFF);
grid.add(rec, x, y); rec.setStroke(Color.BLACK);
} rec.heightProperty().bind(scene.heightProperty().divide(size).subtract(1)); // -1 for stroke
} rec.widthProperty().bind(scene.widthProperty().divide(size).subtract(1));
for(int x = 0; x < size; x++) { {
for(int y = 0; y < size; y++) { final int x = row, y = column;
Rectangle rec = getNodeByRowColumnIndex(x, y, grid); rec.setOnMouseClicked(event -> {
assert rec != null; toggle(rec);
Rectangle up = getNodeByRowColumnIndex(x, y - 1, grid); toggle(getMatrixIndexSafe(positions, x, y - 1));
Rectangle down = getNodeByRowColumnIndex(x, y + 1, grid); toggle(getMatrixIndexSafe(positions, x, y + 1));
Rectangle left = getNodeByRowColumnIndex(x - 1, y, grid); toggle(getMatrixIndexSafe(positions, x - 1, y));
Rectangle right = getNodeByRowColumnIndex(x + 1, y, grid); toggle(getMatrixIndexSafe(positions, x + 1, y));
rec.setOnMouseClicked(event -> { });
toggle(rec); }
toggle(up); grid.add(rec, row, column);
toggle(down); positions[row][column] = rec;
toggle(left);
toggle(right);
});
} }
} }
primaryStage.setScene(scene);
primaryStage.setTitle("Lights");
primaryStage.show(); primaryStage.show();
} }
@ -74,14 +64,10 @@ public class Lights extends Application {
} }
} }
public static <T extends Node> T getNodeByRowColumnIndex(final int column, final int row, GridPane gridPane) { private static <T> T getMatrixIndexSafe(final T[][] positions, final int row, final int column) {
ObservableList<Node> children = gridPane.getChildren(); return (0 <= row && row < positions.length) && (0 <= column && column < positions[row].length)
for(Node node : children) { ? positions[row][column]
if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) { : null;
return (T) node;
}
}
return null;
} }
public static void main(String[] args) { public static void main(String[] args) {

Loading…
Cancel
Save