forked from ShamariFeaster/MySolitaire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
291 lines (259 loc) · 5.24 KB
/
Card.java
File metadata and controls
291 lines (259 loc) · 5.24 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
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JPanel;
class Card extends JPanel
{
public static enum Value
{
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
}
public static enum Suit
{
SPADES, CLUBS, DIAMONDS, HEARTS
}
private Suit _suit;
private Value _value;
private Boolean _faceup;
private Point _location; // location relative to container
private Point whereAmI; // used to create abs postion rectangle for contains
// functions
private int x; // used for relative positioning within CardStack Container
private int y;
private final int x_offset = 10;
private final int y_offset = 20;
private final int new_x_offset = x_offset + (CARD_WIDTH - 30);
final static public int CARD_HEIGHT = 150;
final static public int CARD_WIDTH = 100;
final static public int CORNER_ANGLE = 25;
Card(Suit suit, Value value)
{
_suit = suit;
_value = value;
_faceup = false;
_location = new Point();
x = 0;
y = 0;
_location.x = x;
_location.y = y;
whereAmI = new Point();
}
Card()
{
_suit = Card.Suit.CLUBS;
_value = Card.Value.ACE;
_faceup = false;
_location = new Point();
x = 0;
y = 0;
_location.x = x;
_location.y = y;
whereAmI = new Point();
}
public Suit getSuit()
{
switch (_suit)
{
case HEARTS:
System.out.println("Hearts");
break;
case DIAMONDS:
System.out.println("Diamonds");
break;
case SPADES:
System.out.println("Spades");
break;
case CLUBS:
System.out.println("Clubs");
break;
}
return _suit;
}
public Value getValue()
{
switch (_value)
{
case ACE:
System.out.println(" Ace");
break;
case TWO:
System.out.println(" 2");
break;
case THREE:
System.out.println(" 3");
break;
case FOUR:
System.out.println(" 4");
break;
case FIVE:
System.out.println(" 5");
break;
case SIX:
System.out.println(" 6");
break;
case SEVEN:
System.out.println(" 7");
break;
case EIGHT:
System.out.println(" 8");
break;
case NINE:
System.out.println(" 9");
break;
case TEN:
System.out.println(" 10");
break;
case JACK:
System.out.println(" Jack");
break;
case QUEEN:
System.out.println(" Queen");
break;
case KING:
System.out.println(" King");
break;
}
return _value;
}
public void setWhereAmI(Point p)
{
whereAmI = p;
}
public Point getWhereAmI()
{
return whereAmI;
}
public Point getXY()
{
return new Point(x, y);
}
public Boolean getFaceStatus()
{
return _faceup;
}
public void setXY(Point p)
{
x = p.x;
y = p.y;
}
public void setSuit(Suit suit)
{
_suit = suit;
}
public void setValue(Value value)
{
_value = value;
}
public Card setFaceup()
{
_faceup = true;
return this;
}
public Card setFacedown()
{
_faceup = false;
return this;
}
@Override
public boolean contains(Point p)
{
Rectangle rect = new Rectangle(whereAmI.x, whereAmI.y, Card.CARD_WIDTH, Card.CARD_HEIGHT);
return (rect.contains(p));
}
private void drawSuit(Graphics2D g, String suit, Color color)
{
g.setColor(color);
g.drawString(suit, _location.x + x_offset, _location.y + y_offset);
g.drawString(suit, _location.x + x_offset, _location.y + CARD_HEIGHT - 5);
}
private void drawValue(Graphics2D g, String value)
{
g.drawString(value, _location.x + new_x_offset, _location.y + y_offset);
g.drawString(value, _location.x + new_x_offset, _location.y + y_offset + CARD_HEIGHT - 25);
}
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
RoundRectangle2D rect2 = new RoundRectangle2D.Double(_location.x, _location.y, CARD_WIDTH, CARD_HEIGHT,
CORNER_ANGLE, CORNER_ANGLE);
g2d.setColor(Color.WHITE);
g2d.fill(rect2);
g2d.setColor(Color.black);
g2d.draw(rect2);
// DRAW THE CARD SUIT AND VALUE IF FACEUP
if (_faceup)
{
switch (_suit)
{
case HEARTS:
drawSuit(g2d, "Hearts", Color.RED);
break;
case DIAMONDS:
drawSuit(g2d, "Diamonds", Color.RED);
break;
case SPADES:
drawSuit(g2d, "Spades", Color.BLACK);
break;
case CLUBS:
drawSuit(g2d, "Clubs", Color.BLACK);
break;
}
int new_x_offset = x_offset + (CARD_WIDTH - 30);
switch (_value)
{
case ACE:
drawValue(g2d, "A");
break;
case TWO:
drawValue(g2d, "2");
break;
case THREE:
drawValue(g2d, "3");
break;
case FOUR:
drawValue(g2d, "4");
break;
case FIVE:
drawValue(g2d, "5");
break;
case SIX:
drawValue(g2d, "6");
break;
case SEVEN:
drawValue(g2d, "7");
break;
case EIGHT:
drawValue(g2d, "8");
break;
case NINE:
drawValue(g2d, "9");
break;
case TEN:
drawValue(g2d, "10");
break;
case JACK:
drawValue(g2d, "J");
break;
case QUEEN:
drawValue(g2d, "Q");
break;
case KING:
drawValue(g2d, "K");
break;
}
} else
{
// DRAW THE BACK OF THE CARD IF FACEDOWN
RoundRectangle2D rect = new RoundRectangle2D.Double(_location.x, _location.y, CARD_WIDTH, CARD_HEIGHT,
CORNER_ANGLE, CORNER_ANGLE);
g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(rect);
g2d.setColor(Color.black);
g2d.draw(rect);
}
}
}// END Card