-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDotButton.java
178 lines (143 loc) · 5.16 KB
/
DotButton.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
// Author: Samuel Saint-Fleur, Aden Li
// Course: ITI 1121, University of Ottawa
// Assignment: 2
// Question: DotButton
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
/**
* In the application <b>Minesweeper</b>, a <b>DotButton</b> is a specialized type of
* <b>JButton</b> that represents a square in the game.
* It can have a number of possible icons, which are found in the
* "icons" directory. The icon expresses the state of the dot:
* covered, number of neighbooring mines, exploded..
*
*
*
* The icons have been found on <a href=
* "https://en.wikipedia.org/wiki/Open_content">wikimedia</a>. The author of these
* icons seems to be someone called
* <a href="https://commons.wikimedia.org/wiki/User:Cryosta">Kazukiokumura</a>.
*
* @author Guy-Vincent Jourdan, University of Ottawa
* @author Samuel Saint-Fleur, Aden Li
*/
public class DotButton extends JButton {
/**
* predefined values to capture icons of a DotInfo
*/
public static final int NUMBER_OF_ICONS = 13;
public static final int ZERO_NEIGHBOURS = 0;
public static final int ONE_NEIGHBOURS = 1;
public static final int TWO_NEIGHBOURS = 2;
public static final int THREE_NEIGHBOURS = 3;
public static final int FOUR_NEIGHBOURS = 4;
public static final int FIVE_NEIGHBOURS = 5;
public static final int SIX_NEIGHBOURS = 6;
public static final int SEVEN_NEIGHBOURS = 7;
public static final int EIGHT_NEIGHBOURS = 8;
public static final int MINED = 9;
public static final int CLICKED_MINE = 10;
public static final int COVERED = 11;
public static final int FLAGGED = 12;
/**
* An array is used to cache all the images. Since the images are not
* modified, all the cells that display the same image reuse the same
* <b>ImageIcon</b> object. Notice the use of the keyword <b>static</b>.
*/
private static final ImageIcon[] icons = new ImageIcon[NUMBER_OF_ICONS];
// ADD YOUR INSTANCE VARIABLES HERE
private int column;
private int iconNumber;
private int row;
/**
* Constructor used for initializing a DotButton at a specific
* Board location, with a specific icon.
*
* @param column
* the column of this Cell
* @param row
* the row of this Cell
* @param iconNumber
* specifies which iconNumber to use for this cell
*/
public DotButton(int column, int row, int iconNumber) {
// ADD YOU CODE HERE
this.column = column;
this.row = row;
this.iconNumber = iconNumber;
this.setIcon(getImageIcon());
this.setBorder(null);
}
/**
* Sets the current value of the instance variable iconNumber, and update
* the iconNumber used by the instance, using the method getImageIcon()
* to get the new icon.
* @param iconNumber
* the iconNumber to use, based on the predifined constant values
* defined in this class
*/
public void setIconNumber(int iconNumber) {
// ADD YOU CODE HERE
this.iconNumber = iconNumber;
this.setIcon(getImageIcon());
}
/**
* Getter method for the attribute row.
*
* @return the value of the attribute row
*/
public int getRow() {
// ADD YOU CODE HERE
return row;
}
/**
* Getter method for the attribute column.
*
* @return the value of the attribute column
*/
public int getColumn() {
// ADD YOU CODE HERE
return column;
}
/**
* Returns the <b>ImageIcon</b> reference to use based on
* the current value of the variable iconNumber.
*
* @return the image to be displayed by the button
*/
private ImageIcon getImageIcon() {
if (icons[iconNumber] == null) {
icons[iconNumber] = new ImageIcon("icons/" + getIconFileName());
}
return icons[iconNumber];
}
/**
* This method returns the name of the file containing the image
* corresponding to the current value of the variable iconNumber.
*
* @return the name of the icon file to be used
*/
private String getIconFileName(){
switch(iconNumber) {
case 0 : return "Minesweeper_0.png";
case 1 : return "Minesweeper_1.png";
case 2 : return "Minesweeper_2.png";
case 3 : return "Minesweeper_3.png";
case 4 : return "Minesweeper_4.png";
case 5 : return "Minesweeper_5.png";
case 6 : return "Minesweeper_6.png";
case 7 : return "Minesweeper_7.png";
case 8 : return "Minesweeper_8.png";
case 9 : return "Minesweeper_mine.png";
case 10 : return "Minesweeper_mineSelected.png";
case 11 : return "Minesweeper_unopened_square.png";
case 12 : return "Minesweeper_flag.png";
default: System.out.println("Invalid icon number: " + iconNumber);
return "";
}
}
}