Source code: org/zazof/jteg/Card.java
1 /*
2 * Class Card
3 *
4 * This class contains all information about a playingcard
5 *
6 * Classauthors:
7 * Yves Vandewoude (yves@stcham.org)
8 */
9
10 package org.zazof.jteg;
11
12 import java.util.Vector;
13 import java.awt.*;
14 import java.net.URL;
15 import java.net.URLClassLoader;
16
17 public class Card {
18
19
20 public Card(int cardNumber)
21 {
22 $cardNumber = cardNumber;
23 $active = false;
24
25 URLClassLoader urlLoader = (URLClassLoader)getClass().getClassLoader();
26
27 URL fileLoc = urlLoader.findResource("org/zazof/jteg/resources/stock/tar_globo.png");
28 Image cardImage = Toolkit.getDefaultToolkit().createImage(fileLoc);
29 $images.addElement(cardImage);
30
31 fileLoc = urlLoader.findResource("org/zazof/jteg/resources/stock/tar_galeon.png");
32 cardImage = Toolkit.getDefaultToolkit().createImage(fileLoc);
33 $images.addElement(cardImage);
34
35 fileLoc = urlLoader.findResource("org/zazof/jteg/resources/stock/tar_canon.png");
36 cardImage = Toolkit.getDefaultToolkit().createImage(fileLoc);
37 $images.addElement(cardImage);
38
39 fileLoc = urlLoader.findResource("org/zazof/jteg/resources/stock/tar_comodin.png");
40 cardImage = Toolkit.getDefaultToolkit().createImage(fileLoc);
41 $images.addElement(cardImage);
42 }
43
44
45
46 /**********************************
47 / INSPECTORS
48 /**********************************/
49
50
51 /**
52 * Returns the Country associated with this Cardowner of the Country
53 *
54 * @return The Country associated with this card
55 */
56
57 public Country getCountry(){
58 return $country;
59 }
60
61
62 /**
63 * Returns the type of this card
64 *
65 * @return 0 if this card is a balloon, 1 if it is a galleon, 2 for a cannon and 3 for a joker
66 */
67 public int getType(){
68 return $typeID;
69 }
70
71
72 /**
73 * Returns the number of this card
74 *
75 * @return A number between 1 and 5 which represents the number of this card
76 */
77
78 public int getCardNumber(){
79 return $cardNumber;
80 }
81
82
83 /**
84 * Returns the Imageobject that represents this country
85 *
86 * @return The image of the country
87 */
88 public Image getImage(){
89 return (Image) $images.elementAt(getType());
90 }
91
92
93
94 /**
95 * Returns whether this card is selected
96 * @return true if the card is selected, false otherwise
97 *
98 */
99 public boolean isSelected(){
100 return $selected;
101 }
102
103
104 /**
105 * Returns whether this card is active or not
106 *
107 * @return true if the card is active, false otherwise
108 *
109 */
110 public boolean isActive(){
111 return $active;
112 }
113
114
115 /**
116 * Returns whether this card is compatible with another card
117 *
118 * @returns true if both cards have the same type or one of the cards is a joker, false otherwise
119 */
120
121 public boolean isEqualTo(Card other){
122 return (
123 (getType() == 3) ||
124 (other.getType() == 3) ||
125 (getType() == other.getType())
126 );
127 }
128
129 /**
130 * Returns whether this card is different from another card
131 *
132 * @returns true if both cards have the a different type or one of the cards is a joker, false otherwise
133 */
134
135 public boolean isDifferentFrom(Card other){
136 return (
137 (getType() == 3) ||
138 (other.getType() == 3) ||
139 (getType() != other.getType())
140 );
141 }
142
143
144
145 /**
146 * Sets the current position of this card in the cardcontainer
147 *
148 * @param The new position of this card (number between 1 and 5)
149 */
150
151 public int getPosition()
152 {
153 return $currentPosition;
154 }
155
156
157
158 /**********************************
159 / MODIFIERS
160 /**********************************/
161
162 /**
163 * Will activate a card with given values
164 *
165 * @param Country The Country associated with this card
166 * @param type The figureid (0 for a balloon, 1 for a galleon, 2 for a cannon, 3 for a joker)
167 * @param filename The filename of the Bitmap used (png) for the graphical representation of the country
168 *
169 */
170 public void activate (Country country, int type, String filename)
171 {
172 if (DEBUG) System.out.println("Card number " + $cardNumber + " is activated.");
173 $country = country;
174 $typeID = type;
175 // $cardImage = (Image) $images.elementAt($typeID);
176 $active = true;
177 }
178
179
180 /**
181 * Releases a card. After this method, the card is free to be reused.
182 *
183 */
184
185 public void deActivate()
186 {
187 if (DEBUG) System.out.println("Card number " + $cardNumber + " is deactivated.");
188 $active = false;
189 $selected = false;
190 }
191
192 /**
193 *
194 */
195 public void changeSelected(){
196 if ($selected) $selected = false; else $selected = true;
197 }
198
199 /**
200 * selects this card
201 */
202 public void select(){
203 if (DEBUG) System.out.println("Selected card with country " + getCountry().getCountryName());
204 $selected = true;
205 }
206
207 /**
208 * unselects this card
209 */
210 public void unselect(){
211 if (DEBUG) System.out.println("Unselected card with country " + getCountry().getCountryName());
212 $selected = false;
213 }
214
215
216 /**
217 * Sets the current position of this card in the cardcontainer
218 *
219 * @param The new position of this card (number between 1 and 5)
220 */
221
222 public void setPosition(int newPosition)
223 {
224 $currentPosition = newPosition;
225 }
226
227 /*
228 *
229 * Private Variables
230 *
231 *
232 */
233 private int $cardNumber; // The unique number of this card (does never change)
234 private int $currentPosition; // The current location of this card in the container
235 private Country $country;
236 private Vector $images = new Vector(); // All the images that can be associated with this card during its life (depends on type)
237 private int $typeID; // Type of card (balloon, canon, joker or galleon)
238 private boolean $selected; // Is this card currently selected?
239 private boolean $active; // is this card currently free or used?
240 private static final boolean DEBUG = false;
241 }
242