-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlowerBedCardStack.java
More file actions
240 lines (214 loc) · 4.87 KB
/
FlowerBedCardStack.java
File metadata and controls
240 lines (214 loc) · 4.87 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
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;
import javax.swing.JComponent;
/* This is GUI component with a embedded
* data structure. This structure is a mixture
* of a queue and a stack
*/
class FlowerBedCardStack extends JComponent
{
protected final int NUM_CARDS = 52;
protected Vector<Card> v;
protected boolean playStack = false;
protected int SPREAD = 18;
protected int _x = 0;
protected int _y = 0;
public FlowerBedCardStack(boolean isDeck)
{
this.setLayout(null);
v = new Vector<Card>();
if (isDeck)
{
// set deck position
for (Card.Suit suit : Card.Suit.values())
{
for (Card.Value value : Card.Value.values())
{
v.add(new Card(suit, value));
}
}
} else
{
playStack = true;
}
}
public boolean empty()
{
if (v.isEmpty())
return true;
else
return false;
}
public void putFirst(Card c)
{
v.add(0, c);
}
public Card getFirst()
{
if (!this.empty())
{
return v.get(0);
} else
return null;
}
// analogous to peek()
public Card getLast()
{
if (!this.empty())
{
return v.lastElement();
} else
return null;
}
// queue-like functionality
public Card popFirst()
{
if (!this.empty())
{
Card c = this.getFirst();
v.remove(0);
return c;
} else
return null;
}
public void push(Card c)
{
v.add(c);
}
public Card pop()
{
if (!this.empty())
{
Card c = v.lastElement();
v.remove(v.size() - 1);
return c;
} else
return null;
}
// shuffle the cards
public void shuffle()
{
Vector<Card> v = new Vector<Card>();
while (!this.empty())
{
v.add(this.pop());
}
while (!v.isEmpty())
{
Card c = v.elementAt((int) (Math.random() * v.size()));
this.push(c);
v.removeElement(c);
}
}
public int showSize()
{
return v.size();
}
public Vector<Card> getStack()
{
return v;
}
public void removeCard(Card c) {
v.removeElement(c);
}
// reverse the order of the stack
public FlowerBedCardStack reverse()
{
Vector<Card> v = new Vector<Card>();
while (!this.empty())
{
v.add(this.pop());
}
while (!v.isEmpty())
{
Card c = v.firstElement();
this.push(c);
v.removeElement(c);
}
return this;
}
public void makeEmpty()
{
while (!this.empty())
{
this.popFirst();
}
}
@Override
public boolean contains(Point p)
{
if(playStack) {
Rectangle rect = new Rectangle(_x, _y, Card.CARD_WIDTH + 10, Card.CARD_HEIGHT * 2);
return (rect.contains(p));
} else {
Rectangle rect = new Rectangle(_x, _y, Card.CARD_WIDTH * 16, Card.CARD_HEIGHT);
return (rect.contains(p));
}
}
public void setXY(int x, int y)
{
_x = x;
_y = y;
// System.out.println("CardStack SET _x: " + _x + " _y: " + _y);
setBounds(_x, _y, Card.CARD_WIDTH + 1000, Card.CARD_HEIGHT * 3);
}
public Point getXY()
{
// System.out.println("CardStack GET _x: " + _x + " _y: " + _y);
return new Point(_x, _y);
}
// moves a card to abs location within a component
protected Card moveCard(Card c, int x, int y)
{
c.setBounds(new Rectangle(new Point(x, y), new Dimension(Card.CARD_WIDTH + 10, Card.CARD_HEIGHT + 10)));
c.setXY(new Point(x, y));
return c;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (playStack)
{
removeAll();
Point prev = new Point(); // positioning relative to the container
for (int x = 0; x < v.size(); x++)
{
Card c = v.get(x);
prev = new Point(0, (SPREAD * (v.size() - x)));
add(moveCard(c, prev.x, prev.y ));
Point p = getXY();
c.setWhereAmI(new Point(p.x, p.y+ (SPREAD * (v.size() - x))));
}
} else {
removeAll();
//ListIterator<Card> iter = v.listIterator();
Point prev = new Point(); // positioning relative to the container
Point prevWhereAmI = new Point();// abs positioning on the board
for (int x = 0; x < v.size(); x++)
{
Card c = v.get(x);
// this origin is point(0,0) inside the cardstack container
prev = new Point(+ ((SPREAD * 3) * v.size()), 0);// c.getXY(); // starting deck pos
add(moveCard(c, prev.x, prev.y ));
// setting x & y position
Point p = getXY();
c.setWhereAmI(new Point(p.x+ ((SPREAD * 3) * v.size()), p.y));
prevWhereAmI = c.getWhereAmI();
}
for (int x = 0; x < v.size(); x++)
{
Card c = v.get(x);
c.setXY(new Point(prev.x- (SPREAD * 3), prev.y ));
add(moveCard(c, prev.x- (SPREAD * 3), prev.y));
prev = c.getXY();
// setting x & y position
c.setWhereAmI(new Point(prevWhereAmI.x- (SPREAD * 3), prevWhereAmI.y));
prevWhereAmI = c.getWhereAmI();
}
}
}
}// END CardStack