import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.InputEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
public class Minesweeper
extends JFrame {
private int columns = 10;
private int rows = 10;
private static final long serialVersionUID = 1L;
// Stores which cells are bombs and which are not
boolean jBombs[][] = new boolean[rows][columns];
// Stores information on which cell has been displayed
boolean jShown[][] = new boolean[rows][columns];
// Stores information on surrounding cells
int jCells[][] = new int[rows][columns];
private int currX, currY = 0;
private JPanel jContentPane =
null;
private JButton jBtnNewGame =
null;
/**
* This is the default constructor
*/
public Minesweeper() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(436, 315);
this.setContentPane(getJPanel());
this.setTitle("Minesweeper");
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
if (jPanel == null) {
}
return jPanel;
}
/**
* This method initializes jToolBar
*
* @return javax.swing.JToolBar
*/
if (jToolBar == null) {
jToolBar.setFloatable(false);
jToolBar.add(getJBtnNewGame());
}
return jToolBar;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane
() {
if (jContentPane == null)
{
gridLayout.setRows(rows);
gridLayout.setColumns(columns);
jContentPane.setLayout(gridLayout);
BuildBoard();
}
return jContentPane;
}
private void BuildBoard()
{
// JContentPane will call this function before jProgressBar was built when first started
if(jProgressBar != null) // So we check to see if the progress bar has been initialized
{
jProgressBar.setValue(0);
}
jContentPane.removeAll();
int i = 0;
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
currX = x;
currY = y;
jBombs[x][y] = randBomb.nextBoolean() && randBomb.nextBoolean() && randBomb.nextBoolean(); // 13% chances of a bomb
jButtons
[i
].
addMouseListener(new java.
awt.
event.
MouseAdapter(){
public void mouseReleased
(java.
awt.
event.
MouseEvent e
) {
{
markCell(e);
}
else if(e.
getModifiers() ==
InputEvent.
BUTTON1_MASK)
{
showCell(e);
}
}
});
jContentPane.add(jButtons[i]);
i++;
}
}
// Build the board
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
jCells[x][y] = bombCount(x, y);
jShown[x][y] = false; // Reset previous values
}
}
jContentPane.setEnabled(true);
this.repaint();
this.validate();
}
/**
* This method initializes jBtnNewGame
*
* @return javax.swing.JButton
*/
if (jBtnNewGame == null) {
jBtnNewGame.setText("New Game");
jBtnNewGame.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
public void mouseReleased
(java.
awt.
event.
MouseEvent e
) {
BuildBoard();
}
});
}
return jBtnNewGame;
}
/**
* This method initializes jProgressBar
*
* @return javax.swing.JProgressBar
*/
if (jProgressBar == null) {
jProgressBar.setMaximum(columns * rows);
}
return jProgressBar;
}
public static void main
(String args
[])
{
}
// Displays all cells marked as bombs
private void showAllBombs()
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < columns; y++)
{
if(jBombs[x][y] == true)
{
if(jButton.isEnabled()) // Don't go over the ones that were already counted
{
jProgressBar.setValue(jProgressBar.getValue() + 1);
}
jButton.setText("X");
jButton.setSelected(true);
jButton.setEnabled(false);
}
}
}
}
private void clearCells(int x, int y)
{
// If the cell is in bounds