Skip to content
Snippets Groups Projects
Commit cc87648b authored by tmp1u19's avatar tmp1u19 :octopus:
Browse files

Memorise values from cells with an event handler

parent eb1d03b8
No related branches found
No related tags found
No related merge requests found
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
......@@ -12,13 +14,15 @@ import javafx.stage.Stage;
import javafx.scene.control.Label;
public class Main extends Application {
public class Mathdoku extends Application {
private GridPane grid = new GridPane();
private VBox menu = new VBox();
private HBox topButton = new HBox();
private HBox bottomButton = new HBox();
private int n = 2;
private int n = 4;
private Cell[][] cells;
private int[][] values;
public static void main(String[] args) {
launch(args);
......@@ -49,15 +53,18 @@ public class Main extends Application {
//bottomButton.getChildren().addAll(clear, fileLoad, textLoad);
draw();
retainValues();
primaryStage.widthProperty().addListener((obs, oldVal, newVal) -> {
//menu.setPrefWidth((Double) newVal);
//draw();
draw();
drawInput();
});
primaryStage.heightProperty().addListener((obs, oldVal, newVal) -> {
//menu.setPrefHeight((Double) newVal);
//draw();
draw();
drawInput();
});
menu.getChildren().addAll(topButton, grid);
......@@ -73,16 +80,50 @@ public class Main extends Application {
}
public void draw() {
cells = new Cell[n][n];
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
Cell cell = new Cell("5");
Cell cell = new Cell("");
cell.setTranslateX(j*(600/n));
cell.setTranslateY(i*(600/n));
cells[i][j] = cell;
grid.getChildren().add(cell);
}
}
}
public void drawInput() {
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(values[i][j] != 0) {
cells[i][j].setText(String.valueOf(values[i][j]));
}
}
}
}
public void retainValues() {
values = new int[n][n];
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
int finalI = i;
int finalJ = j;
cells[i][j].getText().setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
values[finalI][finalJ] = Integer.parseInt(cells[finalI][finalJ].getText().getText());
}
});
}
}
}
class Cell extends StackPane {
private Label label;
......@@ -93,6 +134,9 @@ public class Main extends Application {
this.label = new Label(label);
this.text = new TextField();
this.label.setFont(Font.font(20));
//this.label.setAlignment(Pos.TOP_LEFT);
text.setPrefWidth(10);
text.setFont(Font.font(35));
text.setStyle("-fx-text-box-border: transparent; -fx-background-color: transparent;");
......@@ -102,17 +146,21 @@ public class Main extends Application {
rectangle.setFill(null);
rectangle.setStroke(Color.BLACK);
setAlignment(Pos.CENTER);
setPadding(new Insets(40));
getChildren().addAll(rectangle, this.text);
getChildren().addAll(rectangle, this.text, this.label);
}
public String getLabel() {
return this.label.getText();
}
public String getText() {
return text.getText();
public TextField getText() {
return this.text;
}
public void setText(String text) {
this.text.setText(text);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment