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++) {
CellGUI cell = new CellGUI(
grid[i][j],
s.getCellGUIStartMode(),
s.getTheme(),
s.getCellDimensions()
s
);
cell.addMouseListener(new MouseListener() {
@Override
+32 -36
View File
@@ -2,14 +2,15 @@ package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import gui.backend.Cell;
import gui.backend.Settings;
import gui.backend.Theme;
/**
@@ -24,6 +25,7 @@ class CellGUI extends JPanel {
private JLabel valueLabel;
private Note[] notesLabels = new Note[9];
private Theme theme;
private Font font;
private boolean incorrect;
// Data fields
@@ -39,26 +41,31 @@ class CellGUI extends JPanel {
* @param cell
* @param startInNotesOrValueMode
*/
public CellGUI(Cell cell, boolean noteMode, Theme theme, Dimension size) {
public CellGUI(Cell cell, Settings s) {
super();
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;
size = s.getCellDimensions();
setFocusable(true);
setLayout(valueLayout);
valueLabel = new JLabel("", SwingConstants.CENTER);
this.size = size;
setSize(this.size);
valueLabel = new Note("", theme, font);
setSize(size);
internalPanel.setSize(size);
defaultStyle();
// Populate the cell with the appropriate value or notes.
if(cell.getValue() == 0) {
internalPanel.setLayout(noteLayout);
generateNotes();
generateNotes(s.getAutoFillNotes());
} else {
valueLabel.setText(Integer.toString(cell.getValue()));
internalPanel.setLayout(valueLayout);
valueLabel.setText(Integer.toString(cell.getValue()));
internalPanel.add(valueLabel);
}
add(internalPanel);
@@ -144,15 +151,6 @@ class CellGUI extends JPanel {
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.
*
@@ -164,7 +162,8 @@ class CellGUI extends JPanel {
cell.addPossibleValue(value);
internalPanel.removeAll();
internalPanel.setLayout(noteLayout);
generateNotes();
noteMode = true;
generateNotes(true);
highlightedStyle();
}
@@ -177,15 +176,6 @@ class CellGUI extends JPanel {
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.
*
@@ -210,7 +200,8 @@ class CellGUI extends JPanel {
internalPanel.add(valueLabel);
} else {
internalPanel.setLayout(noteLayout);
generateNotes();
noteMode = true;
generateNotes(true);
}
internalPanel.repaint();
@@ -221,6 +212,9 @@ class CellGUI extends JPanel {
noteMode = !noteMode;
}
/**
* Select the cell, and style it for error, highlight, or default.
*/
public void select() {
selected = !selected;
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();
if(possibleValues.length == 0) {
for(int i = 0; i < 9; i++) {
notesLabels[i] = new Note("", theme);
internalPanel.add(notesLabels[i]);
notesLabels[i] = new Note("", theme, font);
if(autoFill) internalPanel.add(notesLabels[i]);
}
return;
}
@@ -313,11 +309,11 @@ class CellGUI extends JPanel {
// Add the noted possible values to the cell.
for(int i = 1; i <= 9 && index < possibleValues.length; 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++;
} else
notesLabels[i - 1] = new Note("", theme);
internalPanel.add(notesLabels[i - 1]);
notesLabels[i - 1] = new Note("", theme, font);
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("Exit");
fileOptions.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
fileOptions.addActionListener(e -> {
int selected = fileOptions.getSelectedIndex();
switch(selected) {
case 0:
@@ -186,21 +184,6 @@ public class Nav extends JPanel {
default:
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;
+6 -1
View File
@@ -1,5 +1,7 @@
package gui;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
@@ -13,6 +15,7 @@ import gui.backend.Theme;
*/
public class Note extends JLabel {
private Theme t;
private Font f;
/**
* Create a new Note object with the given text and theme.
@@ -20,9 +23,10 @@ public class Note extends JLabel {
* @param text
* @param t
*/
public Note(String text, Theme t) {
public Note(String text, Theme t, Font f) {
super(text, SwingConstants.CENTER);
this.t = t;
this.f = f;
style();
}
@@ -32,5 +36,6 @@ public class Note extends JLabel {
private void style() {
setBackground(t.getPrimaryBackground());
setForeground(t.getPrimaryText());
setFont(f);
}
}
+3 -3
View File
@@ -388,13 +388,13 @@ public class Settings {
// Load the font from the system directory.
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.
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;
cellGUIStartMode = false;
defaultOpenState = 0;