Skip to content
Snippets Groups Projects
Commit 56fbabae authored by Nimrod Abramovich's avatar Nimrod Abramovich
Browse files

added number buttons that enter into selected cell

parent 15efcca7
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -10,7 +10,7 @@ public class Cage {
private int[] squareIds;
private int value;
private String operator;
private Square[][] gridSquares;
private Square[] gridSquares;
private Label[][] cageLabels;
public Cage(int[] squareIds, int value, String operator) {
......@@ -26,6 +26,7 @@ public class Cage {
public void buildCage() {
if (squareIds.length == 1)
return;
for (int idA : squareIds) {
for (int idB : squareIds) {
if (idA != idB) {
......@@ -38,16 +39,16 @@ public class Cage {
final double WIDTH = 2;
if (rowDiff == -1 && colDiff == 0) {
gridSquares[aCoord[0]][aCoord[1]].setRightStroke(WIDTH, Color.LIGHTGREY);
gridSquares[aCoord[0]*Main.N + aCoord[1]].setRightStroke(WIDTH, Color.LIGHTGREY);
}
else if (rowDiff == 1 && colDiff == 0) {
gridSquares[aCoord[0]][aCoord[1]].setLeftStroke(WIDTH, Color.LIGHTGREY);
gridSquares[aCoord[0]*Main.N + aCoord[1]].setLeftStroke(WIDTH, Color.LIGHTGREY);
}
else if (rowDiff == 0 && colDiff == -1) {
gridSquares[aCoord[0]][aCoord[1]].setBottomStroke(WIDTH, Color.LIGHTGREY);
gridSquares[aCoord[0]*Main.N + aCoord[1]].setBottomStroke(WIDTH, Color.LIGHTGREY);
}
else if (rowDiff == 0 && colDiff == 1) {
gridSquares[aCoord[0]][aCoord[1]].setTopStroke(WIDTH, Color.LIGHTGREY);
gridSquares[aCoord[0]*Main.N + aCoord[1]].setTopStroke(WIDTH, Color.LIGHTGREY);
}
}
}
......
......@@ -43,20 +43,18 @@ public class Main extends Application {
private Stage stage;
final private double GRID_PERCENTAGE = 0.75;
private static Square[][] gridSquares = new Square[N][N];
private static Square[] gridSquares = new Square[N*N];
private TextField[] gridNumbers = new TextField[N*N];
private static Label[][] cageLabels = new Label[N*N][2];
private Rectangle gridOutline;
private boolean isWider;
private HBox previous = null;
private HBox previousHBox = null;
private Square previousSquare = null;
private GridPane setupGrid() {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
double size = scene.getWidth() * GRID_PERCENTAGE / N;
//gridPane.add(setupGridOutline(size), 0, 0, N, N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
gridPane = addToGridPane(gridPane, size, i, j);
......@@ -71,7 +69,7 @@ public class Main extends Application {
HBox hbox = setupGridHBox(square, textField, i, j);
Group group = square.getGroup();
gridSquares[i][j] = square;
gridSquares[i*N + j] = square;
gridNumbers[i*N + j] = textField;
gridPane.add(group, j, i);
......@@ -94,19 +92,9 @@ public class Main extends Application {
vbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(vbox);
hbox.setAlignment(Pos.CENTER);
hboxClickSquareEvent(hbox);
hboxClickSquareEvent(hbox, square);
return hbox;
}
private Rectangle setupGridOutline(double size) {
gridOutline = new Rectangle(size*N + N*3, size*N + N*3, size*N + N*3, size*N + N*3);
gridOutline.setStroke(Color.BLACK);
gridOutline.setFill(Color.TRANSPARENT);
gridOutline.setStrokeWidth(2);
gridOutline.toBack();
return gridOutline;
}
private Square setupGridSquare(double size) {
......@@ -198,13 +186,14 @@ public class Main extends Application {
return bottomHBox;
}
private void hboxClickSquareEvent(HBox hbox) {
private void hboxClickSquareEvent(HBox hbox, Square square) {
hbox.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
if (previous != null) {
previous.setStyle("-fx-border-width:0px;");
if (previousHBox != null) {
previousHBox.setStyle("-fx-border-width:0px;");
}
hbox.setStyle("-fx-border-color:red; -fx-border-width:2px;");
previous = hbox;
previousHBox = hbox;
previousSquare = square;
});
}
......@@ -238,17 +227,47 @@ public class Main extends Application {
double newValue = isWider ? scene.getHeight() : scene.getWidth();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
Rectangle rectangle = gridSquares[i][j].getRectangle();
Rectangle rectangle = gridSquares[i*N + j].getRectangle();
rectangle.setWidth(newValue * GRID_PERCENTAGE / N);
rectangle.setHeight(newValue * GRID_PERCENTAGE / N);
gridSquares[i][j].resizeLines(newValue * GRID_PERCENTAGE / N);
gridSquares[i*N + j].resizeLines(newValue * GRID_PERCENTAGE / N);
}
}
}
private HBox setupCenterHBox() {
HBox hbox = new HBox();
for (int i = 1; i <= N; i++) {
Button button = new Button(i + "");
double cellWidth = gridSquares[0].getRectangle().getWidth() / 2;
double fontSize = cellWidth * 0.4;
button.setPrefSize(cellWidth, cellWidth);
button.setStyle("-fx-font-size:"+fontSize+"pt");
button.setOnAction(e -> {
numbersButtonClickEvent(button);
});
hbox.getChildren().add(button);
}
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
return hbox;
}
private void numbersButtonClickEvent(Button button) {
if (previousSquare != null) {
for (int i = 0; i < gridSquares.length; i++) {
if (previousSquare.equals(gridSquares[i])) {
gridNumbers[i].setText(button.getText());
}
}
}
//gridOutline.setWidth(newValue * GRID_PERCENTAGE + N*2.5);
//gridOutline.setHeight(newValue * GRID_PERCENTAGE + N*2.5);
else {
// Some message about selecting a cell first
}
}
public static Square[][] getGridSquares() {
public static Square[] getGridSquares() {
return gridSquares;
}
......@@ -267,9 +286,10 @@ public class Main extends Application {
GridPane gridPane = setupGrid();
HBox topHBox = setupTopHBox();
HBox centerHBox = setupCenterHBox();
HBox bottomHBox = setupBottomHBox();
vBox.getChildren().addAll(topHBox, gridPane, bottomHBox);
vBox.getChildren().addAll(topHBox, gridPane, centerHBox, bottomHBox);
stageWidthResizeEvent(stage, gridPane);
stageHeightResizeEvent(stage, gridPane);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment