1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.image.*; 11 import java.net.URL ; 12 13 import java.io.Serializable ; 14 import java.io.ObjectOutputStream ; 15 import java.io.ObjectInputStream ; 16 import java.io.IOException ; 17 18 import java.util.Locale ; 19 import javax.accessibility.*; 20 21 22 47 public class ImageIcon implements Icon , Serializable , Accessible { 48 53 transient private String filename; 54 transient private URL location; 55 56 transient Image image; 57 transient int loadStatus = 0; 58 ImageObserver imageObserver; 59 String description = null; 60 61 protected final static Component component = new Component() {}; 62 protected final static MediaTracker tracker = new MediaTracker(component); 63 64 67 private static int mediaTrackerID; 68 69 int width = -1; 70 int height = -1; 71 72 80 public ImageIcon(String filename, String description) { 81 image = Toolkit.getDefaultToolkit().getImage(filename); 82 if (image == null) { 83 return; 84 } 85 this.filename = filename; 86 this.description = description; 87 loadImage(image); 88 } 89 90 106 public ImageIcon (String filename) { 107 this(filename, filename); 108 } 109 110 118 public ImageIcon(URL location, String description) { 119 image = Toolkit.getDefaultToolkit().getImage(location); 120 if (image == null) { 121 return; 122 } 123 this.location = location; 124 this.description = description; 125 loadImage(image); 126 } 127 128 137 public ImageIcon (URL location) { 138 this(location, location.toExternalForm()); 139 } 140 141 146 public ImageIcon(Image image, String description) { 147 this(image); 148 this.description = description; 149 } 150 151 159 public ImageIcon (Image image) { 160 this.image = image; 161 Object o = image.getProperty("comment", imageObserver); 162 if (o instanceof String ) { 163 description = (String ) o; 164 } 165 loadImage(image); 166 } 167 168 181 public ImageIcon (byte[] imageData, String description) { 182 this.image = Toolkit.getDefaultToolkit().createImage(imageData); 183 if (image == null) { 184 return; 185 } 186 this.description = description; 187 loadImage(image); 188 } 189 190 206 public ImageIcon (byte[] imageData) { 207 this.image = Toolkit.getDefaultToolkit().createImage(imageData); 208 if (image == null) { 209 return; 210 } 211 Object o = image.getProperty("comment", imageObserver); 212 if (o instanceof String ) { 213 description = (String ) o; 214 } 215 loadImage(image); 216 } 217 218 221 public ImageIcon() { 222 } 223 224 228 protected void loadImage(Image image) { 229 synchronized(tracker) { 230 int id = getNextID(); 231 232 tracker.addImage(image, id); 233 try { 234 tracker.waitForID(id, 0); 235 } catch (InterruptedException e) { 236 System.out.println("INTERRUPTED while loading Image"); 237 } 238 loadStatus = tracker.statusID(id, false); 239 tracker.removeImage(image, id); 240 241 width = image.getWidth(imageObserver); 242 height = image.getHeight(imageObserver); 243 } 244 } 245 246 249 private int getNextID() { 250 synchronized(tracker) { 251 return ++mediaTrackerID; 252 } 253 } 254 255 262 public int getImageLoadStatus() { 263 return loadStatus; 264 } 265 266 270 public Image getImage() { 271 return image; 272 } 273 274 278 public void setImage(Image image) { 279 this.image = image; 280 loadImage(image); 281 } 282 283 292 public String getDescription() { 293 return description; 294 } 295 296 303 public void setDescription(String description) { 304 this.description = description; 305 } 306 307 322 public synchronized void paintIcon(Component c, Graphics g, int x, int y) { 323 if(imageObserver == null) { 324 g.drawImage(image, x, y, c); 325 } else { 326 g.drawImage(image, x, y, imageObserver); 327 } 328 } 329 330 335 public int getIconWidth() { 336 return width; 337 } 338 339 344 public int getIconHeight() { 345 return height; 346 } 347 348 361 public void setImageObserver(ImageObserver observer) { 362 imageObserver = observer; 363 } 364 365 370 public ImageObserver getImageObserver() { 371 return imageObserver; 372 } 373 374 379 public String toString() { 380 if (description != null) { 381 return description; 382 } 383 return super.toString(); 384 } 385 386 private void readObject(ObjectInputStream s) 387 throws ClassNotFoundException , IOException 388 { 389 s.defaultReadObject(); 390 391 int w = s.readInt(); 392 int h = s.readInt(); 393 int[] pixels = (int[])(s.readObject()); 394 395 if (pixels != null) { 396 Toolkit tk = Toolkit.getDefaultToolkit(); 397 ColorModel cm = ColorModel.getRGBdefault(); 398 image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w)); 399 loadImage(image); 400 } 401 } 402 403 404 private void writeObject(ObjectOutputStream s) 405 throws IOException 406 { 407 s.defaultWriteObject(); 408 409 int w = getIconWidth(); 410 int h = getIconHeight(); 411 int[] pixels = image != null? new int[w * h] : null; 412 413 if (image != null) { 414 try { 415 PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w); 416 pg.grabPixels(); 417 if ((pg.getStatus() & ImageObserver.ABORT) != 0) { 418 throw new IOException ("failed to load image contents"); 419 } 420 } 421 catch (InterruptedException e) { 422 throw new IOException ("image load interrupted"); 423 } 424 } 425 426 s.writeInt(w); 427 s.writeInt(h); 428 s.writeObject(pixels); 429 } 430 431 434 435 private AccessibleImageIcon accessibleContext = null; 436 437 449 public AccessibleContext getAccessibleContext() { 450 if (accessibleContext == null) { 451 accessibleContext = new AccessibleImageIcon(); 452 } 453 return accessibleContext; 454 } 455 456 471 protected class AccessibleImageIcon extends AccessibleContext 472 implements AccessibleIcon, Serializable { 473 474 477 478 485 public AccessibleRole getAccessibleRole() { 486 return AccessibleRole.ICON; 487 } 488 489 496 public AccessibleStateSet getAccessibleStateSet() { 497 return null; 498 } 499 500 508 public Accessible getAccessibleParent() { 509 return null; 510 } 511 512 519 public int getAccessibleIndexInParent() { 520 return -1; 521 } 522 523 530 public int getAccessibleChildrenCount() { 531 return 0; 532 } 533 534 540 public Accessible getAccessibleChild(int i) { 541 return null; 542 } 543 544 549 public Locale getLocale() throws IllegalComponentStateException { 550 return null; 551 } 552 553 556 557 565 public String getAccessibleIconDescription() { 566 return ImageIcon.this.getDescription(); 567 } 568 569 577 public void setAccessibleIconDescription(String description) { 578 ImageIcon.this.setDescription(description); 579 } 580 581 586 public int getAccessibleIconHeight() { 587 return ImageIcon.this.height; 588 } 589 590 595 public int getAccessibleIconWidth() { 596 return ImageIcon.this.width; 597 } 598 599 private void readObject(ObjectInputStream s) 600 throws ClassNotFoundException , IOException 601 { 602 s.defaultReadObject(); 603 } 604 605 private void writeObject(ObjectOutputStream s) 606 throws IOException 607 { 608 s.defaultWriteObject(); 609 } 610 } } 612 613 | Popular Tags |