-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b400c25
commit e548d8e
Showing
3 changed files
with
77 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/github/viktigpetterr/sudokusolver/javafx/SudokuTilePane.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.viktigpetterr.sudokusolver.javafx; | ||
|
||
import com.github.viktigpetterr.sudokusolver.sudoku.Sudoku; | ||
import javafx.geometry.Insets; | ||
import javafx.scene.layout.TilePane; | ||
|
||
public class SudokuTilePane extends TilePane { | ||
|
||
private final Sudoku sudoku; | ||
|
||
private static final String DEFAULT_VALUE = "0"; | ||
|
||
public SudokuTilePane(Sudoku sudoku) { | ||
this.sudoku = sudoku; | ||
|
||
this.setPadding(new Insets(10, 10, 10, 10)); | ||
this.setHgap(7); | ||
this.setVgap(7); | ||
|
||
this.refresh(true); | ||
} | ||
|
||
private boolean styled(int i, int j) { | ||
return | ||
(j < 3 && i < 3) || | ||
(j < 3 && i > 5) || | ||
(j > 2 && j < 6 && i > 2 && i < 6) || | ||
(j > 5 && i < 3) || | ||
(j > 5 && i > 5); | ||
} | ||
|
||
public void refresh(boolean clear) { | ||
this.getChildren().clear(); | ||
for (int i = 0; i < sudoku.dimension(); i++) { | ||
for (int j = 0; j < sudoku.dimension(); j++) { | ||
OneNumberTextField numField = new OneNumberTextField(styled(i, j)); | ||
if (!clear) { | ||
numField.setNumber(sudoku.getValueOf(i, j)); | ||
} | ||
int finalI = i; | ||
int finalJ = j; | ||
numField.textProperty().addListener((observable, oldValue, newValue) -> { | ||
try { | ||
Integer.parseInt(newValue); | ||
} catch (NumberFormatException e) { | ||
newValue = DEFAULT_VALUE; | ||
} | ||
sudoku.setValue(finalI, finalJ, Integer.parseInt(newValue)); | ||
}); | ||
|
||
this.getChildren().add(numField); | ||
} | ||
} | ||
} | ||
|
||
} |