7 Commits

  1. 6
      src/main/java/Minesweeper/MinesweeperGame.java
  2. 2
      src/main/java/Minesweeper/Playfield.java
  3. 60
      src/main/java/Minesweeper/TimerLable.java

6
src/main/java/Minesweeper/MinesweeperGame.java

@ -8,11 +8,17 @@ public class MinesweeperGame extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final int WIDTH = 600, HEIGTH = 600; public static final int WIDTH = 600, HEIGTH = 600;
public Playfield playfield; public Playfield playfield;
public TimerLable tl;
public MinesweeperGame(int _playfieldSize, int _bombAmount) { public MinesweeperGame(int _playfieldSize, int _bombAmount) {
this.setSize(WIDTH, HEIGTH); this.setSize(WIDTH, HEIGTH);
setLayout(null); 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) { public static void main(String[] args) {

2
src/main/java/Minesweeper/Playfield.java

@ -69,6 +69,7 @@ public class Playfield {
MsG.remove(cells[i][j]); MsG.remove(cells[i][j]);
} }
} }
MsG.tl.reset();
generatePlayfield(); generatePlayfield();
} }
@ -140,6 +141,7 @@ public class Playfield {
} }
} }
} }
MsG.tl.stop();
MsG.repaint(); MsG.repaint();
} }

60
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();
}
}
Loading…
Cancel
Save