I'm writing a Minesweeper game for class. I have two radio buttons at the bottom of my JFrame that say "Test Square" and "Mark Mine". In another class titled "Square" I have an SquareListener that extends ActionListener that I want to activate when I have the "Test Square" radio button clicked. I guess I'll have to create another ActionListener for when "Mark Mine" is clicked. How can I do this?
I have an if-statement, but I don't know what to put in it.
if (testSquare.isSelected())
Here are the codes for both classes (MineSweeper and Square) in my program if it might help.
MineSweeper:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MineSweeper extends JFrame{
private static final long serialVersionUID = 1L;
final static int boardSize = 10;
public static Square[][] board;
public static int r;
public static int c;
public static int mineNumber;
JRadioButton testSquare = new JRadioButton("Test Square");
JRadioButton markMine = new JRadioButton("Mark Mine");
JPanel mainPanel = new JPanel();
static JPanel radioPanel = new JPanel();
public MineSweeper()
{
int r,c;
//windowPanel.setLayout(new BorderLayout());
//add(this, BorderLayout.CENTER);
board = new Square[boardSize+2][boardSize+2];
for (r=1; r<=boardSize; r++)
for (c=1; c<=boardSize; c++)
{
if (Math.random()*100 < 15){
board[r][c] = new Square(r, c, true, false, board);
mineNumber ++;
}
else
board[r][c] = new Square(r, c, false, false, board);
mainPanel.add(board[r][c]);
}
for (r=0; r<boardSize+2; r++)
{
board[r][0] = new Square(r,0,false, true, board);
board[r][boardSize+1] = new Square(r,0,false, true, board);
board[0][r] = new Square(0,r,false, true, board);
board[boardSize+1][r] = new Square(0,r,false, true, board);
}
for (r=1; r<=boardSize; r++)
for (c=1; c<=boardSize; c++)
{
if (board[r][c].isMine()){
board[r+1][c+1].incSurrMines();
board[r+1][c].incSurrMines();
board[r+1][c-1].incSurrMines();
board[r][c+1].incSurrMines();
board[r][c-1].incSurrMines();
board[r-1][c+1].incSurrMines();
board[r-1][c].incSurrMines();
board[r-1][c-1].incSurrMines();
}
}
this.setLayout(new BorderLayout());
mainPanel.setLayout(new GridLayout(boardSize,boardSize));
add(mainPanel, BorderLayout.CENTER);
add(radioPanel, BorderLayout.SOUTH);
JRadioButton testSquare = new JRadioButton("Test Square", true);
if(testSquare.isSelected()){
}
JRadioButton markMine = new JRadioButton("Mark Mine", false);
if(markMine.isSelected()){
}
ButtonGroup group = new ButtonGroup();
group.add(testSquare);
group.add(markMine);
JLabel mineCountLabel = new JLabel(" Mine Count: "+mineNumber);
radioPanel.add(testSquare);
radioPanel.add(markMine);
radioPanel.add(mineCountLabel);
setTitle("Mine Sweeper");
setSize(50*boardSize,50*boardSize+50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
// Move the window
this.setLocation(x, y);
setVisible(true);
}
public static void showMines()
{
for (r=1; r<=boardSize; r++)
for (c=1; c<=boardSize; c++)
{
if (board[r][c].isMine()){
board[r][c].setBackground(Color.RED);
board[r][c].setText("X");
}
}
}
public class SquareListener implements ActionListener{
private Square mySquare;
private Square[][] board;
public SquareListener(Square s, Square[][] b){
mySquare = s;
board = b;
}
public void actionPerformed(ActionEvent e)
{
// Fill in code here to handle stepping on this square
if (mySquare.isMine()){
mySquare.setBackground(Color.RED);
mySquare.setText("X");
MineSweeper.showMines();
JOptionPane.showMessageDialog(null, "Game Over");
}
else {
mySquare.setSteppedOn();
if (mySquare.getSurrMines()!=0)
mySquare.setText(""+mySquare.getSurrMines());
mySquare.setBackground(Color.LIGHT_GRAY);
if (mySquare.getSurrMines()==0)
{
// Click on the surrounding mines
for (int i = MineSweeper.r-1; i <= MineSweeper.r+1; i++)
for (int j = MineSweeper.c-1; i <= MineSweeper.c + 1; i++){
if(!board[i][j].isMine())
board[i][j].doClick();
}
/*
board[MineSweeper.r-1][MineSweeper.c-1].doClick();
board[MineSweeper.r-1][MineSweeper.c].doClick();
board[MineSweeper.r-1][MineSweeper.c+1].doClick();
board[MineSweeper.r][MineSweeper.c-1].doClick();
board[MineSweeper.r][MineSweeper.c+1].doClick();
board[MineSweeper.r+1][MineSweeper.c-1].doClick();
board[MineSweeper.r+1][MineSweeper.c].doClick();
board[MineSweeper.r+1][MineSweeper.c+1].doClick();
*/
}
if (mySquare.getSurrMines()==1)
mySquare.setForeground(Color.BLUE);
else if (mySquare.getSurrMines()==2)
mySquare.setForeground(Color.MAGENTA);
else if (mySquare.getSurrMines()==3)
mySquare.setForeground(Color.RED);
else if (mySquare.getSurrMines()>3)
mySquare.setForeground(Color.DARK_GRAY);
}
}
}
public static void main(String[] args) {
new MineSweeper();
}
}
Square:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Square extends JButton{
private boolean mine = false;
private int surrMines = 0;
private boolean steppedOn = false;
private int row, col;
public Square(int r, int c, boolean m, Square[][] b)
{
row = r;
col = c;
mine = m;
setText("");
addActionListener(new SquareListener(this, b));
}
public boolean isSteppedOn()
{
return steppedOn;
}
public void setSteppedOn()
{
steppedOn = true;
}
public int getSurrMines()
{
return surrMines;
}
public boolean isMine()
{
return mine;
}
public void incSurrMines()
{
surrMines++;
}
private class SquareListener implements ActionListener {
private Square mySquare;
private Square[][] board;
public SquareListener(Square s, Square[][] b){
mySquare = s;
board = b;
}
public void actionPerformed(ActionEvent e)
{
if (mySquare.isSteppedOn()) return;
// Fill in code here to handle stepping on this square
if (mySquare.isMine())
mySquare.setBackground(Color.RED);
else
{
mySquare.setSteppedOn();
if (surrMines>0) mySquare.setText(""+surrMines);
mySquare.setBackground(Color.LIGHT_GRAY);
if (mySquare.getSurrMines()==0)
{
// Click on the surrounding mines
board[mySquare.row-1][mySquare.col-1].doClick();
board[mySquare.row-1][mySquare.col].doClick();
board[mySquare.row-1][mySquare.col+1].doClick();
board[mySquare.row][mySquare.col-1].doClick();
board[mySquare.row][mySquare.col+1].doClick();
board[mySquare.row+1][mySquare.col-1].doClick();
board[mySquare.row+1][mySquare.col].doClick();
board[mySquare.row+1][mySquare.col+1].doClick();
}
}
}
}
}