diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 8293a18..cf07347 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -8,11 +8,17 @@ public class MinesweeperGame extends JPanel { private static final long serialVersionUID = 1L; public static final int WIDTH = 600, HEIGTH = 600; public Playfield playfield; + public TimerLable tl; public MinesweeperGame(int _playfieldSize, int _bombAmount) { this.setSize(WIDTH, HEIGTH); setLayout(null); - playfield = new Playfield(this, _playfieldSize, _bombAmount ); + playfield = new Playfield(this, _playfieldSize, _bombAmount); + + tl = new TimerLable(); + tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20); + add(tl); + tl.start(); } public static void main(String[] args) { diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 9e73cb8..29dde34 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -69,6 +69,7 @@ public class Playfield { MsG.remove(cells[i][j]); } } + MsG.tl.reset(); generatePlayfield(); } @@ -140,6 +141,7 @@ public class Playfield { } } } + MsG.tl.stop(); MsG.repaint(); } diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java new file mode 100644 index 0000000..68a1ea9 --- /dev/null +++ b/src/main/java/Minesweeper/TimerLable.java @@ -0,0 +1,60 @@ +package Minesweeper; + +import java.util.Timer; +import java.util.TimerTask; + +import javax.swing.JLabel; + +public class TimerLable extends JLabel { + + private static final long serialVersionUID = 1L; + protected int counter = 0; + private Helper task; + + public void start() { + Timer timer = new Timer(); + task = new Helper(this); + + timer.schedule(task, 0, 1000); + } + + public void update() { + setText(String.valueOf(++counter)); + } + + public void reset() { + task.reset = true; + counter = 0; + setText(String.valueOf(counter)); + repaint(); + task.stop = false; + } + + public void stop() { + task.stop = true; + } +} + + +class Helper extends TimerTask +{ + public boolean reset; + public boolean stop; + public static int i = 0; + private TimerLable timerLable; + + public Helper(TimerLable _timerLable) { + timerLable = _timerLable; + } + public void run() + { + if(stop) { + return; + } + if(reset) { + reset = false; + timerLable.counter = 0; + } + timerLable.update(); + } +} \ No newline at end of file