From f902c3550b2ab4c37492b10a89dc97a195d98b7a Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:02:43 +0100 Subject: [PATCH 1/6] Add TimerLable class with basic logic --- .../java/Minesweeper/MinesweeperGame.java | 2 +- src/main/java/Minesweeper/TimerLable.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Minesweeper/TimerLable.java diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 8293a18..f31d319 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -12,7 +12,7 @@ public class MinesweeperGame extends JPanel { public MinesweeperGame(int _playfieldSize, int _bombAmount) { this.setSize(WIDTH, HEIGTH); setLayout(null); - playfield = new Playfield(this, _playfieldSize, _bombAmount ); + playfield = new Playfield(this, _playfieldSize, _bombAmount); } public static void main(String[] args) { diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java new file mode 100644 index 0000000..a194bbf --- /dev/null +++ b/src/main/java/Minesweeper/TimerLable.java @@ -0,0 +1,38 @@ +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; + private int counter = 0; + + public void start() { + Timer timer = new Timer(); + TimerTask task = new Helper(this); + + timer.schedule(task, 0, 1000); + } + + public void update() { + setText(String.valueOf(++counter)); + } +} + + +class Helper extends TimerTask +{ + public static int i = 0; + private TimerLable timerLable; + + public Helper(TimerLable _timerLable) { + timerLable = _timerLable; + } + public void run() + { + timerLable.update(); + } +} \ No newline at end of file From 1a8df9a1a9c3b145d5b618d763a50afa49b3f011 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:03:24 +0100 Subject: [PATCH 2/6] Display TimerLable --- src/main/java/Minesweeper/MinesweeperGame.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index f31d319..1741988 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -13,6 +13,10 @@ public class MinesweeperGame extends JPanel { this.setSize(WIDTH, HEIGTH); setLayout(null); playfield = new Playfield(this, _playfieldSize, _bombAmount); + TimerLable tl = new TimerLable(); + tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20); + add(tl); + tl.start(); } public static void main(String[] args) { From 8128061c09986773d9a3c684ef4e9c77dca2f236 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:07:22 +0100 Subject: [PATCH 3/6] Add reset to TimerLable --- src/main/java/Minesweeper/TimerLable.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java index a194bbf..ae08055 100644 --- a/src/main/java/Minesweeper/TimerLable.java +++ b/src/main/java/Minesweeper/TimerLable.java @@ -8,11 +8,12 @@ import javax.swing.JLabel; public class TimerLable extends JLabel { private static final long serialVersionUID = 1L; - private int counter = 0; + protected int counter = 0; + private Helper task; public void start() { Timer timer = new Timer(); - TimerTask task = new Helper(this); + task = new Helper(this); timer.schedule(task, 0, 1000); } @@ -20,12 +21,18 @@ public class TimerLable extends JLabel { public void update() { setText(String.valueOf(++counter)); } + + public void reset() { + task.reset = true; + } + } class Helper extends TimerTask { - public static int i = 0; + public boolean reset; + public static int i = 0; private TimerLable timerLable; public Helper(TimerLable _timerLable) { @@ -33,6 +40,10 @@ class Helper extends TimerTask } public void run() { + if(reset) { + reset = false; + timerLable.counter = 0; + } timerLable.update(); } } \ No newline at end of file From 1a7908fa2a0b1e43882cdbb9f0eeee0530a5328e Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:09:28 +0100 Subject: [PATCH 4/6] Reset timer on game end --- src/main/java/Minesweeper/MinesweeperGame.java | 4 +++- src/main/java/Minesweeper/Playfield.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index 1741988..cf07347 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -8,12 +8,14 @@ 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); - TimerLable tl = new TimerLable(); + + tl = new TimerLable(); tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20); add(tl); tl.start(); diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 9e73cb8..425f4f2 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(); } From d6f1baffe9cc878be102568f9f0f9e038cafc0b1 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:18:46 +0100 Subject: [PATCH 5/6] Timer stops while game end dialog is open --- src/main/java/Minesweeper/Playfield.java | 1 + src/main/java/Minesweeper/TimerLable.java | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index 425f4f2..29dde34 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/src/main/java/Minesweeper/Playfield.java @@ -141,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 index ae08055..750e417 100644 --- a/src/main/java/Minesweeper/TimerLable.java +++ b/src/main/java/Minesweeper/TimerLable.java @@ -24,14 +24,19 @@ public class TimerLable extends JLabel { public void reset() { task.reset = true; + 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; @@ -40,6 +45,9 @@ class Helper extends TimerTask } public void run() { + if(stop) { + return; + } if(reset) { reset = false; timerLable.counter = 0; From 520c5dc3a0a5320891b6b01374ee9101ca02c4bd Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 20:19:19 +0100 Subject: [PATCH 6/6] Bugfix timer reset --- src/main/java/Minesweeper/TimerLable.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/Minesweeper/TimerLable.java b/src/main/java/Minesweeper/TimerLable.java index 750e417..68a1ea9 100644 --- a/src/main/java/Minesweeper/TimerLable.java +++ b/src/main/java/Minesweeper/TimerLable.java @@ -24,6 +24,9 @@ public class TimerLable extends JLabel { public void reset() { task.reset = true; + counter = 0; + setText(String.valueOf(counter)); + repaint(); task.stop = false; }