From 665d7c123240023c3e7117b7b5034c3620477464 Mon Sep 17 00:00:00 2001 From: kfkama Date: Wed, 16 Feb 2022 18:27:51 +0100 Subject: [PATCH] Add Cell to Minesweeper, replacing JButtons / Move playfield to center --- src/main/java/Minesweeper/Cell.java | 9 +++++---- src/main/java/Minesweeper/MinesweeperGame.java | 14 ++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 594c53d..12a1935 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -1,7 +1,11 @@ package Minesweeper; +import javax.swing.JButton; + enum CellType{Number, Bomb} -public class Cell { +public class Cell extends JButton { + + private static final long serialVersionUID = 1L; public CellType type; public boolean flagged = false; public int value = -1; @@ -9,7 +13,4 @@ public class Cell { public Cell(CellType _type) { type = _type; } - - - } diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index a7764e3..8be5353 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/src/main/java/Minesweeper/MinesweeperGame.java @@ -11,20 +11,26 @@ public class MinesweeperGame extends JPanel { private static final int CELLSIZE = 40; private int playfieldSize; + public Cell[][] playfield; + public MinesweeperGame(int _playfieldSize) { this.setSize(WIDTH, HEIGTH); playfieldSize = _playfieldSize; setLayout(null); - + initPlayfield(); } private void initPlayfield() { + + playfield = new Cell[playfieldSize][playfieldSize]; + for (int i = 0; i < playfieldSize; i++) { 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); } } }