added deselection support, though still buggy. additionally, altered default theme for easier viewing and for aesthetics

This commit is contained in:
Josh Ashton
2024-03-24 12:10:06 -06:00
parent fa3dbf32e2
commit 1afd170fea
4 changed files with 144 additions and 15 deletions
+101 -5
View File
@@ -8,6 +8,7 @@ import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.Box;
import javax.swing.JLabel;
import gui.backend.Settings;
@@ -28,8 +29,10 @@ import gui.backend.Theme;
public class Board extends JPanel {
private Settings s;
private Cell[][] grid;
private CellGUI[][] gridGUI;
private Box[][] boxGUI = new Box[3][3];
private SudokuChecker sc;
private Cell selected;
private CellGUI selected;
/**
* Create a new Board object with the default styling from settings.
@@ -82,14 +85,72 @@ public class Board extends JPanel {
*/
private void createBoard() {
if(s.getAutoFillNotes()) grid = sc.getPossibleValues(grid);
gridGUI = new CellGUI[9][9];
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());
CellGUI cell = new CellGUI(
grid[i][j],
s.getCellGUIStartMode(),
s.getTheme()
);
cell.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
cell.setNoteMode();
// If a cell is already selected, deselect it and
// the row and column.
if(selected != null) {
int boxRow = selected.cell.getRow() / 3;
int boxCol = selected.cell.getCol() / 3;
for(int n = 0; n < 9; n++) {
int row = selected.cell.getRow();
int col = selected.cell.getCol();
if(
(row == selected.cell.getRow() && n == selected.cell.getCol()) ||
(col == selected.cell.getCol() && n == selected.cell.getRow())
) continue;
gridGUI[n][col].select();
gridGUI[row][n].select();
}
for(int n = 0; n < 3; n++) {
for(int m = 0; m < 3; m++) {
int row = boxRow * 3 + n;
int col = boxCol * 3 + m;
if(row == selected.cell.getRow() || col == selected.cell.getCol()) continue;
gridGUI[row][col].select();
}
}
}
// Graphically highlight the row and column
// of the selected cell.
for(int n = 0; n < 9; n++) {
gridGUI[n][cell.cell.getCol()].select();
gridGUI[cell.cell.getRow()][n].select();
}
selected = cell;
int boxRow = selected.cell.getRow() / 3;
int boxCol = selected.cell.getCol() / 3;
for(int n = 0; n < 3; n++) {
for(int m = 0; m < 3; m++) {
int row = boxRow * 3 + n;
int col = boxCol * 3 + m;
if(row == selected.cell.getRow() || col == selected.cell.getCol()) continue;
if(
(row == selected.cell.getRow() && n == selected.cell.getCol()) ||
(col == selected.cell.getCol() && n == selected.cell.getRow())
) continue;
gridGUI[row][col].select();
}
}
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {}
@@ -101,7 +162,8 @@ public class Board extends JPanel {
public void mouseExited(java.awt.event.MouseEvent e) {}
});
add(cell);
gridGUI[i][j] = cell;
add(gridGUI[i][j]);
}
}
}
@@ -121,6 +183,7 @@ public class Board extends JPanel {
// Data fields
private Cell cell;
private boolean noteMode; // true to display notes, or display value
private boolean selected = false; // true if the cell is selected
/**
* Create a new CellGUI object with the given single cell.
@@ -135,19 +198,25 @@ public class Board extends JPanel {
this.cell = cell;
this.noteMode = !noteMode; // Flip for use with setNoteMode()
this.theme = theme;
selected = false;
setLayout(valueLayout);
style();
// Populate the cell with the appropriate value or notes.
if(cell.getValue() == 0) {
valueLabel = new JLabel("");
valueLabel.setForeground(theme.getPrimaryText());
internalPanel.setLayout(noteLayout);
generateNotes();
} else {
valueLabel = new JLabel(
Integer.toString(cell.getValue()),
SwingConstants.CENTER
);
valueLabel.setForeground(theme.getPrimaryText());
internalPanel.setLayout(valueLayout);
internalPanel.add(valueLabel);
}
setNoteMode();
add(internalPanel);
}
@@ -214,6 +283,8 @@ public class Board extends JPanel {
* If the current mode is value, switch to note mode, and vice versa.
*/
public void setNoteMode() {
if(cell.isInitValue()) return;
internalPanel.removeAll();
if(noteMode) {
internalPanel.setLayout(valueLayout);
@@ -231,12 +302,37 @@ public class Board extends JPanel {
noteMode = !noteMode;
}
public void select() {
selected = !selected;
if(!selected) {
setBackground(theme.getPrimaryBackground());
setForeground(theme.getPrimaryText());
internalPanel.setBackground(theme.getPrimaryBackground());
internalPanel.setForeground(theme.getPrimaryText());
valueLabel.setForeground(theme.getPrimaryText());
setBorder(new LineBorder(theme.getPrimaryBorder(), 2));
} else {
setBackground(theme.getSecondaryBackground());
setForeground(theme.getSecondaryText());
internalPanel.setBackground(theme.getSecondaryBackground());
internalPanel.setForeground(theme.getSecondaryText());
valueLabel.setForeground(theme.getPrimaryText());
setBorder(new LineBorder(theme.getSecondaryBorder(), 2));
}
repaint();
revalidate();
}
/**
* Style the cell with the appropriate colors from the theme.
*/
private void style() {
setBackground(theme.getPrimaryBackground());
setForeground(theme.getPrimaryText());
internalPanel.setBackground(theme.getPrimaryBackground());
internalPanel.setForeground(theme.getPrimaryText());
setBorder(new LineBorder(theme.getPrimaryBorder(), 2));
}
+36 -4
View File
@@ -88,6 +88,24 @@ public class Cell {
return value;
}
/**
* Get the row of the cell.
*
* @return row number
*/
public int getRow() {
return row;
}
/**
* Get the column of the cell.
*
* @return column number
*/
public int getCol() {
return col;
}
/**
* Get the box a cell is in.
*
@@ -143,7 +161,8 @@ public class Cell {
/**
* Get a list of possible values for the cell.
*
* This list is a copy of the cell's possible values, and is sorted in ascending order.
* This list is a copy of the cell's possible values, and is sorted
* in ascending order.
*
* @return list of possible values for the cell
*/
@@ -151,6 +170,17 @@ public class Cell {
return possibleValues.toArray();
}
/**
* Check if the cell has an initial value.
*
* An initial value is a value that is set when the cell is created,
* and cannot be changed.
* @return
*/
public boolean isInitValue() {
return initValue;
}
/**
* Given the cell's row and column, set the box number for the cell.
*/
@@ -161,10 +191,12 @@ public class Cell {
}
/**
* This class is used to store the possible values for a cell in a sorted list.
* The list is sorted in ascending order, and the values are stored in a linked list.
* This class is used to store the possible values for a cell in a
* sorted list. The list is sorted in ascending order, and the values are
* stored in a linked list.
*
* The List class also contains a method to add a value to the list, and a method to get the list of values as an array.
* The List class also contains a method to add a value to the list,
* and a method to get the list of values as an array.
*/
private class List {
+1 -1
View File
@@ -343,7 +343,7 @@ public class Settings {
dimension = new Dimension(1000, 1000);
resizable = false;
cellGUIStartMode = true;
cellGUIStartMode = false;
defaultOpenState = 0;
theme = new Theme(new File(appDirectory + "default.theme"));
autoFillNotes = true;
+6 -5
View File
@@ -71,11 +71,12 @@ public class Theme {
* Sets the theme for the GUI components.
*/
private void setTheme() {
primaryBackground = Color.LIGHT_GRAY;
secondaryBackground = Color.LIGHT_GRAY;
primaryText = Color.DARK_GRAY;
secondaryText = Color.DARK_GRAY;
primaryBorder = Color.BLACK;
primaryBackground = Color.decode("#4A245E");
secondaryBackground = Color.decode("#DADFF7");
primaryText = Color.decode("#DBABF7");
secondaryText = Color.decode("#4A245E");
primaryBorder = Color.decode("#A76BCA");
secondaryBorder = Color.decode("#DBABF7");
}
/**