starting optimizations and refactoring

This commit is contained in:
Josh Ashton
2024-03-24 18:46:26 -06:00
parent bf5a54d500
commit 0409a8f41d
5 changed files with 59 additions and 77 deletions
+1 -3
View File
@@ -186,9 +186,7 @@ public class Board extends JPanel {
for(int j = 0; j < 9; j++) { for(int j = 0; j < 9; j++) {
CellGUI cell = new CellGUI( CellGUI cell = new CellGUI(
grid[i][j], grid[i][j],
s.getCellGUIStartMode(), s
s.getTheme(),
s.getCellDimensions()
); );
cell.addMouseListener(new MouseListener() { cell.addMouseListener(new MouseListener() {
@Override @Override
+32 -36
View File
@@ -2,14 +2,15 @@ package gui;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout; import java.awt.GridLayout;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder; import javax.swing.border.LineBorder;
import gui.backend.Cell; import gui.backend.Cell;
import gui.backend.Settings;
import gui.backend.Theme; import gui.backend.Theme;
/** /**
@@ -24,6 +25,7 @@ class CellGUI extends JPanel {
private JLabel valueLabel; private JLabel valueLabel;
private Note[] notesLabels = new Note[9]; private Note[] notesLabels = new Note[9];
private Theme theme; private Theme theme;
private Font font;
private boolean incorrect; private boolean incorrect;
// Data fields // Data fields
@@ -39,26 +41,31 @@ class CellGUI extends JPanel {
* @param cell * @param cell
* @param startInNotesOrValueMode * @param startInNotesOrValueMode
*/ */
public CellGUI(Cell cell, boolean noteMode, Theme theme, Dimension size) { public CellGUI(Cell cell, Settings s) {
super(); super();
this.cell = cell; this.cell = cell;
this.noteMode = !noteMode; // Flip for use with setNoteMode()
this.theme = theme; // Set the initial state of the cell based off the settings.
noteMode = s.getCellGUIStartMode();
theme = s.getTheme();
font = s.getFont();
selected = false; selected = false;
size = s.getCellDimensions();
setFocusable(true); setFocusable(true);
setLayout(valueLayout); setLayout(valueLayout);
valueLabel = new JLabel("", SwingConstants.CENTER); valueLabel = new Note("", theme, font);
this.size = size; setSize(size);
setSize(this.size); internalPanel.setSize(size);
defaultStyle(); defaultStyle();
// 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) {
internalPanel.setLayout(noteLayout); internalPanel.setLayout(noteLayout);
generateNotes(); generateNotes(s.getAutoFillNotes());
} else { } else {
valueLabel.setText(Integer.toString(cell.getValue()));
internalPanel.setLayout(valueLayout); internalPanel.setLayout(valueLayout);
valueLabel.setText(Integer.toString(cell.getValue()));
internalPanel.add(valueLabel); internalPanel.add(valueLabel);
} }
add(internalPanel); add(internalPanel);
@@ -144,15 +151,6 @@ class CellGUI extends JPanel {
internalPanel.revalidate(); internalPanel.revalidate();
} }
/**
* Set the possible values of the underlying Cell object.
*
* @param value
*/
public void setPossibleValues(int[] values) {
cell.setPossibleValues(values);
}
/** /**
* Add a possible value to the cell. * Add a possible value to the cell.
* *
@@ -164,7 +162,8 @@ class CellGUI extends JPanel {
cell.addPossibleValue(value); cell.addPossibleValue(value);
internalPanel.removeAll(); internalPanel.removeAll();
internalPanel.setLayout(noteLayout); internalPanel.setLayout(noteLayout);
generateNotes(); noteMode = true;
generateNotes(true);
highlightedStyle(); highlightedStyle();
} }
@@ -177,15 +176,6 @@ class CellGUI extends JPanel {
cell.removePossibleValue(value); cell.removePossibleValue(value);
} }
/**
* Get the possible values of the underlying Cell object.
*
* @return int[]
*/
public int[] getPossibleValues() {
return cell.getPossibleValues();
}
/** /**
* Get the status of the cell in notes mode. * Get the status of the cell in notes mode.
* *
@@ -210,7 +200,8 @@ class CellGUI extends JPanel {
internalPanel.add(valueLabel); internalPanel.add(valueLabel);
} else { } else {
internalPanel.setLayout(noteLayout); internalPanel.setLayout(noteLayout);
generateNotes(); noteMode = true;
generateNotes(true);
} }
internalPanel.repaint(); internalPanel.repaint();
@@ -221,6 +212,9 @@ class CellGUI extends JPanel {
noteMode = !noteMode; noteMode = !noteMode;
} }
/**
* Select the cell, and style it for error, highlight, or default.
*/
public void select() { public void select() {
selected = !selected; selected = !selected;
if(incorrect) { if(incorrect) {
@@ -297,14 +291,16 @@ class CellGUI extends JPanel {
} }
/** /**
* Create the GUI components for the cell. * Create the GUI components for the cell's notes.
*
* @param autoFill
*/ */
private void generateNotes() { private void generateNotes(boolean autoFill) {
int[] possibleValues = cell.getPossibleValues(); int[] possibleValues = cell.getPossibleValues();
if(possibleValues.length == 0) { 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, font);
internalPanel.add(notesLabels[i]); if(autoFill) internalPanel.add(notesLabels[i]);
} }
return; return;
} }
@@ -313,11 +309,11 @@ class CellGUI extends JPanel {
// Add the noted possible values to the cell. // Add the noted possible values to the cell.
for(int i = 1; i <= 9 && index < possibleValues.length; i++) { for(int i = 1; i <= 9 && index < possibleValues.length; i++) {
if(possibleValues[index] == i) { if(possibleValues[index] == i) {
notesLabels[i - 1] = new Note(Integer.toString(i), theme); notesLabels[i - 1] = new Note(Integer.toString(i), theme, font);
index++; index++;
} else } else
notesLabels[i - 1] = new Note("", theme); notesLabels[i - 1] = new Note("", theme, font);
internalPanel.add(notesLabels[i - 1]); if(autoFill) internalPanel.add(notesLabels[i - 1]);
} }
} }
} }
+1 -18
View File
@@ -166,9 +166,7 @@ public class Nav extends JPanel {
fileOptions.addItem("Save"); fileOptions.addItem("Save");
fileOptions.addItem("Exit"); fileOptions.addItem("Exit");
fileOptions.addMouseListener(new MouseListener() { fileOptions.addActionListener(e -> {
@Override
public void mouseClicked(MouseEvent e) {
int selected = fileOptions.getSelectedIndex(); int selected = fileOptions.getSelectedIndex();
switch(selected) { switch(selected) {
case 0: case 0:
@@ -186,21 +184,6 @@ public class Nav extends JPanel {
default: default:
System.out.println("Invalid selection."); System.out.println("Invalid selection.");
} }
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
fileOptions.showPopup();
}
@Override
public void mouseExited(MouseEvent e) {}
}); });
return fileOptions; return fileOptions;
+6 -1
View File
@@ -1,5 +1,7 @@
package gui; package gui;
import java.awt.Font;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
@@ -13,6 +15,7 @@ import gui.backend.Theme;
*/ */
public class Note extends JLabel { public class Note extends JLabel {
private Theme t; private Theme t;
private Font f;
/** /**
* Create a new Note object with the given text and theme. * Create a new Note object with the given text and theme.
@@ -20,9 +23,10 @@ public class Note extends JLabel {
* @param text * @param text
* @param t * @param t
*/ */
public Note(String text, Theme t) { public Note(String text, Theme t, Font f) {
super(text, SwingConstants.CENTER); super(text, SwingConstants.CENTER);
this.t = t; this.t = t;
this.f = f;
style(); style();
} }
@@ -32,5 +36,6 @@ public class Note extends JLabel {
private void style() { private void style() {
setBackground(t.getPrimaryBackground()); setBackground(t.getPrimaryBackground());
setForeground(t.getPrimaryText()); setForeground(t.getPrimaryText());
setFont(f);
} }
} }
+3 -3
View File
@@ -388,13 +388,13 @@ public class Settings {
// Load the font from the system directory. // Load the font from the system directory.
if(loadFont("HackNerdFont-Regular") == 0) if(loadFont("HackNerdFont-Regular") == 0)
font = new Font("Hack Nerd Font", Font.PLAIN, 12); font = new Font("Hack Nerd Font", Font.PLAIN, 16);
// If the font does not exist, use Arial as the default font. // If the font does not exist, use Arial as the default font.
else else
font = new Font("Arial", Font.PLAIN, 12); font = new Font("Arial", Font.PLAIN, 16);
dimension = new Dimension(1000, 1000); dimension = new Dimension(600, 800);
resizable = false; resizable = false;
cellGUIStartMode = false; cellGUIStartMode = false;
defaultOpenState = 0; defaultOpenState = 0;