From f95059c13277fbce618aebcd25edc297528c4bcb Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 4 May 2019 20:24:19 +0200 Subject: [PATCH] UX fix - Made high score dialog modal - Added check for type mismatch when unserializing high scores - Added remaining mines counter refresh on board reset --- src/dev/kske/minesweeper/Board.java | 2 ++ src/dev/kske/minesweeper/Minesweeper.java | 27 +++++++++++++---------- src/dev/kske/minesweeper/ScoreDialog.java | 1 + 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/dev/kske/minesweeper/Board.java b/src/dev/kske/minesweeper/Board.java index a2fb517..8dd2c8a 100644 --- a/src/dev/kske/minesweeper/Board.java +++ b/src/dev/kske/minesweeper/Board.java @@ -87,6 +87,8 @@ public class Board extends JPanel { activeTiles = boardWidth * boardHeight; flaggedTiles = 0; + notifyFlaggedTilesEvent(new FlaggedTilesEvent(this, flaggedTiles)); + // Initialize board board = new Tile[boardWidth][boardHeight]; for (int i = 0; i < boardWidth; i++) diff --git a/src/dev/kske/minesweeper/Minesweeper.java b/src/dev/kske/minesweeper/Minesweeper.java index 554a49f..cc90155 100644 --- a/src/dev/kske/minesweeper/Minesweeper.java +++ b/src/dev/kske/minesweeper/Minesweeper.java @@ -48,15 +48,12 @@ public class Minesweeper { * Launch the application. */ public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - - public void run() { - try { - Minesweeper window = new Minesweeper(); - window.mframe.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } + EventQueue.invokeLater(() -> { + try { + Minesweeper window = new Minesweeper(); + window.mframe.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); } }); } @@ -182,14 +179,20 @@ public class Minesweeper { mframe.setJMenuBar(menubar); } - @SuppressWarnings("unchecked") + @SuppressWarnings( + "unchecked" + ) private void loadScores() { try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) { - scores = (TreeSet) in.readObject(); + Object obj = in.readObject(); + if (obj instanceof TreeSet) scores = (TreeSet) obj; + else throw new IOException("Serialized object has the wrong class."); } catch (FileNotFoundException ex) { scores = new TreeSet<>(); } catch (IOException | ClassNotFoundException ex) { - JOptionPane.showMessageDialog(mframe, "The score file seems to be corrupted. It will be replaced when closing the game.", "File error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(mframe, + "The score file seems to be corrupted. It will be replaced when closing the game.", "File error", + JOptionPane.ERROR_MESSAGE); scores = new TreeSet<>(); } } diff --git a/src/dev/kske/minesweeper/ScoreDialog.java b/src/dev/kske/minesweeper/ScoreDialog.java index 11a4553..2e79980 100644 --- a/src/dev/kske/minesweeper/ScoreDialog.java +++ b/src/dev/kske/minesweeper/ScoreDialog.java @@ -27,6 +27,7 @@ public class ScoreDialog extends JDialog { * Create the dialog. */ public ScoreDialog(Set scores) { + setModal(true); setBounds(100, 100, 450, 300); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); getContentPane().setLayout(new BorderLayout(0, 0));