KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ImageMapArea


1 /*
2  * @(#)ImageMapArea.java 1.19 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)ImageMapArea.java 1.19 06/02/22
39  */

40
41 import java.awt.Graphics JavaDoc;
42 import java.awt.Image JavaDoc;
43 import java.awt.image.*;
44 import java.util.StringTokenizer JavaDoc;
45 import java.net.URL JavaDoc;
46 import java.net.MalformedURLException JavaDoc;
47
48 /**
49  * The base ImageArea class.
50  * This class performs the basic functions that most ImageArea
51  * classes will need and delegates specific actions to the subclasses.
52  *
53  * @author Jim Graham
54  * @version 1.19, 02/22/06
55  */

56 class ImageMapArea implements ImageObserver {
57     /** The applet parent that contains this ImageArea. */
58     ImageMap parent;
59     /** The X location of the area (if rectangular). */
60     int X;
61     /** The Y location of the area (if rectangular). */
62     int Y;
63     /** The size().width of the area (if rectangular). */
64     int W;
65     /** The size().height of the area (if rectangular). */
66     int H;
67     /**
68      * This flag indicates whether the user was in this area during the
69      * last scan of mouse locations.
70      */

71     boolean entered = false;
72     /** This flag indicates whether the area is currently highlighted. */
73     boolean active = false;
74
75     /**
76      * This is the default highlight image if no special effects are
77      * needed to draw the highlighted image. It is created by the
78      * default "makeImages()" method.
79      */

80     Image JavaDoc hlImage;
81
82     /**
83      * This is the status string requested by this area. Only the
84      * status string from the topmost area which has requested one
85      * will be displayed.
86      */

87     String JavaDoc status;
88
89     /**
90      * Initialize this ImageArea as called from the applet.
91      * If the subclass does not override this initializer, then it
92      * will perform the basic functions of setting the parent applet
93      * and parsing out 4 numbers from the argument string which specify
94      * a rectangular region for the ImageArea to act on.
95      * The remainder of the argument string is passed to the handleArg()
96      * method for more specific handling by the subclass.
97      */

98     public void init(ImageMap parent, String JavaDoc args) {
99     this.parent = parent;
100     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args, ", ");
101     X = Integer.parseInt(st.nextToken());
102     Y = Integer.parseInt(st.nextToken());
103     W = Integer.parseInt(st.nextToken());
104     H = Integer.parseInt(st.nextToken());
105     if (st.hasMoreTokens()) {
106         handleArg(st.nextToken(","));
107     } else {
108         handleArg(null);
109     }
110     makeImages();
111     }
112
113     /**
114      * This method handles the remainder of the argument string after
115      * the standard initializer has parsed off the 4 rectangular
116      * parameters. If the subclass does not override this method,
117      * the remainder will be ignored.
118      */

119     public void handleArg(String JavaDoc s) {
120     }
121
122     /**
123      * This method loads any additional media that the ImageMapArea
124      * may need for its animations.
125      */

126     public void getMedia() {
127     }
128
129     /**
130      * This method is called every animation cycle if there are any
131      * active animating areas.
132      * @return true if this area requires further animation notifications
133      */

134     public boolean animate() {
135     return false;
136     }
137
138     /**
139      * This method sets the image to be used to render the ImageArea
140      * when it is highlighted.
141      */

142     public void setHighlight(Image JavaDoc img) {
143     hlImage = img;
144     }
145
146     /**
147      * This method handles the construction of the various images
148      * used to highlight this particular ImageArea when the user
149      * interacts with it.
150      */

151     public void makeImages() {
152     setHighlight(parent.getHighlight(X, Y, W, H));
153     }
154
155     /**
156      * The repaint method causes the area to be repainted at the next
157      * opportunity.
158      */

159     public void repaint() {
160     parent.repaint(0, X, Y, W, H);
161     }
162
163     /**
164      * This method tests to see if a point is inside this ImageArea.
165      * The standard method assumes a rectangular area as parsed by
166      * the standard initializer. If a more complex area is required
167      * then this method will have to be overridden by the subclass.
168      */

169     public boolean inside(int x, int y) {
170     return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
171     }
172
173     /**
174      * This utility method draws a rectangular subset of a highlight
175      * image.
176      */

177     public void drawImage(Graphics JavaDoc g, Image JavaDoc img, int imgx, int imgy,
178               int x, int y, int w, int h) {
179     Graphics JavaDoc ng = g.create();
180     try {
181         ng.clipRect(x, y, w, h);
182         ng.drawImage(img, imgx, imgy, this);
183     } finally {
184         ng.dispose();
185     }
186     }
187
188     /**
189      * This method handles the updates from drawing the images.
190      */

