1 36 37 40 41 import java.applet.Applet ; 42 import java.awt.Image ; 43 import java.awt.Graphics ; 44 import java.awt.Rectangle ; 45 import java.awt.MediaTracker ; 46 import java.awt.event.*; 47 import java.util.StringTokenizer ; 48 import java.util.Vector ; 49 import java.util.Hashtable ; 50 import java.net.URL ; 51 import java.awt.image.ImageProducer ; 52 import java.awt.image.ImageFilter ; 53 import java.awt.image.CropImageFilter ; 54 import java.awt.image.FilteredImageSource ; 55 import java.net.MalformedURLException ; 56 57 65 public class ImageMap 66 extends Applet 67 implements Runnable , MouseListener, MouseMotionListener { 68 71 Image baseImage; 72 73 76 ImageMapArea areas[]; 77 78 81 static final int BRIGHTER = 0; 82 static final int DARKER = 1; 83 84 int hlmode = BRIGHTER; 85 86 89 int hlpercent = 50; 90 91 94 MediaTracker tracker; 95 96 100 Image getHighlight(int x, int y, int w, int h) { 101 return getHighlight(x, y, w, h, hlmode, hlpercent); 102 } 103 104 107 Image getHighlight(int x, int y, int w, int h, int mode, int percent) { 108 return getHighlight(x, y, w, h, new HighlightFilter(mode == BRIGHTER, 109 percent)); 110 } 111 112 115 Image getHighlight(int x, int y, int w, int h, ImageFilter filter) { 116 ImageFilter cropfilter = new CropImageFilter (x, y, w, h); 117 ImageProducer prod = new FilteredImageSource (baseImage.getSource(), 118 cropfilter); 119 return makeImage(prod, filter, 0); 120 } 121 122 125 Image makeImage(Image orig, ImageFilter filter) { 126 return makeImage(orig.getSource(), filter); 127 } 128 129 132 Image makeImage(ImageProducer prod, ImageFilter filter) { 133 return makeImage(prod, filter, 134 (prod == baseImage.getSource()) ? 1 : 0); 135 } 136 137 141 Image makeImage(ImageProducer prod, ImageFilter filter, int ID) { 142 Image filtered = createImage(new FilteredImageSource (prod, filter)); 143 tracker.addImage(filtered, ID); 144 return filtered; 145 } 146 147 150 void addImage(Image img) { 151 tracker.addImage(img, 1); 152 } 153 154 157 void parseHighlight(String s) { 158 if (s == null) { 159 return; 160 } 161 if (s.startsWith("brighter") || s.startsWith("BRIGHTER")) { 162 hlmode = BRIGHTER; 163 if (s.length() > "brighter".length()) { 164 hlpercent = Integer.parseInt(s.substring("brighter".length())); 165 } 166 } else if (s.startsWith("darker") || s.startsWith("DARKER")) { 167 hlmode = DARKER; 168 if (s.length() > "darker".length()) { 169 hlpercent = Integer.parseInt(s.substring("darker".length())); 170 } 171 } 172 } 173 174 188 public void init() { 189 String s; 190 191 tracker = new MediaTracker (this); 192 parseHighlight(getParameter("highlight")); 193 introTune = getParameter("startsound"); 194 baseImage = getImage(getDocumentBase(), getParameter("img")); 195 Vector areaVec = new Vector (); 196 int num = 1; 197 while (true) { 198 ImageMapArea newArea; 199 s = getParameter("area"+num); 200 if (s == null) { 201 s = getParameter("rect"+num); 203 if (s == null) { 204 break; 205 } 206 try { 207 newArea = new HighlightArea(); 208 newArea.init(this, s); 209 areaVec.addElement(newArea); 210 String url = getParameter("href"+num); 211 if (url != null) { 212 s += "," + url; 213 newArea = new LinkArea(); 214 newArea.init(this, s); 215 areaVec.addElement(newArea); 216 } 217 } catch (Exception e) { 218 System.out.println("error processing: "+s); 219 e.printStackTrace(); 220 break; 221 } 222 } else { 223 try { 224 int classend = s.indexOf(","); 225 String name = s.substring(0, classend); 226 newArea = (ImageMapArea) Class.forName(name).newInstance(); 227 s = s.substring(classend+1); 228 newArea.init(this, s); 229 areaVec.addElement(newArea); 230 } catch (Exception e) { 231 System.out.println("error processing: "+s); 232 e.printStackTrace(); 233 break; 234 } 235 } 236 num++; 237 } 238 areas = new ImageMapArea[areaVec.size()]; 239 areaVec.copyInto(areas); 240 checkSize(); 241 addMouseListener(this); 242 addMouseMotionListener(this); 243 } 244 245 public void destroy() { 246 removeMouseListener(this); 247 removeMouseMotionListener(this); 248 } 249 250 Thread aniThread = null; 251 String introTune = null; 252 253 public void start() { 254 if (introTune != null) 255 try { 256 play(new URL (getDocumentBase(), introTune)); 257 } catch (MalformedURLException e) {} 258 if (aniThread == null) { 259 aniThread = new Thread (this); 260 aniThread.setName("ImageMap Animator"); 261 aniThread.start(); 262 } 263 } 264 265 public void run() { 266 Thread me = Thread.currentThread(); 267 tracker.checkAll(true); 268 for (int i = areas.length; --i >= 0; ) { 269 areas[i].getMedia(); 270 } 271 me.setPriority(Thread.MIN_PRIORITY); 272 while (aniThread == me) { 273 boolean animating = false; 274 for (int i = areas.length; --i >= 0; ) { 275 animating = areas[i].animate() || animating; 276 } 277 try { 278 synchronized(this) { 279 wait(animating ? 100 : 0); 280 } 281 } catch (InterruptedException e) { 282 break; 283 } 284 } 285 } 286 287 public synchronized void startAnimation() { 288 notify(); 289 } 290 291 public synchronized void stop() { 292 aniThread = null; 293 notify(); 294 for (int i = 0; i < areas.length; i++) { 295 areas[i].exit(); 296 } 297 } 298 299 302 void checkSize() { 303 int w = baseImage.getWidth(this); 304 int h = baseImage.getHeight(this); 305 if (w > 0 && h > 0) { 306 resize(w, h); 307 synchronized(this) { 308 fullrepaint = true; 309 } 310 repaint(0, 0, w, h); 311 } 312 } 313 314 private boolean fullrepaint = false; 315 private final static long UPDATERATE = 100; 316 317 320 public boolean imageUpdate(Image img, int infoflags, 321 int x, int y, int width, int height) { 322 if ((infoflags & (WIDTH | HEIGHT)) != 0) { 323 checkSize(); 324 } 325 if ((infoflags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0) { 326 synchronized(this) { 327 fullrepaint = true; 328 } 329 repaint(((infoflags & (FRAMEBITS | ALLBITS)) != 0) 330 ? 0 : UPDATERATE, 331 x, y, width, height); 332 } 333 return (infoflags & (ALLBITS | ERROR)) == 0; 334 } 335 336 339 public void paint(Graphics g) { 340 synchronized(this) { 341 fullrepaint = false; 342 } 343 if (baseImage == null) { 344 return; 345 } 346 g.drawImage(baseImage, 0, 0, this); 347 if (areas != null) { 348 for (int i = areas.length; --i >= 0; ) { 349 areas[i].highlight(g); 350 } 351 } 352 } 353 354 357 public void update(Graphics g) { 358 boolean full; 359 synchronized(this) { 360 full = fullrepaint; 361 } 362 if (full) { 363 paint(g); 364 return; 365 } 366 if (baseImage == null) { 367 return; 368 } 369 g.drawImage(baseImage, 0, 0, this); 370 if (areas == null) { 371 return; 372 } 373 for (int i = areas.length; --i >= 0; ) { 375 areas[i].highlight(g); 376 } 377 } 378 379 int pressX; 380 int pressY; 381 382 public void mouseClicked(MouseEvent e) 383 {} 384 385 388 public void mousePressed(MouseEvent e) 389 { 390 pressX = e.getX(); 391 pressY = e.getY(); 392 393 for (int i = 0; i < areas.length; i++) { 394 if (areas[i].inside(pressX, pressY)) { 395 if (areas[i].press(pressX, pressY)) { 396 break; 397 } 398 } 399 } 400 e.consume(); 401 } 402 403 408 public void mouseReleased(MouseEvent e) 409 { 410 for (int i = 0; i < areas.length; i++) { 411 if (areas[i].inside(pressX, pressY)) { 412 if (areas[i].lift(e.getX(), e.getY())) { 413 break; 414 } 415 } 416 } 417 e.consume(); 418 } 419 420 public void mouseEntered(MouseEvent e) 421 {} 422 423 426 public void mouseExited(MouseEvent e) { 427 for (int i = 0; i < areas.length; i++) { 428 areas[i].checkExit(); 429 } 430 e.consume(); 431 } 432 433 434 439 440 public void mouseDragged(MouseEvent e) 441 { 442 mouseMoved(e); 443 for (int i = 0; i < areas.length; i++) { 444 if (areas[i].inside(pressX, pressY)) { 445 if (areas[i].drag(e.getX(), e.getY())) { 446 break; 447 } 448 } 449 } 450 e.consume(); 451 } 452 453 456 public void mouseMoved(MouseEvent e) { 457 boolean eaten = false; 458 459 for (int i = 0; i < areas.length; i++) { 460 if (!eaten && areas[i].inside(e.getX(), e.getY())) { 461 eaten = areas[i].checkEnter(e.getX(), e.getY()); 462 } else { 463 areas[i].checkExit(); 464 } 465 } 466 e.consume(); 467 } 468 469 472 public void newStatus() { 473 String msg = null; 474 for (int i = 0; i < areas.length; i++) { 475 msg = areas[i].getStatus(msg); 476 } 477 showStatus(msg); 478 } 479 480 public String getAppletInfo() { 481 return "Title: ImageMap \nAuthor: Jim Graham \nAn extensible ImageMap applet class. \nThe active areas on the image are controlled by ImageArea \nclasses that can be dynamically loaded over the net."; 482 } 483 484 public String [][] getParameterInfo() { 485 String [][] info = { 486 {"area[n]", "delimited string", 487 "This parameter takes the form of <ImageAreaClassName>, <ul>, <ur>, <ll>, <lr>, <action> where ImageAreaClassName is the name of the class from which this feedback area is controlled, the next four arguments are the four corners of the " 488 + " feedback zone, and the final argument is that action that should be taken on click or mouseover. That action can be 1) display text in the status bar (if you provide a string argument), 2) play a sound (if you provide the path to a sound file), or 3) load a page (if you provide a URL)."}, 489 {"rect[n]", "delimited string", "Deprecated: use area[n]"}, 490 {"href[n]", "URL string", "Pass in a URL to create a LinkArea which will point to this URL. Not used in these examples."}, 491 {"highlight", "string/int", "Pass the word 'brighter' followed by an integer 'n' to change the highlight mode to brighter and the hightlight percentage to n. Pass the word 'darker' followed by an integer 'm' to change the highlight mode to darker and the highlight percentage to m. Anything else will be ignored. The default highlight mode is BRIGHTER and the default highlight percentage is 50."}, 492 {"startsound", "path string", "The path of a soundclip to play when the image is first displayed."}, 493 {"img", "path string", "The path to the image to be displayed as a live feedback image map."} 494 }; 495 return info; 496 } 497 } 498 499 500 501 | Popular Tags |