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;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
@@ -83,6 +86,21 @@ public class Board extends JPanel {
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
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);
}
}
@@ -92,13 +110,16 @@ public class Board extends JPanel {
* GUI representation of a single cell in the Sudoku grid.
*/
private class CellGUI extends JPanel {
private GridLayout noteLayout = new GridLayout(3, 3);
private GridLayout valueLayout = new GridLayout(1, 1);
private Theme theme;
private Cell cell;
// GUI fields
private JPanel internalPanel = new JPanel(new BorderLayout());
private GridLayout noteLayout = new GridLayout(3, 3);
private GridLayout valueLayout = new GridLayout(0, 1);
private JLabel valueLabel;
private Note[] notesLabels = new Note[9];
private Theme theme;
// Data fields
private Cell cell;
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) {
super();
this.cell = cell;
this.noteMode = noteMode;
this.noteMode = !noteMode; // Flip for use with setNoteMode()
this.theme = theme;
setLayout(valueLayout);
style();
// Populate the cell with the appropriate value or notes.
if(cell.getValue() == 0) {
noteMode = !noteMode; // Flip for use with setNoteMode()
valueLabel = new JLabel("");
setNoteMode();
} else {
valueLabel = new JLabel(Integer.toString(cell.getValue()));
add(valueLabel);
valueLabel = new JLabel(
Integer.toString(cell.getValue()),
SwingConstants.CENTER
);
}
setNoteMode();
add(internalPanel);
}
/**
@@ -143,7 +167,8 @@ public class Board extends JPanel {
*/
public void setValue(int 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.
*/
public void setNoteMode() {
removeAll();
internalPanel.removeAll();
if(noteMode) {
setLayout(valueLayout);
add(valueLabel);
internalPanel.setLayout(valueLayout);
internalPanel.add(valueLabel);
} else {
setLayout(noteLayout);
internalPanel.setLayout(noteLayout);
generateNotes();
for(int i = 0; i < 9; i++)
add(notesLabels[i]);
System.out.println("Displaying notes.");
}
internalPanel.repaint();
internalPanel.revalidate();
repaint();
revalidate();
noteMode = !noteMode;
}
@@ -222,23 +244,24 @@ public class Board extends JPanel {
* Create the GUI components for the cell.
*/
private void generateNotes() {
if(cell.getPossibleValues().length == 0) {
int[] possibleValues = cell.getPossibleValues();
if(possibleValues.length == 0) {
for(int i = 0; i < 9; i++) {
notesLabels[i] = new Note("", theme);
add(notesLabels[i]);
internalPanel.add(notesLabels[i]);
}
return;
}
int index = 0;
// Add the noted possible values to the cell.
for(int i = 1; i <= 9; i++) {
if(cell.getPossibleValues()[i - 1] == i)
for(int i = 1; i <= 9 && index < possibleValues.length; i++) {
if(possibleValues[index] == i) {
notesLabels[i - 1] = new Note(Integer.toString(i), theme);
else
index++;
} else
notesLabels[i - 1] = new Note("", theme);
add(notesLabels[i - 1]);
internalPanel.add(notesLabels[i - 1]);
}
}
}
-1
View File
@@ -255,7 +255,6 @@ public class Nav extends JPanel {
}
// Otherwise, open the file and populate the grid.
System.out.println("Opening the file " + filename + ".sdku.");
createGrid(f);
}
+2 -1
View File
@@ -1,6 +1,7 @@
package gui;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import gui.backend.Theme;
@@ -20,7 +21,7 @@ public class Note extends JLabel {
* @param t
*/
public Note(String text, Theme t) {
super(text);
super(text, SwingConstants.CENTER);
this.t = t;
style();
}
+3 -2
View File
@@ -306,6 +306,7 @@ public class Settings {
*/
public void setAutoFillNotes(boolean autoFillNotes) {
this.autoFillNotes = autoFillNotes;
setCellGUIStartMode(autoFillNotes);
updateSettingsFile();
}
@@ -340,9 +341,9 @@ public class Settings {
else
font = new Font("Arial", Font.PLAIN, 12);
dimension = new Dimension(750, 550);
dimension = new Dimension(1000, 1000);
resizable = false;
cellGUIStartMode = false;
cellGUIStartMode = true;
defaultOpenState = 0;
theme = new Theme(new File(appDirectory + "default.theme"));
autoFillNotes = true;
+28 -1
View File
@@ -40,9 +40,10 @@ public class SudokuChecker {
public Cell[][] getPossibleValues(Cell[][] grid) {
for(int row = 0; row < grid.length; row++) {
for(int col = 0; col < grid[row].length; col++) {
if(grid[row][col].getValue() == 0)
if(grid[row][col].getValue() != 0)
continue;
// Replace intersection with union?
ArrayList<Integer> intersection = intersection(
getRowRemainingNumbers(row),
getColRemainingNumbers(col)
@@ -65,6 +66,7 @@ public class SudokuChecker {
}
}
this.grid = grid;
return grid;
}
@@ -99,6 +101,31 @@ public class SudokuChecker {
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
* the grid. This is effectively an intersection of the numbers available