Browse Source

Merge commit '520c5dc3a0a5320891b6b01374ee9101ca02c4bd' into HEAD

Minesweeper_Game
Jenkins 3 years ago
parent
commit
0262d65cea
  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;
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);
tl = new TimerLable();
tl.setBounds((WIDTH / 2 - 5), HEIGTH / 2 - 240, 20, 20);
add(tl);
tl.start();
}
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.tl.reset();
generatePlayfield();
}
@ -140,6 +141,7 @@ public class Playfield {
}
}
}
MsG.tl.stop();
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