-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameButton.java
96 lines (87 loc) · 2.96 KB
/
GameButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@SuppressWarnings("serial")
public class GameButton extends JButton {
/**
* For use, pass a number to setIcon(int). Use the public constants below
* for access to the non sequential icons, and otherwise, pass in the number
* of adjacent mines (0-8) and the button will display the proper numeric icon
*/
public static final int BLANK = 0, BANG = 9, BOMB = 10, FLAG = 11, F_FLAG = 12;
private final Color disabledColor = Color.DARK_GRAY, enabledColor = Color.LIGHT_GRAY;
private static String aDir = "assets/buttonIcons/";
private ImageIcon displayIcon;
private static ImageIcon[] buttonIcons =
//Numeric icons 0 is null, 1-8 are accurately named
{null, new ImageIcon(aDir+"1.png"), new ImageIcon(aDir+"2.png"),
new ImageIcon(aDir+"3.png"), new ImageIcon(aDir+"4.png"),
new ImageIcon(aDir+"5.png"), new ImageIcon(aDir+"6.png"),
new ImageIcon(aDir+"7.png"), new ImageIcon(aDir+"8.png"),
//Bomb-based icons and flags
new ImageIcon(aDir+"explosion.png"), new ImageIcon(aDir+"bomb.png"),
new ImageIcon(aDir+"p_bomb.png"), new ImageIcon(aDir+"wrong.png")}; /*For flag, question, mine*/
//Settings for button's type and location
public ButtonType type;
public int x, y;
public boolean flagged, revealed;
/**Extended JButton with coordinates.
*
* @param x X coordinate in a grid
* @param y Y coordinate in a grid
*/
public GameButton(int x, int y){
super();
this.x = x;
this.y = y;
this.type = ButtonType.SAFE;
this.setBackground(Color.LIGHT_GRAY);
flagged = false;
revealed = false;
}
/**
* Set the icon of the current button based on the passed index.
* Self-explanatory constants available for use include:
* GameButton.BLANK, GameButton.BANG,
* GameButton.BOMB, GameButton.FLAG,
* and GameButton.F_FLAG
* Other than the above, pass the number of nearby mines
* into this method to set the icon of the button to an
* appropriate label
*/
public void setIcon(int index){
if(index != BLANK){
displayIcon = new ImageIcon(buttonIcons[index].getImage()
.getScaledInstance(getWidth(), getHeight(), java.awt.Image.SCALE_SMOOTH));
} else{
displayIcon = buttonIcons[BLANK];
}
setIcon(displayIcon);
setDisabledIcon(displayIcon);
//Qmark and blank can be clicked. Otherwise, disabled color
if(!flagged){
deactivate();
}
revalidate();
}
/** @return True if this is a mine */
public boolean isMine() {
return this.type == ButtonType.MINE;
}
/** Sets this button as a known mine */
public void plantMine(){
this.type = ButtonType.MINE;
}
/** Disabler function */
public void deactivate(){
this.setEnabled(false);
this.setBackground(disabledColor);
revealed = true;
}
/** Enabler function */
public void activate(){
this.setEnabled(true);
this.setBackground(enabledColor);
revealed = flagged = false;
}
}