I have a minesweeper project that uses pngs to paint the icons for my cells. For some reason, my paint/draw methods will fill in all of the cells except mines. Everything is in the same directory. In my code, I override paintComponent(Graphics g) from java.awt.Graphics. I then paint the cells of my minefield based on their status "opened"/"flagged"/"covered" etc. enter image description here enter image description here
here is the pathway of methods that are involved in my painting process:
UPDATE: Here is a minimal reproducable example
- Play.java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Play
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
// game configs rows,cols,cellSize,#ofmines
int[] default_configs = {16,16,15,10};
Configuration.loadParameters(default_configs);
JFrame frame = new JFrame();
JLabel statusbar = new JLabel("select a cell");
frame.add(statusbar, BorderLayout.SOUTH);
frame.add(new Board(Configuration.ROWS, Configuration.COLS, Configuration.MINES, statusbar));
frame.setResizable(false);
frame.pack();
frame.setTitle("Ekrem's Minefield");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Board.java
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Dimension;
public class Board extends JPanel
{
private Minefield minefield;
private JLabel status;
private int height;
private int width;
private int mines;
/**
* Constructor that creates a minefield, initializes values, activates mouse, and sets up size of the board
*/
public Board(int height, int width, int mines, JLabel statusbar)
{
this.height = height;
this.width = width;
this.mines = mines;
this.minefield = new Minefield(height, width, mines);
status = statusbar;
setPreferredSize(new Dimension(Configuration.BOARD_WIDTH, Configuration.BOARD_HEIGHT));
addMouseListener(new MouseReader(this));
}
/**
* @param Graphics object
*/
@Override
public void paintComponent(Graphics g)
{
minefield.draw(g);
}
public Minefield getMinefield() {
return this.minefield;
}
/**
* this method adjusts the statuses of cells based on user interaction through a mouse
*/
public void mouseClickOnLocation(int x, int y, String button)
{
Object clickedCell = minefield.getCellByScreenCoordinates(x,y);
if (clickedCell instanceof InfoCell) {
if (button == "right") {
if (((InfoCell)(clickedCell)).getStatus().equals("opnd")) {
return;
}
else if (((InfoCell)(clickedCell)).getStatus().equals("mrkd")) {
((InfoCell)(clickedCell)).setStatus("cvrd");
}
else {
((InfoCell)(clickedCell)).setStatus("mrkd");
}
}
else if (button == "left") {
//minefield.openCells((InfoCell)(clickedCell));
}
}
if (clickedCell instanceof MineCell) {
if (button == "right") {
if(((MineCell)(clickedCell)).getStatus().equals("cvrd")) {
((MineCell)(clickedCell)).setStatus("mrkd");
}
else{
((MineCell)(clickedCell)).setStatus("cvrd");
}
}
else if (button == "left") {
((MineCell)(clickedCell)).setStatus("opnd");
}
}
}
}
Configuration.java
public class Configuration {
public static int ROWS;
public static int COLS;
public static int CELL_SIZE;
public static int MINES;
public static int BOARD_WIDTH;
public static int BOARD_HEIGHT;
public static int UNOPENED_INFOCELL_COUNT;
public static void loadParameters(int[] parameters){
ROWS = parameters[0];
COLS = parameters[1];
CELL_SIZE = parameters[2];
MINES = parameters[3];
UNOPENED_INFOCELL_COUNT = ROWS * COLS - MINES;
BOARD_WIDTH = COLS * CELL_SIZE + 1;
BOARD_HEIGHT = ROWS * CELL_SIZE + 1;
}
}
InfoCell.java
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Image;
public class InfoCell {
private int row;
private int column;
private int numOfAdjacentMines = 0;
private String status = "cvrd";
//covered, uncovered, marked, wrongly_marked
public InfoCell(int row, int column, int numOfAdjacentMines) {
this.row = row;
this.column = column;
this.numOfAdjacentMines = numOfAdjacentMines;
}
public int getRow() {
return this.row;
}
public int getCol() {
return this.column;
}
public void setMines(int mineCount) {
this.numOfAdjacentMines = mineCount;
}
public int getNumOfAdjacentMines() {
return this.numOfAdjacentMines;
}
public void draw(Graphics g) {
g.drawImage(this.getImage(), this.getHorizontalPosition(), this.getVerticalPosition(), null);
}
public Image getImage() {
Image image;
if (status.equals("cvrd")) {
ImageIcon a = new ImageIcon("img/covered_cell.png");
image = a.getImage();
}
else if(status.equals("mrkd")) {
ImageIcon a = new ImageIcon("img/marked_cell.png");
image = a.getImage();
}
else if(status.equals("wmrkd")){
ImageIcon a = new ImageIcon("img/wrong_mark.png");
image = a.getImage();
}
else /*(status.equals(Configuration.STATUS_OPENED))*/ {
ImageIcon a = new ImageIcon("img/info_" + this.numOfAdjacentMines + ".png");
image = a.getImage();
}
return image;
}
public int getHorizontalPosition() {
return (column) * Configuration.CELL_SIZE;
//Calculates and returns the pixel-level horizontal position of the top-left corner of the cell
}
public int getVerticalPosition() {
return (row) * Configuration.CELL_SIZE;
//Calculates and returns the pixel-level vertical position of the top-left corner of the cell
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
//Setter method for the current status of the cell. Do not hard-code the value;
//use a Configuration parameter instead.
this.status = status;
}
}
MineCell.java
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Graphics;
/**
*
* @author Ekrem Kaya
* @version 9:44 03/11
*/
public class MineCell {
/**
* row, columnm, and status of each cell
*/
private int row;
private int column;
private String status = "cvrd";
//covered - cvrd, uncovered - opnd, marked - mrkd
/**
*
* @param row vertical location of the Cell object in Minefield
* @param column horizantal poisition of the Cell object in Minefield
*/
public MineCell(int row, int column) {
this.row = row;
this.column = row;
}
/**
*
* @param g the Graphics object passed to the cell by the Minefield class
*/
public void draw(Graphics g) {
g.drawImage(this.getImage(), this.getHorizontalPosition(), this.getVerticalPosition(), null);
}
/**
*
* @return this method returns an in representing the horizantal pixel location
*/
public int getHorizontalPosition() {
return (column) * Configuration.CELL_SIZE;
//Calculates and returns the pixel-level horizontal position of the top-left corner of the cell
}
/**
*
* @return this method returns an in representing the vertical pixel location
*/
public int getVerticalPosition() {
return (row) * Configuration.CELL_SIZE;
//Calculates and returns the pixel-level vertical position of the top-left corner of the cell
}
/**
*
* @return this method returns the status attribute of the Cell which can be covered, open, wrongly marked, or marked
*/
public String getStatus() {
return this.status;
}
/**
*
* @param status the string that represents the status of the cell
*/
public void setStatus(String status) {
//Setter method for the current status of the cell. Do not hard-code the value;
//use a Configuration parameter instead.
this.status = status;
}
/**
*
* @return this returns the image from the folder that will represent the status of the cell
*/
public Image getImage() {
Image image;
if(this.status.equals("mrkd")) {
ImageIcon a = new ImageIcon("img/marked_cell.png");
image = a.getImage();
}
else if(this.status.equals("opnd")) {
ImageIcon a = new ImageIcon("img/mine_cell.png");
image = a.getImage();
}
else /*(status.equals(Configuration.STATUS_COVERED))*/ {
ImageIcon a = new ImageIcon("img/covered_cell.png");
image = a.getImage();
}
return image;
}
}
MouseReader.java
/**
* DO NOT EDIT ANYTHING IN THIS FILE
*/
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseReader extends MouseAdapter
{
private Board board;
public MouseReader(Board board)
{
this.board = board;
}
@Override
public void mouseClicked(MouseEvent e)
{
String button = "";
if (e.getButton() == MouseEvent.BUTTON1)
button = "left";
else if (e.getButton() == MouseEvent.BUTTON3)
button = "right";
board.mouseClickOnLocation(e.getX(), e.getY(), button);
}
}
The methods for non-mine cells and minecells are almost identical, the code ends up painting all cells except minecells. I have checked and found that each of the above methods do end up being called and that the status of the minecells are also correctly labeled.