From 3f080299c62c0d7ba96cdf866edb70f145aa8b7a Mon Sep 17 00:00:00 2001 From: Selebrator Date: Wed, 5 Jun 2019 10:29:57 +0200 Subject: [PATCH] 9.2 initial commit --- build.gradle | 2 +- src/main/java/_9/_2/Lights.java | 90 +++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/java/_9/_2/Lights.java diff --git a/build.gradle b/build.gradle index 0d565c6..821408a 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ compileJava.options.encoding = 'UTF-8' project.ext.junitVersion = '5.3.1' -mainClassName = '_9._1.ColorMixer' +mainClassName = '_9._2.Lights' sourceCompatibility = 1.8 diff --git a/src/main/java/_9/_2/Lights.java b/src/main/java/_9/_2/Lights.java new file mode 100644 index 0000000..1759e0e --- /dev/null +++ b/src/main/java/_9/_2/Lights.java @@ -0,0 +1,90 @@ +package _9._2; + +import javafx.application.Application; +import javafx.collections.ObservableList; +import javafx.scene.Node; +import javafx.scene.Scene; +import javafx.scene.layout.GridPane; +import javafx.scene.paint.Color; +import javafx.scene.paint.Paint; +import javafx.scene.shape.Rectangle; +import javafx.stage.Stage; + +import java.util.Optional; + +public class Lights extends Application { + + private static final int DEFAULT_SIZE = 4; + private static final int GAP = 2; + private static final Paint ON = Color.YELLOW; + private static final Paint OFF = Color.WHITE; + + @Override + public void start(Stage primaryStage) throws Exception { + final int size = Optional.ofNullable(this.getParameters().getNamed().get("size")) + .map(Integer::parseInt).orElse(DEFAULT_SIZE); + GridPane grid = new GridPane(); + Scene scene = new Scene(grid, 600, 600); + scene.setFill(Color.BLACK); + grid.setHgap(GAP); + grid.setVgap(GAP); + for(int x = 0; x < size; x++) { + for(int y = 0; y < size; y++) { + Rectangle rec = new Rectangle(150, 150); + rec.heightProperty().bind(scene.heightProperty().subtract(size * GAP).divide(size)); + rec.widthProperty().bind(scene.widthProperty().subtract(size * GAP).divide(size)); + rec.setFill(OFF); + grid.add(rec, x, y); + } + } + for(int x = 0; x < size; x++) { + for(int y = 0; y < size; y++) { + Rectangle rec = getNodeByRowColumnIndex(x, y, grid); + assert rec != null; + Rectangle up = getNodeByRowColumnIndex(x, y - 1, grid); + Rectangle down = getNodeByRowColumnIndex(x, y + 1, grid); + Rectangle left = getNodeByRowColumnIndex(x - 1, y, grid); + Rectangle right = getNodeByRowColumnIndex(x + 1, y, grid); + rec.setOnMouseClicked(event -> { + toggle(rec); + toggle(up); + toggle(down); + toggle(left); + toggle(right); + }); + } + } + + primaryStage.setScene(scene); + primaryStage.setTitle("Lights"); + primaryStage.show(); + } + + private void toggle(Rectangle rec) { + if(rec == null) { + return; + } + Paint color = rec.getFill(); + if(color == ON) { + rec.setFill(OFF); + } else if(color == OFF) { + rec.setFill(ON); + } else { + throw new IllegalStateException(); + } + } + + public static T getNodeByRowColumnIndex(final int column, final int row, GridPane gridPane) { + ObservableList children = gridPane.getChildren(); + for(Node node : children) { + if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) { + return (T) node; + } + } + return null; + } + + public static void main(String[] args) { + launch(args); + } +}