191     public boolean imageUpdate(Image JavaDoc img, int infoflags,
192                    int x, int y, int width, int height) {
193     if (img == hlImage) {
194         return parent.imageUpdate(img, infoflags, x + X, y + Y,
195                       width, height);
196     } else {
197         return (infoflags & (ALLBITS | ERROR)) == 0;
198     }
199     }
200
201     /**
202      * This utility method records a string to be shown in the status bar.
203      */

204     public void showStatus(String JavaDoc msg) {
205     status = msg;
206     parent.newStatus();
207     }
208
209     /**
210      * This utility method returns the status string this area wants to
211      * put into the status bar. If no previous area (higher in the
212      * stacking order) has yet returned a status message, prevmsg will
213      * be null and this area will then return its own message, otherwise
214      * it will leave the present message alone.
215      */

216     public String JavaDoc getStatus(String JavaDoc prevmsg) {
217     return (prevmsg == null) ? status : prevmsg;
218     }
219
220     /**
221      * This utility method tells the browser to visit a URL.
222      */

223     public void showDocument(URL JavaDoc u) {
224     parent.getAppletContext().showDocument(u);
225     }
226
227     /**
228      * This method highlights the specified area when the user enters
229      * it with his mouse. The standard highlight method is to replace
230      * the indicated rectangular area of the image with the primary
231      * highlighted image.
232      */

233     public void highlight(Graphics JavaDoc g) {
234     }
235
236     /**
237      * The checkEnter method is called when the mouse is inside the
238      * region to see if the area needs to have its enter method called.
239      * The default implementation simply checks if the entered flag is
240      * set and only calls enter if it is false.
241      */

242     public boolean checkEnter(int x, int y) {
243     if (!entered) {
244         entered = true;
245         enter(x, y);
246     }
247     return isTerminal();
248     }
249
250     /**
251      * The checkExit method is called when the mouse is outside the
252      * region to see if the area needs to have its exit method called.
253      * The default implementation simply checks if the entered flag is
254      * set and only calls exit if it is true.
255      */

256     public void checkExit() {
257     if (entered) {
258         entered = false;
259         exit();
260     }
261     }
262
263     /**
264      * The isTerminal method controls whether events propagate to the
265      * areas which lie beneath this one.
266      * @return true if the events should be propagated to the underlying
267      * areas.
268      */

269     public boolean isTerminal() {
270     return false;
271     }
272
273     /**
274      * The enter method is called when the mouse enters the region.
275      * The location is supplied, but the standard implementation is
276      * to call the overloaded method with no arguments.
277      */

278     public void enter(int x, int y) {
279     enter();
280     }
281
282     /**
283      * The overloaded enter method is called when the mouse enters
284      * the region. This method can be overridden if the ImageArea
285      * does not need to know where the mouse entered.
286      */

287     public void enter() {
288     }
289
290     /**
291      * The exit method is called when the mouse leaves the region.
292      */

293     public void exit() {
294     }
295
296     /**
297      * The press method is called when the user presses the mouse
298      * button inside the ImageArea. The location is supplied, but
299      * the standard implementation is to call the overloaded method
300      * with no arguments.
301      * @return true if this ImageMapArea wants to prevent any underlying
302      * areas from seeing the press
303      */

304     public boolean press(int x, int y) {
305     return press();
306     }
307
308     /**
309      * The overloaded press method is called when the user presses the
310      * mouse button inside the ImageArea. This method can be overridden
311      * if the ImageArea does not need to know the location of the press.
312      * @return true if this ImageMapArea wants to prevent any underlying
313      * areas from seeing the press
314      */

315     public boolean press() {
316     return isTerminal();
317     }
318
319     /**
320      * The lift method is called when the user releases the mouse button.
321      * The location is supplied, but the standard implementation is to
322      * call the overloaded method with no arguments. Only those ImageAreas
323      * that were informed of a press will be informed of the corresponding
324      * release.
325      * @return true if this ImageMapArea wants to prevent any underlying
326      * areas from seeing the lift
327      */

328     public boolean lift(int x, int y) {
329     return lift();
330     }
331
332     /**
333      * The overloaded lift method is called when the user releases the
334      * mouse button. This method can be overridden if the ImageArea
335      * does not need to know the location of the release.
336      * @return true if this ImageMapArea wants to prevent any underlying
337      * areas from seeing the lift
338      */

339     public boolean lift() {
340     return isTerminal();
341     }
342
343     /**
344      * The drag method is called when the user moves the mouse while
345      * the button is pressed. Only those ImageAreas that were informed
346      * of a press will be informed of the corresponding mouse movements.
347      * @return true if this ImageMapArea wants to prevent any underlying
348      * areas from seeing the drag
349      */

350     public boolean drag(int x, int y) {
351     return isTerminal();
352     }
353
354   public String JavaDoc getAppletInfo() {
355     return "Title: ImageArea \nAuthor: Jim Graham \nThis class performs the basic functions that most ImageArea classes will need and delegates specific actions to the subclasses.";
356   }
357 }
358
359
Popular Tags