added toggle-able notes and and basic button functionality to cell's

This commit is contained in:
Josh Ashton
2024-03-23 22:15:15 -06:00
parent 5e3c4151a6
commit fa3dbf32e2
5 changed files with 85 additions and 34 deletions
+52 -29
View File
@@ -1,9 +1,12 @@
package gui; package gui;
import java.awt.BorderLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.LayoutManager; import java.awt.LayoutManager;
import java.awt.event.MouseListener;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder; import javax.swing.border.LineBorder;
import javax.swing.JLabel; import javax.swing.JLabel;
@@ -83,6 +86,21 @@ public class Board extends JPanel {
for(int i = 0; i < 9; i++) { for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) { for(int j = 0; j < 9; j++) {
CellGUI cell = new CellGUI(grid[i][j], s.getCellGUIStartMode() || s.getAutoFillNotes(), s.getTheme()); CellGUI cell = new CellGUI(grid[i][j], s.getCellGUIStartMode() || s.getAutoFillNotes(), s.getTheme());
cell.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
cell.setNoteMode();
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {}
});
add(cell); add(cell);
} }
} }
@@ -92,13 +110,16 @@ public class Board extends JPanel {
* GUI representation of a single cell in the Sudoku grid. * GUI representation of a single cell in the Sudoku grid.
*/ */
private class CellGUI extends JPanel { private class CellGUI extends JPanel {
private GridLayout noteLayout = new GridLayout(3, 3); // GUI fields
private GridLayout valueLayout = new GridLayout(1, 1); private JPanel internalPanel = new JPanel(new BorderLayout());
private Theme theme; private GridLayout noteLayout = new GridLayout(3, 3);
private GridLayout valueLayout = new GridLayout(0, 1);
private Cell cell;
private JLabel valueLabel; private JLabel valueLabel;
private Note[] notesLabels = new Note[9]; private Note[] notesLabels = new Note[9];
private Theme theme;
// Data fields
private Cell cell;
private boolean noteMode; // true to display notes, or display value private boolean noteMode; // true to display notes, or display value
/** /**
@@ -112,19 +133,22 @@ public class Board extends JPanel {
public CellGUI(Cell cell, boolean noteMode, Theme theme) { public CellGUI(Cell cell, boolean noteMode, Theme theme) {
super(); super();
this.cell = cell; this.cell = cell;
this.noteMode = noteMode; this.noteMode = !noteMode; // Flip for use with setNoteMode()
this.theme = theme; this.theme = theme;
setLayout(valueLayout);
style(); style();
// Populate the cell with the appropriate value or notes. // Populate the cell with the appropriate value or notes.
if(cell.getValue() == 0) { if(cell.getValue() == 0) {
noteMode = !noteMode; // Flip for use with setNoteMode()
valueLabel = new JLabel(""); valueLabel = new JLabel("");
setNoteMode();
} else { } else {
valueLabel = new JLabel(Integer.toString(cell.getValue())); valueLabel = new JLabel(
add(valueLabel); Integer.toString(cell.getValue()),
SwingConstants.CENTER
);
} }
setNoteMode();
add(internalPanel);
} }
/** /**
@@ -143,7 +167,8 @@ public class Board extends JPanel {
*/ */
public void setValue(int value) { public void setValue(int value) {
cell.setValue(value); cell.setValue(value);
valueLabel.setText(Integer.toString(value)); // Update the GUI with the actual Cell value (unchanged if invalid)
valueLabel.setText(Integer.toString(cell.getValue()));
} }
/** /**
@@ -189,23 +214,20 @@ public class Board extends JPanel {
* If the current mode is value, switch to note mode, and vice versa. * If the current mode is value, switch to note mode, and vice versa.
*/ */
public void setNoteMode() { public void setNoteMode() {
removeAll(); internalPanel.removeAll();
if(noteMode) { if(noteMode) {
setLayout(valueLayout); internalPanel.setLayout(valueLayout);
internalPanel.add(valueLabel);
add(valueLabel);
} else { } else {
setLayout(noteLayout); internalPanel.setLayout(noteLayout);
generateNotes(); generateNotes();
for(int i = 0; i < 9; i++)
add(notesLabels[i]);
System.out.println("Displaying notes.");
} }
internalPanel.repaint();
internalPanel.revalidate();
repaint(); repaint();
revalidate(); revalidate();
noteMode = !noteMode; noteMode = !noteMode;
} }
@@ -222,23 +244,24 @@ public class Board extends JPanel {
* Create the GUI components for the cell. * Create the GUI components for the cell.
*/ */
private void generateNotes() { private void generateNotes() {
if(cell.getPossibleValues().length == 0) { int[] possibleValues = cell.getPossibleValues();
if(possibleValues.length == 0) {
for(int i = 0; i < 9; i++) { for(int i = 0; i < 9; i++) {
notesLabels[i] = new Note("", theme); notesLabels[i] = new Note("", theme);
add(notesLabels[i]); internalPanel.add(notesLabels[i]);
} }
return; return;
} }
int index = 0;
// Add the noted possible values to the cell. // Add the noted possible values to the cell.
for(int i = 1; i <= 9; i++) { for(int i = 1; i <= 9 && index < possibleValues.length; i++) {
if(cell.getPossibleValues()[i - 1] == i) if(possibleValues[index] == i) {
notesLabels[i - 1] = new Note(Integer.toString(i), theme); notesLabels[i - 1] = new Note(Integer.toString(i), theme);
else index++;
} else
notesLabels[i - 1] = new Note("", theme); notesLabels[i - 1] = new Note("", theme);
internalPanel.add(notesLabels[i - 1]);
add(notesLabels[i - 1]);
} }
} }
} }
-1
View File
@@ -255,7 +255,6 @@ public class Nav extends JPanel {
} }
// Otherwise, open the file and populate the grid. // Otherwise, open the file and populate the grid.
System.out.println("Opening the file " + filename + ".sdku.");
createGrid(f); createGrid(f);
} }
+2 -1
View File
@@ -1,6 +1,7 @@
package gui; package gui;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.SwingConstants;
import gui.backend.Theme; import gui.backend.Theme;
@@ -20,7 +21,7 @@ public class Note extends JLabel {
* @param t * @param t
*/ */
public Note(String text, Theme t) { public Note(String text, Theme t) {
super(text); super(text, SwingConstants.CENTER);
this.t = t; this.t = t;
style(); style();
} }
+3 -2
View File
@@ -306,6 +306,7 @@ public class Settings {
*/ */
public void setAutoFillNotes(boolean autoFillNotes) { public void setAutoFillNotes(boolean autoFillNotes) {
this.autoFillNotes = autoFillNotes; this.autoFillNotes = autoFillNotes;
setCellGUIStartMode(autoFillNotes);
updateSettingsFile(); updateSettingsFile();
} }
@@ -340,9 +341,9 @@ public class Settings {
else else
font = new Font("Arial", Font.PLAIN, 12); font = new Font("Arial", Font.PLAIN, 12);
dimension = new Dimension(750, 550); dimension = new Dimension(1000, 1000);
resizable = false; resizable = false;
cellGUIStartMode = false; cellGUIStartMode = true;
defaultOpenState = 0; defaultOpenState = 0;
theme = new Theme(new File(appDirectory + "default.theme")); theme = new Theme(new File(appDirectory + "default.theme"));
autoFillNotes = true; autoFillNotes = true;
+28 -1
View File
@@ -40,9 +40,10 @@ public class SudokuChecker {
public Cell[][] getPossibleValues(Cell[][] grid) { public Cell[][] getPossibleValues(Cell[][] grid) {
for(int row = 0; row < grid.length; row++) { for(int row = 0; row < grid.length; row++) {
for(int col = 0; col < grid[row].length; col++) { for(int col = 0; col < grid[row].length; col++) {
if(grid[row][col].getValue() == 0) if(grid[row][col].getValue() != 0)
continue; continue;
// Replace intersection with union?
ArrayList<Integer> intersection = intersection( ArrayList<Integer> intersection = intersection(
getRowRemainingNumbers(row), getRowRemainingNumbers(row),
getColRemainingNumbers(col) getColRemainingNumbers(col)
@@ -65,6 +66,7 @@ public class SudokuChecker {
} }
} }
this.grid = grid;
return grid; return grid;
} }
@@ -99,6 +101,31 @@ public class SudokuChecker {
return intersection; return intersection;
} }
/**
* Determine what numbers are available to be placed in the given cell of
* the grid. This is effectively an union of the numbers available in the
* row, column, and box of the cell.
*
* @param row
* @param col
*/
private ArrayList<Integer> union(
ArrayList<Integer> a,
ArrayList<Integer> b
) {
ArrayList<Integer> union = new ArrayList<Integer>();
for(int i = 0; i < a.size(); i++) {
union.add(a.get(i));
}
for(int i = 0; i < b.size(); i++) {
if(!union.contains(b.get(i))) {
union.add(b.get(i));
}
}
return union;
}
/** /**
* Determine what numbers are available to be placed in the given cell of * Determine what numbers are available to be placed in the given cell of
* the grid. This is effectively an intersection of the numbers available * the grid. This is effectively an intersection of the numbers available