From 5f408ff38341f15bc1cf89c10550341fe804fd18 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 15 May 2019 19:02:01 +0200 Subject: [PATCH] Added a visible game timer --- src/dev/kske/minesweeper/Board.java | 10 +++-- src/dev/kske/minesweeper/Minesweeper.java | 48 +++++++++++++++++------ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/dev/kske/minesweeper/Board.java b/src/dev/kske/minesweeper/Board.java index 8dd2c8a..ec8ee9b 100644 --- a/src/dev/kske/minesweeper/Board.java +++ b/src/dev/kske/minesweeper/Board.java @@ -38,7 +38,7 @@ public class Board extends JPanel { private int mines, activeTiles, flaggedTiles; private Tile[][] board; private BoardConfig boardConfig; - + private Instant start, finish; private List listeners; @@ -97,7 +97,7 @@ public class Board extends JPanel { initMines(); repaint(); revalidate(); - + start = Instant.now(); } @@ -242,15 +242,17 @@ public class Board extends JPanel { private void onGameOver() { finish = Instant.now(); int duration = (int) Duration.between(start, finish).toMillis(); - + repaint(); GameOverEvent evt = new GameOverEvent(this, gameState, boardConfig, duration); notifyGameStateEvent(evt); } - + public int getMines() { return mines; } public int getActiveTiles() { return activeTiles; } public int getFlaggedTiles() { return flaggedTiles; } + + public BoardConfig getBoardConfig() { return boardConfig; } } diff --git a/src/dev/kske/minesweeper/Minesweeper.java b/src/dev/kske/minesweeper/Minesweeper.java index cc90155..35b1f84 100644 --- a/src/dev/kske/minesweeper/Minesweeper.java +++ b/src/dev/kske/minesweeper/Minesweeper.java @@ -2,7 +2,6 @@ package dev.kske.minesweeper; import java.awt.BorderLayout; import java.awt.EventQueue; -import java.awt.FlowLayout; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -24,6 +23,7 @@ import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; +import javax.swing.Timer; import javax.swing.UIManager; /** @@ -39,6 +39,8 @@ public class Minesweeper { private JFrame mframe; private Board board; + private Timer timer; + private int gameTime; private TreeSet scores; private final String scoresFile = "scores.ser"; private final BoardConfig easyConfig = new BoardConfig(8, 8, 10), mediumConfig = new BoardConfig(16, 16, 40), @@ -60,6 +62,8 @@ public class Minesweeper { /** * Create the application. + * + * @wbp.parser.entryPoint */ public Minesweeper() { initialize(); @@ -85,7 +89,7 @@ public class Minesweeper { }); mframe.setResizable(false); mframe.setTitle("Minesweeper"); - mframe.setBounds(100, 100, 198, 123); + mframe.setBounds(100, 100, 359, 86); mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); createMenuBar(); @@ -97,16 +101,39 @@ public class Minesweeper { JPanel headerPanel = new JPanel(); mframe.getContentPane().add(headerPanel, BorderLayout.NORTH); - headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); + headerPanel.setLayout(new BorderLayout(0, 0)); + + JButton btnRestart = new JButton("Restart"); + btnRestart.setHorizontalAlignment(SwingConstants.RIGHT); + headerPanel.add(btnRestart, BorderLayout.EAST); + + JPanel panel = new JPanel(); + headerPanel.add(panel, BorderLayout.WEST); + panel.setLayout(new BorderLayout(0, 0)); + + JLabel lblTime = new JLabel("Time:"); + panel.add(lblTime, BorderLayout.NORTH); + + timer = new Timer(1000, e -> lblTime.setText("Time: " + gameTime++ + "s")); + timer.setRepeats(true); + timer.setInitialDelay(0); + timer.setCoalesce(true); JLabel lblRemainingMines = new JLabel("Remaining Mines: " + easyConfig.mines); + panel.add(lblRemainingMines, BorderLayout.SOUTH); lblRemainingMines.setHorizontalAlignment(SwingConstants.LEFT); - headerPanel.add(lblRemainingMines); + btnRestart.addActionListener((evt) -> { + board.reset(); + gameTime = 0; + timer.restart(); + }); + mframe.pack(); board.registerGameListener(new GameListener() { @Override public void onGameOverEvent(GameOverEvent evt) { + timer.stop(); switch (evt.getGameState()) { case LOST: JOptionPane.showMessageDialog(mframe, "Game lost!"); @@ -128,13 +155,8 @@ public class Minesweeper { } }); - JButton btnRestart = new JButton("Restart"); - btnRestart.setHorizontalAlignment(SwingConstants.RIGHT); - headerPanel.add(btnRestart); - btnRestart.addActionListener((evt) -> board.reset()); - mframe.pack(); - loadScores(); + timer.start(); } private void createMenuBar() { @@ -179,9 +201,7 @@ public class Minesweeper { mframe.setJMenuBar(menubar); } - @SuppressWarnings( - "unchecked" - ) + @SuppressWarnings("unchecked") private void loadScores() { try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) { Object obj = in.readObject(); @@ -207,6 +227,8 @@ public class Minesweeper { private void initGame(BoardConfig config) { board.init(config); + gameTime = 0; + timer.restart(); mframe.pack(); } }