-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.java
445 lines (405 loc) · 15.5 KB
/
Table.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package solitaire;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
import javax.swing.event.*;
public class Table extends JPanel implements MouseMotionListener {
JFrame frame;
Deck deck = new Deck();
Tablaa tablaa = new Tablaa(deck);
Fundament fundament = new Fundament();
MyListener myListener = new MyListener();
Wireframe wireframe = new Wireframe();
Color backgroundColor = Color.GREEN.darker().darker();
final Dimension size = new Dimension(630,530);
String cardBack = "img/b2fv.png";
int cX,cY,offsetX,offsetY,offset,offsetType;
boolean gameOver = false;
ArrayList<Card> remaining = new ArrayList<Card>();
public Table() {
createAndShowGUI();
}
private void createAndShowGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(size);
frame.setPreferredSize(size);
frame.setMaximumSize(size);
frame.setTitle("Solitaire");
JComponent newContentPane = this;
frame.setContentPane(newContentPane);
//... Making menu
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Menu"),submenu = new JMenu("Card color");
JMenuItem newGameItem = new JMenuItem("New Game");
JMenuItem quitItem = new JMenuItem("Quit");
JRadioButtonMenuItem rbbutton1,rbbutton2;
//... Add listeners to menu items
newGameItem.addActionListener(new NewGameAction());
quitItem.addActionListener(new QuitAction());
ButtonGroup group = new ButtonGroup();
rbbutton1 = new JRadioButtonMenuItem("Red back");
rbbutton1.setSelected(true);
group.add(rbbutton1);
rbbutton2 = new JRadioButtonMenuItem("Blue back");
group.add(rbbutton2);
rbbutton1.addActionListener(new CardColorRed());
rbbutton2.addActionListener(new CardColorBlue());
menubar.add(menu);
menu.add(newGameItem);
menu.addSeparator();
menu.add(submenu);
submenu.add(rbbutton1);
submenu.add(rbbutton2);
menu.addSeparator();
menu.add(quitItem);
frame.setJMenuBar(menubar);
//... End make menu
addMouseListener(myListener);
addMouseMotionListener(this);
//Display the window.
frame.pack();
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
remaining.clear();
g.setColor(backgroundColor);
g.fillRect(0, 0, screensize().width, screensize().height);
if (deck.size() > 0) { g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(cardBack)), 20, 20, this); }
else {
g.setColor(Color.BLACK);
g.drawRect(20,20,72,96);
}
if (deck.pileSize() > 0) {
if (deck.showTopCard().isSelected()) {
remaining.add(deck.showTopCard());
offsetType = 1;
if (deck.pileSize() > 1) { g.drawImage(deck.show2ndCard().getCardImage(cardBack), 105, 20, this); }
}
else {
g.drawImage(deck.showTopCard().getCardImage(cardBack), 105, 20, this);
}
}
int col = 0, coltemp = 0;
for (Stack stack : fundament.getFundament()) {
if (stack.size() != 0) {
if (stack.getTopCard().isSelected()) {
remaining.add(stack.getTopCard());
offsetType = 2;
coltemp = col;
if (stack.size() > 1) { g.drawImage(stack.get2ndCard().getCardImage(cardBack), 275+(col*85), 20, this); }
else { g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("img/53.png")), 275+(col*85), 20, this); }
}
else {
g.drawImage(stack.getTopCard().getCardImage(cardBack), 275+(col*85), 20, this);
}
}
else {
g.drawImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("img/53.png")), 275+(col*85), 20, this);
}
col++;
}
int column = 0,row,columntemp=0,rowtemp=0;
for (Stack stack : tablaa.getTablaa()) {
row = 0;
if (stack.size() == 0) {
g.setColor(Color.BLACK);
g.drawRect(20+(column*85), 130+(row*20), 72, 96);
}
for (Card card : stack.getStack()) {
if (card.isSelected()) {
rowtemp = row;
columntemp = column;
offset = 1;
offsetType = 3;
}
if (offset == 1) {
remaining.add(card);
if (rowtemp == 0) {
g.setColor(Color.BLACK);
g.drawRect(20+(column*85), 130+(row*20), 72, 96);
}
continue;
}
g.drawImage(card.getCardImage(cardBack), 20+(column*85), 130+(row*20), this);
row++;
}
offset = 0;
column++;
}
for (Card card : remaining) {
if (offsetType == 1) { g.drawImage(deck.showTopCard().getCardImage(cardBack), 105+offsetX, 20+offsetY, this); }
if (offsetType == 2) { g.drawImage(card.getCardImage(cardBack), 275+(coltemp*85)+offsetX, 20+offsetY, this); }
if (offsetType == 3) { g.drawImage(card.getCardImage(cardBack), 20+(columntemp*85)+(offsetX), 130+(rowtemp*20)+(offsetY), this); }
rowtemp++;
}
if (gameOver) {
Random r = new Random();
int x,y,vx,vy,i=0;
for (Stack s : fundament.getFundament()) {
for (Card c : s.getStack()) {
x = 275+(i*85);
y = 20;
vx = r.nextInt(12)-6;
vy = r.nextInt(10);
while (x > 0 && x < screensize().width) {
x = x = vx;
g.drawImage(c.getCardImage(cardBack), x, y, this);
}
}
i++;
}
}
}
private Dimension screensize() {
return java.awt.Toolkit.getDefaultToolkit().getScreenSize();
}
class CardColorRed implements ActionListener {
public void actionPerformed(ActionEvent e) {
cardBack = "img/b2fv.png";
repaint();
}
}
class CardColorBlue implements ActionListener {
public void actionPerformed(ActionEvent e) {
cardBack = "img/b1fv.png";
repaint();
}
}
class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
newGame();
}
}
class QuitAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
int i = JOptionPane.showConfirmDialog(frame, "Are you sure you want to quit?", "confirm quit", JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
System.exit(0); // terminate this program
}
}
}
public void mouseDragged(MouseEvent e) {
offsetX = e.getX() - cX;
offsetY = e.getY() - cY;
repaint();
}
public void mouseMoved(MouseEvent arg0) { }
private class MyListener extends MouseInputAdapter {
Card selectedCard;
int selectedStack,selectedNumber,selectedCardOrigin;
@Override
public void mousePressed(MouseEvent e) {
int stack = stackHit(e);
if (stack < 14) {
cX = e.getX();
cY = e.getY();
if (stack == 1) {
if (deck.size() > 0) {
deck.toPile();
}
else {
deck.flip();
}
}
if (stack == 2) {
selectedCard = deck.showTopCard();
selectedCard.setSelected(true);
selectedCardOrigin = 1;
}
if (stack < 7 && stack > 2) {
if (fundament.size(stack) > 0) {
selectedCard = fundament.showTopCard(stack);
selectedCard.setSelected(true);
selectedStack = stack;
selectedCardOrigin = 2;
}
}
if (stack < 14 && stack > 6) {
selectedCard = findClickedCard(e.getY(),stack);
selectedStack = stack;
selectedCardOrigin = 3;
if (selectedCard != null) {
if (selectedCard.isFaceUp()) { selectedCard.setSelected(true); }
else {
if (selectedNumber == tablaa.getStack(stack-6).size() -1) {
selectedCard.setFaceUp();
}
}
}
}
}
repaint();
if (fundament.checkGameOver()) {
gameOver();
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (selectedCard != null) {
selectedCard.setSelected(false);
offsetX = 0;
offsetY = 0;
//System.out.println("Mouse released at: " + e.getX() + "," + e.getY());
int stack = stackHit(e);
if (stack > 2 && stack < 7) {
if (remaining.size() > 1) { }
else {
if (fundament.size(stack) == 0) {
if (selectedCard.getFace() == 1) {
fundament.putCard(stack, selectedCard);
removeCard(selectedCard);
}
}
else {
if (fundament.showTopCard(stack).collide(selectedCard,stack)) {
fundament.putCard(stack, selectedCard);
removeCard(selectedCard);
}
}
}
}
if (stack < 14 && stack > 6) {
if (tablaa.getStack(stack-6).size() == 0) {
if (selectedCard.getFace() == 13) {
for (Card card : remaining) {
tablaa.putCard(stack-6, card);
removeCard(card);
}
}
}
else {
Card destinationCard = findClickedCard(e.getY(),stack);
if (destinationCard != null) {
if (destinationCard.collide(selectedCard,stack)) {
for (Card card : remaining) {
tablaa.putCard(stack-6, card);
removeCard(card);
}
}
}
}
}
repaint();
if (fundament.checkGameOver()) {
gameOver();
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2){
int stack = stackHit(e);
Card clickedCard;
if (stack == 2 || (stack < 14 && stack > 6)) {
if (stack == 2) {
clickedCard = deck.showTopCard();
selectedCardOrigin = 1;
}
else {
clickedCard = findClickedCard(e.getY(),stack);
selectedCardOrigin = 3;
}
if (clickedCard != null) {
int i = 2;
for (Stack s : fundament.getFundament()) {
i++;
Card topCard = s.getTopCard();
if (topCard == null) {
if (clickedCard.getFace() == 1) {
s.add(clickedCard);
removeCard(clickedCard);
break;
}
}
else {
if (topCard.collide(clickedCard,i)) {
s.add(clickedCard);
removeCard(clickedCard);
break;
}
}
}
}
}
}
if (fundament.checkGameOver()) {
gameOver();
}
}
private void removeCard(Card card) {
switch (selectedCardOrigin) {
case 1: {
deck.removeCard(card);
break;
}
case 2: {
fundament.removeCard(selectedStack-3, card);
break;
}
case 3: {
tablaa.removeCard(selectedStack-6, card);
break;
}
}
}
private int stackHit(MouseEvent e) {
int ret = 1;
for (Shape s : wireframe.getWireFrame()) {
if (s.contains(e.getPoint())) {
return ret;
}
ret++;
}
return ret;
}
private Card findClickedCard(int y, int stack) {
stack = stack - 6;
int i = 0;
y = y - 130;
for (Card c : tablaa.getStack(stack)) {
if ((y > (i * 20)) && (y < ((i+1)*20))) {
selectedNumber = i;
return c;
}
if (i+1 == tablaa.getStack(stack).size()) {
if ((y > (i * 20)) && (y < ((i*20)+96))) {
selectedNumber = i;
return c;
}
}
i++;
}
return null;
}
}
public void gameOver() {
gameOver = true;
repaint();
int i = JOptionPane.showConfirmDialog(frame, "You have completed Håvards Epic Solitaire\nPlay Again?", "Game Over", JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
newGame(); // starts a new game
}
if (i == JOptionPane.NO_OPTION) {
System.exit(0); // terminate this program
}
}
public void newGame() {
deck = new Deck();
tablaa = new Tablaa(deck);
fundament = new Fundament();
gameOver = false;
repaint();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Table();
}
});
}
}