From 3477cc53ed4b8d20e0ef929349b7cf4f73ca0057 Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Thu, 21 Mar 2024 15:14:45 -0600 Subject: [PATCH] bug fix relating to file picker. --- src/App.java | 34 ++++++++----- src/gui/Board.java | 38 +++++++++++++++ src/{ => gui}/Cell.java | 1 + src/gui/ComboBox.java | 2 + src/gui/FileChooser.java | 3 ++ src/{ => gui}/Nav.java | 8 ++-- src/gui/Root.java | 35 ++++++++++++++ src/gui/{ => backend}/Settings.java | 61 +++++++++++++++++++++++- src/{ => gui/backend}/SudokuChecker.java | 3 ++ src/run.ps1 | 1 + src/run.sh | 2 + 11 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 src/gui/Board.java rename src/{ => gui}/Cell.java (99%) rename src/{ => gui}/Nav.java (99%) create mode 100644 src/gui/Root.java rename src/gui/{ => backend}/Settings.java (80%) rename src/{ => gui/backend}/SudokuChecker.java (99%) diff --git a/src/App.java b/src/App.java index 4447296..3e87fdd 100644 --- a/src/App.java +++ b/src/App.java @@ -1,8 +1,10 @@ import javax.swing.JFrame; -import javax.swing.JPanel; import javax.swing.UIManager; -import gui.Settings; +import gui.Nav; +import gui.Root; +import gui.backend.Settings; +import gui.backend.SudokuChecker; import java.awt.BorderLayout; import java.awt.Dimension; @@ -16,7 +18,7 @@ public class App extends JFrame { private Nav nav; // Data fields - private Settings settings; + private Settings s; private SudokuChecker checker; /** @@ -24,8 +26,8 @@ public class App extends JFrame { */ public App() { super("Sudoku"); - settings = new Settings(); - nav = new Nav(settings, false); + s = new Settings(); + nav = new Nav(s, false); initSetup(); add(createApp()); } @@ -38,31 +40,37 @@ public class App extends JFrame { // values from Settings. setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); - setPreferredSize(new Dimension(750, 550)); + setPreferredSize(s.getDimension()); setSize(getPreferredSize()); - setResizable(false); + setResizable(s.getResizable()); setLocationRelativeTo(null); + // Set the look and feel for the app to be cross-platform. try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() ); } catch (Exception e) { System.out.println( - "Error with cross platform look/feel in frameSetup()." + "Error with cross platform look/feel in initSetup()." ); } } /** - * Create the root JPanel holding all other GUI components. + * Create the Root JPanel for the Sudoku App. This will contain all other + * GUI components. * - * @return JPanel + * Settings must be passed since, in the future, the user may be able to + * customize the layout of the app. + * + * @param settings + * @return Root */ - private JPanel createApp() { - // TODO: Create the root JPanel holding all other GUI components. - JPanel root = new JPanel(); + private Root createApp() { + Root root = new Root(s); root.add(nav); + return root; } diff --git a/src/gui/Board.java b/src/gui/Board.java new file mode 100644 index 0000000..f3c33ff --- /dev/null +++ b/src/gui/Board.java @@ -0,0 +1,38 @@ +package gui; + +import javax.swing.JPanel; + +import gui.backend.Settings; + +/** + * The Board class represents the Sudoku board in the GUI. + * + * It contains all other related GUI components, and is the main JPanel for + * the board. Accessible information from the Board includes: + * - the current state of the board, including filled values and notes; + * - the current difficulty; + * - the current time since the puzzle was started; + * - the current hints available; and + * - the current number of mistakes. + */ +public class Board extends JPanel { + private Settings s; + + /** + * Create a new Board object with the default styling from settings. + * + * @param s + */ + public Board(Settings s) { + super(); + this.s = s; + style(); + } + + /** + * Set up the Board Panel with the appropriate styling. + */ + private void style() { + // TODO: Style the Board Panel. + } +} diff --git a/src/Cell.java b/src/gui/Cell.java similarity index 99% rename from src/Cell.java rename to src/gui/Cell.java index 0f242cc..5f72672 100644 --- a/src/Cell.java +++ b/src/gui/Cell.java @@ -1,3 +1,4 @@ +package gui; /** * A Cell represents a single cell in the Sudoku grid. * This helper class is used to store the row, column, value, and possible values for a cell. diff --git a/src/gui/ComboBox.java b/src/gui/ComboBox.java index 6db084e..fec2702 100644 --- a/src/gui/ComboBox.java +++ b/src/gui/ComboBox.java @@ -2,6 +2,8 @@ package gui; import javax.swing.JComboBox; +import gui.backend.Settings; + /** * A custom JComboBox with the appropriate styling for the Sudoku app. */ diff --git a/src/gui/FileChooser.java b/src/gui/FileChooser.java index a84353b..184cf81 100644 --- a/src/gui/FileChooser.java +++ b/src/gui/FileChooser.java @@ -2,6 +2,9 @@ package gui; import java.io.File; import javax.swing.filechooser.FileFilter; + +import gui.backend.Settings; + import javax.swing.JFileChooser; /** diff --git a/src/Nav.java b/src/gui/Nav.java similarity index 99% rename from src/Nav.java rename to src/gui/Nav.java index 184eac7..ed0b07c 100644 --- a/src/Nav.java +++ b/src/gui/Nav.java @@ -1,14 +1,14 @@ +package gui; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import javax.swing.JPanel; + +import gui.backend.Settings; + import javax.swing.JLabel; import javax.swing.JButton; - -import gui.ComboBox; -import gui.FileChooser; -import gui.Settings; /** * The Nav class represents the Navigation bar at the top of the JFrame window. * diff --git a/src/gui/Root.java b/src/gui/Root.java new file mode 100644 index 0000000..376a45d --- /dev/null +++ b/src/gui/Root.java @@ -0,0 +1,35 @@ +package gui; + +import javax.swing.JPanel; + +import gui.backend.Settings; + +/** + * The Root class represents the root JPanel for the Sudoku app. + * + * It contains all other GUI components, and is the main JPanel for the app. + * To create a new Root object, a Settings object must be passed in for + * styling purposes. + */ +public class Root extends JPanel { + private Settings s; + + /** + * Create a new Root object with the default styling from settings. + * + * @param s + */ + public Root(Settings s) { + super(); + + this.s = s; + style(); + } + + /** + * Set up the Root Panel with the appropriate styling. + */ + private void style() { + // TODO: Style the Root Panel. + } +} diff --git a/src/gui/Settings.java b/src/gui/backend/Settings.java similarity index 80% rename from src/gui/Settings.java rename to src/gui/backend/Settings.java index ee88797..3ccc88a 100644 --- a/src/gui/Settings.java +++ b/src/gui/backend/Settings.java @@ -1,9 +1,10 @@ -package gui; +package gui.backend; import java.io.File; import java.io.IOException; import java.awt.GraphicsEnvironment; +import java.awt.Dimension; import java.awt.Font; import java.awt.FontFormatException; @@ -37,6 +38,12 @@ public class Settings { // The font to be used throughout the application. private Font font; + // The default dimension of the application window. + private Dimension dimension; + + // Resizable setting + private boolean resizable; + /** * Create a new Settings object, initializing the default settings * or reading in the settings from settings file if it exists. @@ -137,6 +144,50 @@ public class Settings { updateSettingsFile(); } + /** + * The default dimension of the application window. + * + * The user can specify both the starting width and height of the app, + * and the app will remember the last used dimensions as well. + * + * @return Dimension + */ + public Dimension getDimension() { + return dimension; + } + + /** + * Set the default dimension and write it to the settings file. + * + * @param dimension + */ + public void setDimension(Dimension dimension) { + this.dimension = dimension; + updateSettingsFile(); + } + + /** + * The resizable setting for the application window. + * + * By default, the application window is not resizable and is considered + * an experimental feature. Dynamic layout is not yet supported. + * + * @return boolean + */ + public boolean getResizable() { + return resizable; + } + + /** + * Set the resizable setting and write it to the settings file. + * + * @param resizable + */ + public void setResizable(boolean resizable) { + this.resizable = resizable; + updateSettingsFile(); + } + /** * Open and write the settings to the settings file. * @@ -151,7 +202,13 @@ public class Settings { * settings file. */ private void defaultSettings() { + // Setup the default directory for .sdku puzzle files. defaultDirectory = appDirectory + "puzzles"; + File dir = new File(defaultDirectory); + + if(!dir.exists() || !dir.isDirectory()) + dir.mkdirs(); + newFileProperties = 0; // Load the font from the system directory. @@ -162,6 +219,8 @@ public class Settings { else font = new Font("Arial", Font.PLAIN, 12); + dimension = new Dimension(750, 550); + resizable = false; updateSettingsFile(); } diff --git a/src/SudokuChecker.java b/src/gui/backend/SudokuChecker.java similarity index 99% rename from src/SudokuChecker.java rename to src/gui/backend/SudokuChecker.java index 34cc148..10af2a6 100644 --- a/src/SudokuChecker.java +++ b/src/gui/backend/SudokuChecker.java @@ -1,5 +1,8 @@ +package gui.backend; import java.util.ArrayList; +import gui.Cell; + /** * The SudokuChecker class is responsible for calculating the solution to * a given Sudoku puzzle. diff --git a/src/run.ps1 b/src/run.ps1 index 9ef6518..8c74113 100644 --- a/src/run.ps1 +++ b/src/run.ps1 @@ -6,6 +6,7 @@ function Run { java App $flag rm *.class rm gui/*.class + rm gui/backend/*.class } if ($args.Length -eq 1) { diff --git a/src/run.sh b/src/run.sh index 448bcb8..70f0d5b 100644 --- a/src/run.sh +++ b/src/run.sh @@ -2,6 +2,8 @@ run() { javac *.java java App $1 rm *.class + rm gui/*.class + rm gui/backend/*.class } if($# -eq 1) then