-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardClass.java
348 lines (316 loc) · 11.4 KB
/
BoardClass.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.imageio.*;
import java.net.URL;
import java.applet.*;
/*
* This class checks the Minesweeper and interacts with it.
*/
public class BoardClass extends JApplet{
//-----------------------------------------------------------------
//---------------------Instance Variables--------------------------
//-----------------------------------------------------------------
//new game board object to produce all the functionality
Minesweeper mineSweeper = new Minesweeper(15,20,22);
int rows = 0;
int columns = 0;
int score = mineSweeper.getBombs();
JLabel scoreCount;
JLabel timeCount;
JButton resetFace;
javax.swing.Timer timeCounter;
int time = 0;
//-----------------------------------------------------------------
//------------------------Constructor------------------------------
//-----------------------------------------------------------------
public void init(){
rows = mineSweeper.getRows();
columns = mineSweeper.getColumns();
// Dimension dimension = new Dimension(getPreferredSize());
// System.out.println(dimension);
// this.resize(100,500);
//creates time
timeCount = new JLabel(Integer.toString(time));
timeCounter = new javax.swing.Timer(1000, new TimeCountListener());
//creates score
scoreCount = new JLabel(Integer.toString(score));
//creates the underlying board
JPanel gameBoard = new JPanel();
gameBoard.setLayout(new GridLayout(columns, rows));
PanelListener panListener = new PanelListener();
gameBoard.addComponentListener(panListener);
//create a cell for every row and column and adds them to gameboard.
for ( int c = 0; c < this.columns ; c++){
for ( int r = 0; r < this.rows; r++ ){
//Creates a new minecell object for every row and column
MineCell mineCells = new MineCell(c, r);
gameBoard.add(mineCells);
}
}
//creates the reset button
try{
resetFace = new JButton();
Image img = ImageIO.read(getClass().getResource("Happy_Face.png"));
resetFace.setIcon(new ImageIcon(img));
Dimension d = new Dimension(getPreferredSize());
int height = img.getHeight(null);
int width = img.getWidth(null);
resetFace.setPreferredSize(new Dimension(width, height));
resetFace.setBorder(BorderFactory.createEmptyBorder());
ClearListener resetListener = new ClearListener();
resetFace.addMouseListener(resetListener);
}
catch(IOException ex){}
//Size, organize, and show this board
setLayout( new BorderLayout());
//header
JPanel header = new JPanel();
header.setLayout(new FlowLayout());
header.add(timeCount);
header.add(resetFace, BorderLayout.NORTH);
header.add(scoreCount);
add(header, BorderLayout.NORTH);
add(gameBoard, BorderLayout.CENTER);
revalidate();
setVisible(true);
}
//----------------------------------------------------------------
//---------------------------Music--------------------------------
//----------------------------------------------------------------
URL urlForAudio = getClass().getResource("bloop.wav");
AudioClip audioClip = Applet.newAudioClip(urlForAudio);
//----------------------------------------------------------------
//--------------------MineCell Inner Class------------------------
//----------------------------------------------------------------
/**
* This inner class holds everything about the cells in this game.
*/
public class MineCell extends JPanel
{
private int row;
private int column;
MineCell(int c, int r)
{
this.row = r;
this.column = c;
this.addMouseListener(new ClickListener());
}
//------------------------------------------------
/**
* sets the preferredsize of the board
*/
public Dimension getPreferredSize()
{
return new Dimension( 20, 20 );
}
//----------------------------------------------------------------
//-----------------------PaintComponent---------------------------
//----------------------------------------------------------------
/**
* Paint Componenent (paints everything)
*/
public void paint( Graphics g )
{
super.paintComponent( g );
setBackground(new Color(255,255,255));
int x = this.getWidth(); //width of each cell.
int y = this.getHeight(); //height of each cell.
g.setColor(new Color(150,150, 150));
g.drawRect(0,0, x,y);
int fontsize = (3*(this.getWidth() + this.getHeight())/10);
//custom font.
try{
Font font = Font.createFont(Font.PLAIN, new File("DS-DIGII.ttf"));
scoreCount.setFont(font.deriveFont(Font.BOLD, 40f));
timeCount.setFont(font.deriveFont(Font.BOLD, 40f));
setFont(font.deriveFont(Font.BOLD, fontsize));
}
catch(FontFormatException event)
{
scoreCount.setFont(new Font("Serif" , Font.BOLD, fontsize));
timeCount.setFont(new Font("Serif" , Font.BOLD, fontsize));
g.setFont(new Font("Serif" , Font.BOLD, fontsize * 2));
}
catch(IOException event)
{
scoreCount.setFont(new Font("Serif" , Font.BOLD, fontsize));
timeCount.setFont(new Font("Serif" , Font.BOLD, fontsize));
g.setFont(new Font("Serif" , Font.BOLD, fontsize));
}
//draws each cell.
//if flag but no mine.
if(mineSweeper.getStatus() !=0
&& mineSweeper.statusBoard[column][row] == 2
&& mineSweeper.flagBoard[column][row] == 1
&& mineSweeper.mineBoard[column][row] != 1 ){
g.setColor(Color.red);
g.drawString("X", ((x/2) - fontsize/4),y - fontsize/2);
}
//if flag
else if(mineSweeper.flagBoard[column][row] == 1)
{
ImageIcon img = new ImageIcon("Flag.png");
img = new ImageIcon(img.getImage().getScaledInstance((7*this.getWidth())/8,(7*this.getHeight())/8,0));
img.paintIcon(this, g, x/9, y/11);
}
//if unrevealed
else if(mineSweeper.statusBoard[column][row] == 0)
{
g.setColor(new Color(0,0,0));
g.fillRect(1,1,x-1,y-1);
}
//if revealed draw numbers.
else if(mineSweeper.statusBoard[column][row] == 2)
{
switch(mineSweeper.numMinesAround(column,row)){
case 0: break;
case 1:
g.setColor(Color.blue);
g.drawString("1", ((x/2) - fontsize/4) ,y - fontsize/2);
break;
case 2:
g.setColor(Color.green);
g.drawString("2", ((x/2) - fontsize/4),y - fontsize/2);
break;
case 3:
g.setColor(Color.red);
g.drawString("3", ((x/2) - fontsize/4),y - fontsize/2);
break;
case 4:
g.setColor(new Color(0,0,150));
g.drawString("4", ((x/2) - fontsize/4),y - fontsize/2);
break;
case 5:
g.setColor(new Color(150,0,0));
g.drawString("5", ((x/2) - fontsize/4),y - fontsize/2);
break;
case 6:
g.setColor(new Color(0,150,150));
g.drawString("6", ((x/2) - fontsize/4),y - fontsize/2);
break;
case 7:
g.setColor(Color.black);
g.drawString("7", ((x/2) - fontsize/4),y - fontsize/2);
break;
case 8:
g.setColor(Color.gray);
g.drawString("8", ((x/2) - fontsize/4),y - fontsize/2);
break;
}
}
//if the cell is a mine.
else if(mineSweeper.statusBoard[column][row] == 3)
{
if(mineSweeper.mineBoard[column][row] == 1)
{
ImageIcon img = new ImageIcon("Mine.png");
img = new ImageIcon(img.getImage().getScaledInstance((7*this.getWidth())/8,(7*this.getHeight())/8,0));
img.paintIcon(this, g, x/12, y/12);
}
}
repaint();
}
//----------------------------------------------------------------
//-------------------------Listener--------------------------------
//----------------------------------------------------------------
/*
* This class handles all minecell clicking.
*/
class ClickListener implements MouseListener
{
public void mouseClicked( MouseEvent e ) {
//if flag
if ( e.getButton() == 3){
if(mineSweeper.getStatus() == 0){
if(mineSweeper.statusBoard[column][row] == 0){
if( mineSweeper.flagBoard[column][row] == 1){
mineSweeper.flagBoard[column][row] = 0;
score++;
scoreCount.setText(Integer.toString(score));
}
else{
mineSweeper.flagBoard[column][row] = 1;
score--;
scoreCount.setText(Integer.toString(score));
}
}
}
}
//if not flag
else{
if(mineSweeper.flagBoard[column][row] == 0){
mineSweeper.click(column,row);
timeCounter.start();
audioClip.play();
try{
if(mineSweeper.getStatus() == 1){
Image img = ImageIO.read(getClass().getResource("Sad_Face.png"));
resetFace.setIcon(new ImageIcon(img));
timeCounter.stop();
}
else if(mineSweeper.getStatus() == 2){
Image img = ImageIO.read(getClass().getResource("Sunglasses_Face.png"));
resetFace.setIcon(new ImageIcon(img));
timeCounter.stop();
}
}
catch(IOException ex){}
}
}
}
public void mousePressed( MouseEvent e ) {}
public void mouseReleased( MouseEvent e ) {
audioClip.stop();
}
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
}
}
//------------------------------------------------------------
/*
* This class handles the clear button.
*/
public class ClearListener implements MouseListener{
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mousePressed (MouseEvent e) {
//change face to happy
}
public void mouseReleased (MouseEvent e) {
mineSweeper.clearReset();
mineSweeper.setStatus(0);
time = 0;
score = mineSweeper.getBombs();
scoreCount.setText(Integer.toString(score));
timeCount.setText(Integer.toString(time));
timeCounter.stop();
try{
Image img = ImageIO.read(getClass().getResource("Happy_Face.png"));
resetFace.setIcon(new ImageIcon(img));
}
catch(IOException ex){}
}
public void mouseClicked( MouseEvent e ) {}
}
public class PanelListener implements ComponentListener{
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {
Dimension d = getPreferredSize();
setMaximumSize(d);
setMinimumSize(d);
}
public void componentShown(ComponentEvent e) {}
}
public class TimeCountListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
time++;
timeCount.setText(Integer.toString(time));
}
}
}