Browse Source

Add Cell to Minesweeper, replacing JButtons / Move playfield to center

feature_Minesweeper_Playfield
kfkama 3 years ago
parent
commit
665d7c1232
  1. 9
      src/main/java/Minesweeper/Cell.java
  2. 14
      src/main/java/Minesweeper/MinesweeperGame.java

9
src/main/java/Minesweeper/Cell.java

@ -1,7 +1,11 @@
package Minesweeper; package Minesweeper;
import javax.swing.JButton;
enum CellType{Number, Bomb} enum CellType{Number, Bomb}
public class Cell {
public class Cell extends JButton {
private static final long serialVersionUID = 1L;
public CellType type; public CellType type;
public boolean flagged = false; public boolean flagged = false;
public int value = -1; public int value = -1;
@ -9,7 +13,4 @@ public class Cell {
public Cell(CellType _type) { public Cell(CellType _type) {
type = _type; type = _type;
} }
} }

14
src/main/java/Minesweeper/MinesweeperGame.java

@ -11,20 +11,26 @@ public class MinesweeperGame extends JPanel {
private static final int CELLSIZE = 40; private static final int CELLSIZE = 40;
private int playfieldSize; private int playfieldSize;
public Cell[][] playfield;
public MinesweeperGame(int _playfieldSize) { public MinesweeperGame(int _playfieldSize) {
this.setSize(WIDTH, HEIGTH); this.setSize(WIDTH, HEIGTH);
playfieldSize = _playfieldSize; playfieldSize = _playfieldSize;
setLayout(null); setLayout(null);
initPlayfield(); initPlayfield();
} }
private void initPlayfield() { private void initPlayfield() {
playfield = new Cell[playfieldSize][playfieldSize];
for (int i = 0; i < playfieldSize; i++) { for (int i = 0; i < playfieldSize; i++) {
for (int j = 0; j < playfieldSize; j++) { for (int j = 0; j < playfieldSize; j++) {
JButton b = new JButton();
b.setBounds(j * CELLSIZE, i * CELLSIZE, CELLSIZE, CELLSIZE);
add(b);
Cell c = new Cell(CellType.Number);
c.setBounds(j * CELLSIZE + (WIDTH / 2 - playfieldSize * CELLSIZE / 2), i * CELLSIZE + (HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE,
CELLSIZE);
add(c);
} }
} }
} }

Loading…
Cancel
Save