-
Notifications
You must be signed in to change notification settings - Fork 0
/
Die.java
203 lines (187 loc) · 5.62 KB
/
Die.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
/*
* Course: CSC421
* Assignment: #3
* Author: Andrew Seligman
* Date: May 7, 2014
* File: Die.java
*******************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Die.java holds an integer between 1 and 6 to represent the value of a die
**/
public class Die extends JButton
{
private int number;
private int indexInDice;
private ImageIcon[] Faces;
private ImageIcon[] Disabled;
private boolean held = false;
private JPopupMenu popup;
private JMenuItem menuItem;
private SoundLib Click;
private String[] sound;
private boolean cheatAllowed;
private JButton muteButton;
/**
* Constructor initializes the value on the die, loads images that are
* associated with each value, and adds an action listener to this
* object.
*
* @param initVal is the initial value used as the number on the die
*
* @param idx is the index of this object as it exists in the array
* of Die objects in the Dice class. Used for transmitting hold
* information in networked play.
*
* @param canCheat determines whether a player can right-click a Die to
* change its value
**/
public Die(int initVal, int idx, boolean canCheat, JButton mute)
{
super();
try{
number = initVal;
indexInDice = idx;
cheatAllowed = canCheat;
muteButton = mute;
sound = new String[1];
sound[0] = new String("smallClick.wav");
Click = new SoundLib(sound);
Faces = new ImageIcon[6];
Faces[0] = new ImageIcon(getClass().getResource("Die1.png"));
Faces[1] = new ImageIcon(getClass().getResource("Die2.png"));
Faces[2] = new ImageIcon(getClass().getResource("Die3.png"));
Faces[3] = new ImageIcon(getClass().getResource("Die4.png"));
Faces[4] = new ImageIcon(getClass().getResource("Die5.png"));
Faces[5] = new ImageIcon(getClass().getResource("Die6.png"));
Disabled = new ImageIcon[6];
Disabled[0] = new ImageIcon(getClass().getResource("Die1disabled.png"));
Disabled[1] = new ImageIcon(getClass().getResource("Die2disabled.png"));
Disabled[2] = new ImageIcon(getClass().getResource("Die3disabled.png"));
Disabled[3] = new ImageIcon(getClass().getResource("Die4disabled.png"));
Disabled[4] = new ImageIcon(getClass().getResource("Die5disabled.png"));
Disabled[5] = new ImageIcon(getClass().getResource("Die6disabled.png"));
putImage();
createCheatPopup();
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if(SwingUtilities.isLeftMouseButton(e))
{
if(muteButton.isEnabled())
Click.playSound("smallClick.wav");
hold();
}
else if(cheatAllowed)
{
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
);
setPreferredSize(new Dimension(100,100));
setBorder(BorderFactory.createEmptyBorder());
setOpaque(false);
setBackground(new Color(0,0,0,0));
setFocusPainted(false);
setBorderPainted(false);
setContentAreaFilled(false);
}catch (Exception e) { e.printStackTrace();}
}
/**
* @return the index of this object as it exists in the array
* of Die objects in the Dice class.
**/
public int getDieIndex()
{
return indexInDice;
}
/**
* Toggle the held flag for the die
**/
public void hold()
{
if(this.isEnabled())
{
this.setEnabled(false);
held = true;
}
else
{
this.setEnabled(true);
held = false;
}
}
/**
* @return whether the die is held
**/
public boolean isHeld()
{
return held;
}
/**
* Set the held flag on the die to "not held"
**/
public void resetHold()
{
this.setEnabled(true);
held = false;
}
/**
* Place the image corresponding to the die's value on the die
**/
public void putImage()
{
setIcon(Faces[number - 1]);
setDisabledIcon(Disabled[number - 1]);
}
/**
* Set the number on the die
*
* @param newValue is the new number
*
* @param addImage determines whether the new number is displayed
* automatically
**/
public void setNumber(int newValue, boolean addImage)
{
number = newValue;
if(addImage)
putImage();
validate();
}
/**
* Get the number on the die
*
* @return the generated number
**/
public int getNumber()
{
return number;
}
/**
* Create a popup menu for selecting a value to display on the die and
* add actionListeners to set the new value.
**/
private void createCheatPopup()
{
popup = new JPopupMenu();
for(int num = 1; num < 7; num++)
{
final int theNum = num;
menuItem = new JMenuItem(""+num);
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setNumber(theNum, true);
}
}
);
popup.add(menuItem);
}
}
}