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;
}
}

Recommended Answers

All 19 Replies

You could set the ImageIcon.

ImageIcon icon = new ImageIcon("image.png");
JButton button = new JButton(icon);

This is minesweeper I only want to picuter of all mine display when user click on mine... Not defult button

It looks like your grid is displayed as a grid of JButtons, so do as Phaelax says and set the appropriate icons for each button ... button.setIcon(icon)

What James said but I think you need to do it inside actionPerformed, so that is actually happening when a button is pressed not as a default image

commented: Yes, that's what I intended +15

Where do I set. I having bedugg this code for 5 hours still couldn't find out or where to put can some help me

For the flag - set the flag icon for the button in the actionPerformed (like slavi said). Ie when the user clicks a button, set that button;s icon to the flag.

At the end of the game, loop through all the buttons and if they have a mine, set their icon to the mine icon

When i do this it wont work at all

 public void actionPerformed(ActionEvent event)
    {
    if(grid.containsMineAt(row, col))
    {

       // button.setText(String.valueOf(grid.getInfoAt(row, col)));
        ImageIcon icon = new ImageIcon("image.png");
        JButton button = new JButton(icon);
    //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)));
    }

You do not want to create a new button when you hit a mine but use the button at position(row,col) to set it's icon as James has shown earlier button.setIcon(icon)

Also, check whether your method containsMinesAt(row,col) actually works by printing out some string to the terminal if a mine is found at that button, unless you have done it already ;)

 I am confuse 

 ImageIcon icon = new ImageIcon("image.png");
    button.setIcon(icon)
 if(grid.containsMineAt(row, col))
{

ImageIcon icon = new ImageIcon("image.png");
button,setIcon(icon)
//But it say i dont button?
//This is right place to put it?
JOptionPane.showMessageDialog(null, "You step on mine. You die! \n Welcome to the haven");
System.exit(0);
}

my mine is working 100%. I test it before I test the img

The problem that you need to solve now is how do you know which button to set the Icon of :)

ok. I have the button as CONSTANT number! Where do I put img code at ? In containsMineAT or at Action...

so the user can change how many mine there are and how many button that they want

At Action as you want it to change the image only after a button that holds one is pressed, but I think an approach of getting which button was pressed could be using the event object that you are passing, if I recall(been using JavaFX over swing for awhile) it was someting like
event.getSource().setIcon(image); //as the source of the event is a button

After I put this code in:

 public void actionPerformed(ActionEvent event) 
    {

        if(grid.containsMineAt(row, col)) 
        {
            setIcon (null);
            JOptionPane.showMessageDialog(null, "You step on mine. You die! \n Welcome to the haven");
            System.exit(0);
        }
    }
private void setIcon(Image image) 
{
    //This is an error that eclipse tell me 
    setIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("mine.png")));}

}
Error?
at ButtonHandler.setIcon(MineSweeper2.java:91)

what is wrong?

        ((JOptionPane) event.getSource()).setIcon(mine.png);

I got an error at mine.png if i put into " " it say error w/o any help

Can you show us your currect full code?

I didnt change anything. My full is at 1 post. I just add couple lines for picture to show up when user step on mine. But it didnt work at all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.