-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridButton.java
More file actions
177 lines (147 loc) · 4.38 KB
/
GridButton.java
File metadata and controls
177 lines (147 loc) · 4.38 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
public class GridButton extends JButton {
// declare instance variables
private int row;
private int col;
private int value;
private GUI gui;
private boolean isMine;
private boolean isFlag;
private MineSweeper game;
private Color baseBackground;
private boolean isRevealed;
private boolean isTan;
private boolean gameOver;
public GridButton(String text, int r, int c, MineSweeper game, GUI gui) {
// initialize instance variables
super(text);
row = r;
col = c;
isMine = false;
value = 0;
isFlag = false;
this.game = game;
this.gui = gui;
isRevealed = false;
// set default font for labeled buttons
Font largeFont = new Font("Arial", Font.BOLD, 24);
setFont(largeFont);
setPreferredSize(new Dimension(50, 50)); // preferred size of button
// darkens button when hovered over
addMouseListener(new MouseInputAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
if (gameOver) return;
if (!isRevealed) {
setBackground(baseBackground.darker());
}
}
// returns color to previous when mouse stops hovering
@Override
public void mouseExited(MouseEvent e) {
if (gameOver) return;
if (!isRevealed) {
setBackground(baseBackground);
}
}
// when a button is clicked
@Override
public void mouseClicked(MouseEvent e) {
if (gameOver) return; // don't allow interaction if game is over
// right click detected
if (e.getButton() == MouseEvent.BUTTON3) {
if (!isFlag && !isTan) {
gui.flag(row, col);
}
else {
gui.removeFlag(row, col);
}
}
// left click detected
else if (e.getButton() == MouseEvent.BUTTON1) {
gui.handleClick(GridButton.this);
}
}
});
}
// place a flag on the cell
public void flag() {
ImageIcon baseIcon = new ImageIcon("images/red_flag.png");
java.awt.Image icon = baseIcon.getImage().getScaledInstance(30, 30,
java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(icon);
setIcon(newIcon);
isFlag = true;
// check if flag placed correctly
if (game.isMine(row, col)) {
gui.addCorrectFlag();
}
else {
gui.subtractCorrectFlag();
}
}
// remove flag from cell
public void removeFlag() {
setIcon(null);
isFlag = false;
}
// set cell to a mine
public void setMine() {
isMine = true;
}
// return row
public int getRow() {
return row;
}
// return column
public int getCol() {
return col;
}
// return whether cell is a mine
public boolean isMine() {
return isMine;
}
// set value of cell
public void setValue(int val) {
value = val;
}
// return value of cell
public int getValue() {
return value;
}
// return whether cell is flagged
public boolean isFlag() {
return isFlag;
}
// change color of cell
public void setBaseBackground(Color col) {
baseBackground = col;
}
// turn the cell tan through color values from gui
public void reveal() {
isRevealed = true;
setBackground(gui.getTan(row, col));
setBaseBackground(gui.getTan(row, col));
}
// tan the cell and remove flag
public void tan() {
isTan = true;
removeFlag();
}
// reveal red mine on cell
public void revealMine() {
Color red = new Color(217, 33, 33);
setBackground(red);
setBaseBackground(red);
}
// set the game as over on button to prevent interaction
public void endGame() {
gameOver = true;
}
}