Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/com/zetcode/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
Expand Down Expand Up @@ -33,14 +34,12 @@ public class Board extends JPanel {

private final int BOARD_WIDTH = N_COLS * CELL_SIZE + 1;
private final int BOARD_HEIGHT = N_ROWS * CELL_SIZE + 1;

private final JLabel statusbar;
private int[] field;
private boolean inGame;
private int minesLeft;
private Image[] img;

private int allCells;
private final JLabel statusbar;

public Board(JLabel statusbar) {

Expand All @@ -56,8 +55,9 @@ private void initBoard() {

for (int i = 0; i < NUM_IMAGES; i++) {

var path = "src/resources/" + i + ".png";
img[i] = (new ImageIcon(path)).getImage();
var path = "resources/" + i + ".png"; //made src as source folder, now this path should be correct
img[i] = (new ImageIcon(Objects.requireNonNull(getClass().getClassLoader().getResource(
path)))).getImage(); //this way it should also show images when executing remote jar file or class from terminal
}

addMouseListener(new MinesAdapter());
Expand All @@ -80,7 +80,7 @@ private void newGame() {
field[i] = COVER_FOR_CELL;
}

statusbar.setText(Integer.toString(minesLeft));
statusbar.setText(minesLeft + " mines left"); //not a big change)

int i = 0;

Expand Down Expand Up @@ -331,7 +331,12 @@ public void mousePressed(MouseEvent e) {
if (minesLeft > 0) {
field[(cRow * N_COLS) + cCol] += MARK_FOR_CELL;
minesLeft--;
String msg = Integer.toString(minesLeft);
String msg;
if (minesLeft == 1) { // just for interface purpose
msg = "1 mine left";
} else {
msg = minesLeft + " mines left";
}
statusbar.setText(msg);
} else {
statusbar.setText("No marks left");
Expand All @@ -340,7 +345,12 @@ public void mousePressed(MouseEvent e) {

field[(cRow * N_COLS) + cCol] -= MARK_FOR_CELL;
minesLeft++;
String msg = Integer.toString(minesLeft);
String msg;
if (minesLeft == 1) {
msg = "1 mine left";
} else {
msg = minesLeft + " mines left";
}
statusbar.setText(msg);
}
}
Expand Down