-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGVdie.java
309 lines (265 loc) · 9.03 KB
/
GVdie.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
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/*****************************************************************
A graphical representation of a six-sided die with various controls
over the appearance. Current value is constrained between 1 and 6.
You can control the size and color of the dice.
<h4>An Example</h4>
<blockquote><pre><code>
GVdice die1 = new GVdice();
die1.roll( );
int result = die1.getValue();
</code></pre></blockquote>
<p>
@author Scott Grissom
@version 1.4 October 10, 2006
*****************************************************************/
public class GVdie extends JPanel implements MouseListener, Comparable{
/** current value of the die */
private int myValue, displayValue;
/** is the dice currently selected or scored? */
private boolean selected, scored;
/** current size in pixels */
private int mySize;
/** dot size in pixels defined by overall die size */
private int dotSize;
/** offset in pixels for the left row of dots */
private int left;
/** offset in pixels for the right row of dots */
private int right;
/** offset in pixels for the middle dot */
private int middle;
/** color of the dice when selected */
private Color SELECTED_COLOR = Color.pink;
/** color of the dice when scored */
private Color SCORED_COLOR = Color.gray;
/** default color of dice */
private Color BACKGROUND = Color.white;
/** repeats for animation */
private int NUM_ROLLS;
/** Timer for animation */
private javax.swing.Timer myTimer;
/*****************************************************************
constructor creates a die of specified size X size pixels
@param size the length of each side in pixels
*****************************************************************/
public GVdie(int size) {
// initialize the die and determine display characteristics
mySize = size;
selected = false;
dotSize = mySize / 5;
int spacer = (mySize - (3*dotSize))/4;
left = spacer;
right = mySize - spacer - dotSize;
middle = (mySize - dotSize) /2;
setBackground(BACKGROUND);
setForeground(Color.black);
setSize(size,size);
setPreferredSize(new Dimension(size, size));
setMinimumSize(new Dimension(size, size));
setMaximumSize(new Dimension(size, size));
// create the fancy border
Border raised = BorderFactory.createRaisedBevelBorder();
Border lowered = BorderFactory.createLoweredBevelBorder();
Border compound = BorderFactory.createCompoundBorder(raised, lowered);
setBorder(compound);
// set default values
displayValue = myValue = (int) (Math.random()*6)+1;
setNumRolls(6);
myTimer = new javax.swing.Timer(250, new Animator());
addMouseListener(this);
}
/*****************************************************************
* default constructor creates a die of size 100 X 100 pixels
*****************************************************************/
public GVdie() {
this(100);
}
/*****************************************************************
Is the dice currently selected?
@return true if the die is selected. Otherise, false.
*****************************************************************/
public boolean isSelected(){
return selected;
}
/*****************************************************************
Has the die been scored already?
@return true if the die has been scored. Otherise, false.
*****************************************************************/
public boolean isScored(){
return scored;
}
/*****************************************************************
Set the die face to blank
*****************************************************************/
public void setBlank(){
displayValue = 0;
myValue = 0;
scored = false;
selected = false;
repaint();
}
/*****************************************************************
Set whether the die is selected or not
@param h true if die is currently selected
*****************************************************************/
public void setScored(boolean h){
scored = h;
if(scored){
selected = false;
setBackground(SCORED_COLOR);
}else{
setBackground(BACKGROUND);
}
repaint();
}
/*****************************************************************
Set whether the die is selected or not
@param h true if die is currently selected
*****************************************************************/
public void setSelected(boolean h){
selected = h;
if(selected){
scored = false;
setBackground(SELECTED_COLOR);
}else{
setBackground(BACKGROUND);
}
repaint();
}
/*****************************************************************
Sets the color of the dots
@param c a Java Color object such as Color.red
*****************************************************************/
public void setForeground(Color c){
super.setForeground(c);
}
/*****************************************************************
Updates the image after obtaining a random value in the range 1 - 6.
@param none
@return the new value between 1 - 6
*****************************************************************/
public void roll (){
myValue = (int) (Math.random()*6)+1;
// start the animated roll
myTimer.restart();
}
/*****************************************************************
Set the delay in milliseconds between frames of the animation.
Default value is 250.
@param msec milliseconds to delay
*****************************************************************/
public void setDelay (int msec){
if (msec > 0)
myTimer = new javax.swing.Timer(msec, new Animator());
}
/*****************************************************************
Set the number of rolls before stopping the animation.
Default value is 6.
@param num number of rolls before stopping
*****************************************************************/
public void setNumRolls (int num){
NUM_ROLLS = 0;
if (num > 0)
NUM_ROLLS = num;
}
/*****************************************************************
gets the current value of the die (1 - 6)
@return the current value of the die
*****************************************************************/
public int getValue(){
return myValue;
}
/*****************************************************************
Display the current value of the die. Called automatically
after rolling. There is no need to call this method directly.
@param g the graphics context for the panel
@return none
*****************************************************************/
public void paintComponent(Graphics g){
super.paintComponent(g);
// paint dots
switch (displayValue){
case 1:
g.fillOval (middle,middle,dotSize,dotSize);
break;
case 2:
g.fillOval (left,left,dotSize,dotSize);
g.fillOval (right,right,dotSize,dotSize);
break;
case 3:
g.fillOval (middle,left,dotSize,dotSize);
g.fillOval (middle,middle,dotSize,dotSize);
g.fillOval (middle,right,dotSize,dotSize);
break;
case 5: g.fillOval (middle,middle,dotSize,dotSize);
// fall throught and paint four more dots
case 4:
g.fillOval (left,left,dotSize,dotSize);
g.fillOval (left,right,dotSize,dotSize);
g.fillOval (right,left,dotSize,dotSize);
g.fillOval (right,right,dotSize,dotSize);
break;
case 6:
g.fillOval (left,left,dotSize,dotSize);
g.fillOval (left,middle,dotSize,dotSize);
g.fillOval (left,right,dotSize,dotSize);
g.fillOval (right,left,dotSize,dotSize);
g.fillOval (right,middle,dotSize,dotSize);
g.fillOval (right,right,dotSize,dotSize);
break;
}
}
/*****************************************************************
respond to the dice being clicked
@param e the mouse event
*****************************************************************/
public void mouseClicked(MouseEvent e){
// do not allow scored die to be selected
if(scored){
return;
}
if(selected){
selected = false;
setBackground(BACKGROUND);
}else{
selected = true;
setBackground(SELECTED_COLOR);
}
repaint();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
/*****************************************************************
allows dice to be compared if necessary
@param o compare the dice with this object
@return -1 if dice is less than passed object
*****************************************************************/
public int compareTo(Object o){
GVdie d = (GVdie) o;
return getValue() - d.getValue();
}
/******************************************************
INNER class to roll the dice as an animation
******************************************************/
private class Animator implements ActionListener{
int count = 0;
public void actionPerformed(ActionEvent e){
displayValue = (int) (Math.random()*6)+1;
repaint();
count++;
// Should we stop rolling?
if (count == NUM_ROLLS){
count=0;
myTimer.stop();
displayValue = myValue;
repaint();
}
}
}
}