1
2
3
4
5 package spellcast.ui;
6
7 import io.*;
8 import java.awt.*;
9 import javax.swing.*;
10 import java.io.*;
11 import spellcast.gestures.*;
12
13 /***
14 * This class is a Flyweight Factory for creating the Icons needed
15 * for the spellcast Gestures.
16 *
17 * @author Barrie Treloar
18 */
19 public class GestureIcon
20 {
21 /***
22 * A flag to indicate that left handed images are desired.
23 */
24 public static int LEFT_HANDED = 1;
25
26 /***
27 * A flag to indicate that right handed images are desired.
28 */
29 public static int RIGHT_HANDED = 2;
30
31 private static Icon antiSpellIcon;
32 private static Icon clapLefthandIcon;
33 private static Icon clapRighthandIcon;
34 private static Icon digitLefthandIcon;
35 private static Icon digitRighthandIcon;
36 private static Icon emptyHandIcon;
37 private static Icon fingersLefthandIcon;
38 private static Icon fingersRighthandIcon;
39 private static Icon hiddenHandIcon;
40 private static Icon knifeLefthandIcon;
41 private static Icon knifeRighthandIcon;
42 private static Icon palmLefthandIcon;
43 private static Icon palmRighthandIcon;
44 private static Icon snapLefthandIcon;
45 private static Icon snapRighthandIcon;
46 private static Icon waveLefthandIcon;
47 private static Icon waveRighthandIcon;
48
49 private static int GESTURE_WIDTH = 48;
50 private static int GESTURE_HEIGHT = 48;
51 private static int GESTURE_SCALE_HINTS = Image.SCALE_SMOOTH;
52
53 public static Icon getAntiSpellIcon()
54 {
55 if ( antiSpellIcon == null )
56 {
57 antiSpellIcon = createIconFromResource( "images/Anti-Spell.gif" );
58 }
59 return antiSpellIcon;
60 }
61
62 public static Icon getClapLefthandIcon()
63 {
64 if ( clapLefthandIcon == null )
65 {
66 clapLefthandIcon = createIconFromResource( "images/Clap-Lefthand.gif" );
67 }
68 return clapLefthandIcon;
69 }
70
71 public static Icon getClapRighthandIcon()
72 {
73 if ( clapRighthandIcon == null )
74 {
75 clapRighthandIcon = createIconFromResource( "images/Clap-Righthand.gif" );
76 }
77 return clapRighthandIcon;
78 }
79
80 public static Icon getDigitLefthandIcon()
81 {
82 if ( digitLefthandIcon == null )
83 {
84 digitLefthandIcon = createIconFromResource( "images/Digit-Lefthand.gif" );
85 }
86 return digitLefthandIcon;
87 }
88
89 public static Icon getDigitRighthandIcon()
90 {
91 if ( digitRighthandIcon == null )
92 {
93 digitRighthandIcon = createIconFromResource( "images/Digit-Righthand.gif" );
94 }
95 return digitRighthandIcon;
96 }
97
98 public static Icon getEmptyHandIcon()
99 {
100 if ( emptyHandIcon == null )
101 {
102 emptyHandIcon = createIconFromResource( "images/Empty-Hand.gif" );
103 }
104 return emptyHandIcon;
105 }
106
107 public static Icon getFingersLefthandIcon()
108 {
109 if ( fingersLefthandIcon == null )
110 {
111 fingersLefthandIcon = createIconFromResource( "images/Fingers-Lefthand.gif" );
112 }
113 return fingersLefthandIcon;
114 }
115
116 public static Icon getFingersRighthandIcon()
117 {
118 if ( fingersRighthandIcon == null )
119 {
120 fingersRighthandIcon = createIconFromResource( "images/Fingers-Righthand.gif" );
121 }
122 return fingersRighthandIcon;
123 }
124
125 public static Icon getHiddenHandIcon()
126 {
127 if ( hiddenHandIcon == null )
128 {
129 hiddenHandIcon = createIconFromResource( "images/Hidden-Hand.gif" );
130 }
131 return hiddenHandIcon;
132 }
133
134 public static Icon getKnifeLefthandIcon()
135 {
136 if ( knifeLefthandIcon == null )
137 {
138 knifeLefthandIcon = createIconFromResource( "images/Knife-Lefthand.gif" );
139 }
140 return knifeLefthandIcon;
141 }
142
143 public static Icon getKnifeRighthandIcon()
144 {
145 if ( knifeRighthandIcon == null )
146 {
147 knifeRighthandIcon = createIconFromResource( "images/Knife-Righthand.gif" );
148 }
149 return knifeRighthandIcon;
150 }
151
152 public static Icon getPalmLefthandIcon()
153 {
154 if ( palmLefthandIcon == null )
155 {
156 palmLefthandIcon = createIconFromResource( "images/Palm-Lefthand.gif" );
157 }
158 return palmLefthandIcon;
159 }
160
161 public static Icon getPalmRighthandIcon()
162 {
163 if ( palmRighthandIcon == null )
164 {
165 palmRighthandIcon = createIconFromResource( "images/Palm-Righthand.gif" );
166 }
167 return palmRighthandIcon;
168 }
169
170 public static Icon getSnapLefthandIcon()
171 {
172 if ( snapLefthandIcon == null )
173 {
174 snapLefthandIcon = createIconFromResource( "images/Snap-Lefthand.gif" );
175 }
176 return snapLefthandIcon;
177 }
178
179 public static Icon getSnapRighthandIcon()
180 {
181 if ( snapRighthandIcon == null )
182 {
183 snapRighthandIcon = createIconFromResource( "images/Snap-Righthand.gif" );
184 }
185 return snapRighthandIcon;
186 }
187
188 public static Icon getWaveLefthandIcon()
189 {
190 if ( waveLefthandIcon == null )
191 {
192 waveLefthandIcon = createIconFromResource( "images/Wave-Lefthand.gif" );
193 }
194 return waveLefthandIcon;
195 }
196
197 public static Icon getWaveRighthandIcon()
198 {
199 if ( waveRighthandIcon == null )
200 {
201 waveRighthandIcon = createIconFromResource( "images/Wave-Righthand.gif" );
202 }
203 return waveRighthandIcon;
204 }
205
206 private static Icon createIconFromResource( String imageName )
207 {
208 byte[] imageData = getImageBytesFromResource( imageName );
209 MediaTracker tracker = new MediaTracker( new Label() );
210 Image original = Toolkit.getDefaultToolkit().createImage( imageData );
211 tracker.addImage( original, 0 );
212 try
213 {
214 tracker.waitForAll();
215 if ( tracker.isErrorAny() )
216 {
217 System.out.println( "Failed to load Image: " + imageName );
218 }
219 }
220 catch ( InterruptedException ex )
221 {
222 System.out.println( "Unexpected Exception: " + ex.getMessage() );
223 }
224 tracker.removeImage( original );
225 Image scaled = original.getScaledInstance( GESTURE_WIDTH, GESTURE_HEIGHT, GESTURE_SCALE_HINTS );
226 tracker.addImage( scaled, 0 );
227 try
228 {
229 tracker.waitForAll();
230 if ( tracker.isErrorAny() )
231 {
232 System.out.println( "Failed to load Image: " + imageName );
233 }
234 }
235 catch ( InterruptedException ex )
236 {
237 System.out.println( "Unexpected Exception: " + ex.getMessage() );
238 }
239 tracker.removeImage( scaled );
240 Icon result = new ImageIcon( scaled );
241 return result;
242 }
243
244 private static byte[] getImageBytesFromResource( String imageName )
245 {
246 byte[] result = null;
247 InputStream imageInputStream = ClassLoader.getSystemResourceAsStream( imageName );
248 if ( imageInputStream == null )
249 {
250 throw new ImageNotAvailableException( "Could not find Gesture Image for : " + imageName );
251 }
252 try
253 {
254 result = IOUtilities.readAllBytes( imageInputStream );
255 }
256 catch ( IOException e )
257 {
258 throw new ImageNotAvailableException( "Could not find Gesture Image for : " + imageName +
259 ". Reason = " + e.getMessage() );
260 }
261 finally
262 {
263 try
264 {
265 imageInputStream.close();
266 }
267 catch ( IOException ignored )
268 {
269 }
270 }
271 return result;
272 }
273
274 public static Icon getIconForGesture( Gesture g, int hand )
275 {
276 Icon result = null;
277
278 if ( hand != LEFT_HANDED && hand != RIGHT_HANDED )
279 {
280 throw new IllegalArgumentException( "Unknown hand specified to getIconForGesture(): " + hand );
281 }
282
283 if ( GestureFactory.instance().getEmptyHand().equals( g ) )
284 {
285 result = getEmptyHandIcon();
286 }
287 else if ( GestureFactory.instance().getPalm().equals( g ) )
288 {
289 if ( hand == LEFT_HANDED )
290 {
291 result = getPalmLefthandIcon();
292 }
293 else
294 {
295 result = getPalmRighthandIcon();
296 }
297 }
298 else if ( GestureFactory.instance().getDigit().equals( g ) )
299 {
300 if ( hand == LEFT_HANDED )
301 {
302 result = getDigitLefthandIcon();
303 }
304 else
305 {
306 result = getDigitRighthandIcon();
307 }
308 }
309 else if ( GestureFactory.instance().getFingers().equals( g ) )
310 {
311 if ( hand == LEFT_HANDED )
312 {
313 result = getFingersLefthandIcon();
314 }
315 else
316 {
317 result = getFingersRighthandIcon();
318 }
319 }
320 else if ( GestureFactory.instance().getWave().equals( g ) )
321 {
322 if ( hand == LEFT_HANDED )
323 {
324 result = getWaveLefthandIcon();
325 }
326 else
327 {
328 result = getWaveRighthandIcon();
329 }
330 }
331 else if ( GestureFactory.instance().getClap().equals( g ) )
332 {
333 if ( hand == LEFT_HANDED )
334 {
335 result = getClapLefthandIcon();
336 }
337 else
338 {
339 result = getClapRighthandIcon();
340 }
341 }
342 else if ( GestureFactory.instance().getSnap().equals( g ) )
343 {
344 if ( hand == LEFT_HANDED )
345 {
346 result = getSnapLefthandIcon();
347 }
348 else
349 {
350 result = getSnapRighthandIcon();
351 }
352 }
353 else if ( GestureFactory.instance().getKnife().equals( g ) )
354 {
355 if ( hand == LEFT_HANDED )
356 {
357 result = getKnifeLefthandIcon();
358 }
359 else
360 {
361 result = getKnifeRighthandIcon();
362 }
363 }
364 else
365 {
366 throw new RuntimeException( "Unknown Gesture specified in getIconForGesture: " + g );
367 }
368
369 return result;
370 }
371 }
372