forked from SeanWalsh95/Ticket-To-Ride
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardSelectPanel.java
188 lines (171 loc) · 6.12 KB
/
CardSelectPanel.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
import javax.swing.*;
import java.util.*;
import java.awt.*;
/**
* Extnsion of JPanel to be used to have a player select from a group of given
* cards
*
* @author Sean Walsh
* @version 1.0
*/
public class CardSelectPanel extends JPanel {
// backround image to be drawn behind everything
Image background;
// lists for cards passed in for selection
ArrayList<Card> cards;
// int's representing various static vlaues
int cardWidth, cardHeight, rows, border = 50, topBorder = 100,
leftBorder = 50;
// GButton's to be pressed by the user
protected GButton backButt;
// String to be printed at the top of the Panel
String title;
// array of size cards that holds if a card is or is not selected
boolean[] selectedCards;
/**
* Contsructor for CardSelectPanel
*
* @param titleIn the text to be displayed at the top of the panel
* @param avalableCards the list of cards to print to the screen
*/
public CardSelectPanel(String titleIn, ArrayList<Card> avalableCards) {
this.setLayout(null);
//makes the mouse cursor
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor a = toolkit.createCustomCursor(ImgLib.mouseCursor,
new Point(this.getX(),this.getY()), "img");
setCursor(a);
this.background = ImgLib.woodBackground;
// sets the title of the Panel
title = titleIn;
// init the boolean array to size of the passed list of cards
selectedCards = new boolean[avalableCards.size() + 1];
// sets the local list to refrence the passed list
cards = new ArrayList<Card>(avalableCards);
// sets the width and height based on the card type
if (avalableCards.size() > 0) {
if (avalableCards.get(0) instanceof Dest) {
cardWidth = 160;
cardHeight = 247;
rows = 6;
} else {
cardWidth = 247;
cardHeight = 160;
rows = 4;
}
}
// self refrence to this JPanel for button ActionListener's
JPanel self = this;
// adds a back GButton that closes the JDialog Class containing this
// JPanel
backButt = new GButton(new int[]{5, 828, 98, 48},
ImgLib.backButtonUnselected, ImgLib.backButtonHighlighted,
ImgLib.backButtonPressed);
backButt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
SoundLib.button.play();
cards = new ArrayList<Card>();
for(int i = 0; i < selectedCards.length; i++)
selectedCards[i] = false;
((JDialog)
SwingUtilities.windowForComponent(self)).dispose();
}
});
this.add(backButt);
}
/**
* PaintComponent for this JPanel component
*
* @param g the Graphics object for this Class
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.setColor(Color.WHITE);
// sets the curent font to a larger size
Font newFont = new Font("Wide Latin", Font.PLAIN, 30);
g.setFont(newFont);
// centers the title text
int titleBuffer = (this.getWidth()
- (g.getFontMetrics().stringWidth(title))) / 2;
// paints the title text
g.drawString(title, titleBuffer, 50);
// paints the list of cards to the screen
for (int i = 0; i < cards.size(); i++) {
int x = ((i % rows) * 25) + (i % rows) * cardWidth + leftBorder;
int y = ((i / rows) * 25) + (i / rows) * cardHeight + topBorder;
g.drawImage(cards.get(i).getImage(), x, y, cardWidth, cardHeight,
this);
if (selectedCards[i]) {
g.setColor(new Color(195, 224, 235, 165));
g.fillRect(x, y, cardWidth, cardHeight);
}
}
}
/**
* method to return the index of a selected card
*
* @param point the point where the user clicked
* @return return the index of the clicked card or -1 if a card was not
* clicked
*/
public int getCardIndex(Point point) {
for (int i = 0; i < cards.size(); i++) {
int x = ((i % rows) * 25) + (i % rows) * cardWidth + leftBorder;
int y = ((i / rows) * 25) + (i / rows) * cardHeight + topBorder;
Rectangle imageBounds = new Rectangle(x, y, cardWidth, cardHeight);
if (imageBounds.contains(point))
return i;
}
return -1;
}
/**
* method to toggle the selection of a card
*
* @param idx the index of the card to be selected
*/
public void select(int idx) {
if (idx < cards.size() && !selectedCards[idx]) {
selectedCards[idx] = true;
} else {
selectedCards[idx] = false;
}
repaint();
}
/**
* method to get the number of cards currently selected
*
* @return the number of cards currently selected
*/
public int getNumberSelected() {
int count = 0;
for (boolean selected : selectedCards)
if (selected)
count++;
return count;
}
/**
* method to get the number of cards currently selected
*
* @return an array of cards currently selected
*/
public ArrayList<Card> getSelected() {
ArrayList<Card> selected = new ArrayList<Card>();
for (int i = 0; i < cards.size(); i++)
if (selectedCards[i])
selected.add(cards.get(i));
return selected;
}
/**
* method to get the number of cards currently unselected
*
* @return an array of cards currently unselected
*/
public ArrayList<Card> getRemainder() {
ArrayList<Card> remainder = new ArrayList<Card>();
for (int i = 0; i < cards.size(); i++)
if (selectedCards[i])
remainder.add(cards.get(i));
return remainder;
}
}