-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolitaireDisplay.java
More file actions
executable file
·191 lines (165 loc) · 5.07 KB
/
SolitaireDisplay.java
File metadata and controls
executable file
·191 lines (165 loc) · 5.07 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class SolitaireDisplay extends JComponent implements MouseListener
{
private static final int CARD_WIDTH = 73;
private static final int CARD_HEIGHT = 97;
private static final int SPACING = 5; //distance between cards
private static final int FACE_UP_OFFSET = 15; //distance for cascading face-up cards
private static final int FACE_DOWN_OFFSET = 5; //distance for cascading face-down cards
private JFrame frame;
private int selectedRow = -1;
private int selectedCol = -1;
private Solitaire game;
public SolitaireDisplay(Solitaire game)
{
this.game = game;
frame = new JFrame("Solitaire");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
this.setPreferredSize(new Dimension(CARD_WIDTH * 7 + SPACING * 8, CARD_HEIGHT * 2 + SPACING * 3 + FACE_DOWN_OFFSET * 7 + 13 * FACE_UP_OFFSET));
this.addMouseListener(this);
frame.pack();
frame.setVisible(true);
}
public void paintComponent(Graphics g)
{
//background
g.setColor(new Color(0, 128, 0));
g.fillRect(0, 0, getWidth(), getHeight());
//face down
drawCard(g, game.getStockCard(), SPACING, SPACING);
//stock
drawCard(g, game.getWasteCard(), SPACING * 2 + CARD_WIDTH, SPACING);
if (selectedRow == 0 && selectedCol == 1)
drawBorder(g, SPACING * 2 + CARD_WIDTH, SPACING);
//aces
for (int i = 0; i < 4; i++)
drawCard(g, game.getFoundationCard(i), SPACING * (4 + i) + CARD_WIDTH * (3 + i), SPACING);
//piles
for (int i = 0; i < 7; i++)
{
Stack<Card> pile = game.getPile(i);
int offset = 0;
for (int j = 0; j < pile.size(); j++)
{
drawCard(g, pile.get(j), SPACING + (CARD_WIDTH + SPACING) * i, CARD_HEIGHT + 2 * SPACING + offset);
if (selectedRow == 1 && selectedCol == i && j == pile.size() - 1)
drawBorder(g, SPACING + (CARD_WIDTH + SPACING) * i, CARD_HEIGHT + 2 * SPACING + offset);
if (pile.get(j).isFaceUp())
offset += FACE_UP_OFFSET;
else
offset += FACE_DOWN_OFFSET;
}
}
}
private void drawCard(Graphics g, Card card, int x, int y)
{
if (card == null)
{
g.setColor(Color.BLACK);
g.drawRect(x, y, CARD_WIDTH, CARD_HEIGHT);
}
else
{
String fileName = card.getFileName();
if (!new File(fileName).exists())
throw new IllegalArgumentException("bad file name: " + fileName);
Image image = new ImageIcon(fileName).getImage();
g.drawImage(image, x, y, CARD_WIDTH, CARD_HEIGHT, null);
}
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
//none selected previously
int col = e.getX() / (SPACING + CARD_WIDTH);
int row = e.getY() / (SPACING + CARD_HEIGHT);
if (row > 1)
row = 1;
if (col > 6)
col = 6;
if (row == 0 && col == 0)
game.stockClicked();
else if (row == 0 && col == 1)
game.wasteClicked();
else if (row == 0 && col >= 3)
game.foundationClicked(col - 3);
else if (row == 1)
game.pileClicked(col);
repaint();
}
private void drawBorder(Graphics g, int x, int y)
{
g.setColor(Color.YELLOW);
g.drawRect(x, y, CARD_WIDTH, CARD_HEIGHT);
g.drawRect(x + 1, y + 1, CARD_WIDTH - 2, CARD_HEIGHT - 2);
g.drawRect(x + 2, y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4);
}
/**
* Unselects cards.
*/
public void unselect()
{
selectedRow = -1;
selectedCol = -1;
}
/**
* Checks if the waste is selected
* @return true if the waste is selected
*/
public boolean isWasteSelected()
{
return selectedRow == 0 && selectedCol == 1;
}
/**
* Selects the waste pile
*/
public void selectWaste()
{
selectedRow = 0;
selectedCol = 1;
}
/**
* Sees if a pile is selected
* @return true if selectedRow is 1, else false.
*/
public boolean isPileSelected()
{
return selectedRow == 1;
}
/**
* Returns the column of the pile
* @return the selected column, it it's 1, else -1
*/
public int selectedPile()
{
if (selectedRow == 1)
return selectedCol;
else
return -1;
}
/**
* Selects a pile
* @param index the index of the pile you select
*/
public void selectPile(int index)
{
selectedRow = 1;
selectedCol = index;
}
}