- Made high score dialog modal
- Added check for type mismatch when unserializing high scores
- Added remaining mines counter refresh on board reset
This commit is contained in:
Kai S. K. Engelbart 2019-05-04 20:24:19 +02:00
parent 43e0b9d856
commit f95059c132
3 changed files with 18 additions and 12 deletions

View File

@ -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++)

View File

@ -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<Score>) in.readObject();
Object obj = in.readObject();
if (obj instanceof TreeSet<?>) scores = (TreeSet<Score>) 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<>();
}
}

View File

@ -27,6 +27,7 @@ public class ScoreDialog extends JDialog {
* Create the dialog.
*/
public ScoreDialog(Set<Score> scores) {
setModal(true);
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0));