cs501b:cs-501b-su13_minesweeper_square_gui
Minesweeper Square Class with GUI Components
The following is a Minesweeper square class with GUI components.
/**
* Represents a single square in Minesweeper.
*
* @author J. Chung
* @version CS-501B
*/
// GUI imports:
import javax.swing.*;
public class MineSquare
{
// Fields:
private String _shown; // What a square is showing now
private boolean _mined; // Square is mined or not
private boolean _flagged; // Square is flagged or not
private boolean _questioned; // Square is question marked or not
private int _minecount; // Square's surrounding mine count
private boolean _opened; // Player has opened this square or not
// GUI fields:
private JButton _button; // Use when square not opened; displays " " or "F" (flag)
private JLabel _label; // Use when square opened; displays " " or _minecount
/**
* Default constructor
*
* Sets _mined and _opened to false.
*/
public MineSquare()
{
_mined = false;
_opened = false;
// GUI initialization:
_button = new JButton(" ");
_label = new JLabel(" ");
}
/**
* Returns flagged status of square.
*
* @return _flagged Flagged status
*/
public boolean isFlagged() {
return _flagged;
}
/**
* Sets or unsets flag on a square.
*
* @param _flagged True or false (square is flagged or not)
*/
public void setFlagged(boolean _flagged) {
this._flagged = _flagged;
// If flagged, square should show "F":
if ( isFlagged() == true )
setShown("F");
else
setShown(" ");
}
public int getMinecount() {
return _minecount;
}
public void setMinecount(int _minecount) {
this._minecount = _minecount;
}
public boolean isMined() {
return _mined;
}
public void setMined(boolean _mined) {
this._mined = _mined;
}
public boolean isOpened() {
return _opened;
}
/**
* Open a square.
* (A square can't be unopened.)
*/
public void setOpened() {
this._opened = true;
if ( isMined() == true )
setShown("X");
else if ( getMinecount() > 0 )
setShown(_minecount + "");
else // blank space for _minecount = 0
setShown(" ");
}
public boolean isQuestioned() {
return _questioned;
}
public void setQuestioned(boolean _questioned) {
this._questioned = _questioned;
}
public String getShown() {
return _shown;
}
public void setShown(String _shown) {
this._shown = _shown;
}
// GUI methods:
/**
* Return the "face" of the MineSquare.
*
* @return _button if unopened or _label if opened
*/
public JButton getFace()
{
return _button;
}
}
cs501b/cs-501b-su13_minesweeper_square_gui.txt · Last modified: by jchung
