How do i put a flag on location that the user right click on it. and how do i put an img to all mine in game board when game is end? I try to put an img but wont work? How can i fix it? here is what a so far
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MineSweeper2
{
private static final int NUMBER_MINES = 150;
private static final int SIZE = 20;
public static void main(String[] args)
{
JFrame frame = new JFrame("Welcome to Mine Sweeper Game!");
// JFrame frame2 = new JFrame (" \n # of mines: " + NUM_MINES );
//frame.setBackground(Color.green);
// ButtonHandler (SIZE,SIZE);
frame.add(new MineSweeperGUI(SIZE, SIZE, NUMBER_MINES));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,500);
frame.setVisible(true);
}
}
class MineSweeperGUI extends JPanel
{
private MineGrid grid;
public MineSweeperGUI(int numRows, int numCols, int numMines)
{
grid = new MineGrid(numRows, numCols, numMines);
setLayout(new GridLayout(numRows, numCols));
for(int index = 0; index < numRows; index++)
{
for(int col = 0; col < numCols; col++)
{
JButton button = new JButton();
add(button);
button.addActionListener(new ButtonHandler(index,col, grid));
}
}
}
}
class ButtonHandler implements ActionListener
{
private int row, col;
private MineGrid grid;
// private int rsize,csize;
public ButtonHandler(int x, int y, MineGrid g)
{
row = x;
col = y;
grid = g;
}
/* public ButtonHandler (int size, int size2)
{
rsize = size;
csize = size2;
}*/
public void actionPerformed(ActionEvent event)
{
if(grid.containsMineAt(row, col))
{
Image img = new ImageIcon (this.getClass().getResource("/mine.png")).getImage();
JOptionPane.showMessageDialog(null, "You step on mine. You die! \n Welcome to the haven");
System.exit(0);
}
else
{
JButton button = (JButton)event.getSource();
button.setText(String.valueOf(grid.getInfoAt(row, col)));
}
}
// int click = 0;
// int totalbutton = size * col;
}
class MineGrid
{
public static final int MINE = -1;
protected int[][] informationaboutmine;
// mine information is either MINE, meaning there is a mine
// at that cell, or it keeps the number of surrounding mines.
public MineGrid(int numRows, int numCols, int numMines)
{
informationaboutmine = new int[numRows][numCols];
initializeCells();
placeMines(numMines);
setinformationaboutmine();
}
private void initializeCells()
{
for(int index = 0; index < informationaboutmine.length; index ++)
{
for(int col = 0; col < informationaboutmine[0].length; col++)
{
informationaboutmine[index][col] = 0;
}
}
}
private void placeMines(int numMines)
{
Random random = new Random();
for(int index = 0; index < numMines; index++)
{
int r = random.nextInt(informationaboutmine.length);
int c = random.nextInt(informationaboutmine[0].length);
informationaboutmine[r][c] = MINE;
}
}
private void setinformationaboutmine()
{
for(int index = 0; index < informationaboutmine.length; index++)
{
for(int col = 0; col < informationaboutmine[0].length; col++)
{
if(informationaboutmine[index][col] == MINE)
{
// previous row
incrementMineCountAt(index-1, col-1);
incrementMineCountAt(index-1, col);
incrementMineCountAt(index-1, col+1);
// left and right cells
incrementMineCountAt(index, col-1);
incrementMineCountAt(index, col+1);
// next row
incrementMineCountAt(index+1, col-1);
incrementMineCountAt(index+1, col);
incrementMineCountAt(index+1, col+1);
}
}
}
}
private void incrementMineCountAt(int index, int col)
{
if(isInsideGrid(index, col) && informationaboutmine[index][col] != MINE)
{
informationaboutmine[index][col]++;
}
}
private boolean isInsideGrid(int index, int col)
{
return (index >= 0 && index < informationaboutmine.length) &&
(col >= 0 && col < informationaboutmine[0].length);
}
public int getInfoAt(int index, int col)
{
return informationaboutmine[index][col];
}
public boolean containsMineAt(int index, int col)
{
return getInfoAt(index, col) == MINE;
}
}