home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / DemoImages.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  2.6 KB  |  82 lines

  1. /*
  2.  * @(#)DemoImages.java    1.3 98/09/13
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.image.*;
  18. import java.net.URL;
  19. import java.io.File;
  20. import java.util.Hashtable;
  21.  
  22.  
  23. /**
  24.  * A cache of all the demo images found in the images directory.
  25.  */
  26. public class DemoImages extends Component {
  27.  
  28.     public static Hashtable cache;
  29.  
  30.     public DemoImages() {
  31.         URL url = DemoImages.class.getResource("images");
  32.         File dir = new File(url.getFile());
  33.         if (dir != null && dir.isDirectory()) {
  34.             String list[] = dir.list();
  35.             cache = new Hashtable(list.length);
  36.             for (int i = 0; i < list.length; i++) {
  37.                 cache.put(list[i], createImage(list[i], this));
  38.             }
  39.         }
  40.         if (cache.containsKey("jumptojavastrip.gif")) {
  41.             Image img = (Image) cache.get("jumptojavastrip.gif");
  42.             for (int i=0, x=0; i < 10; i++, x+=80) {
  43.                 String s = "jumptojavastrip-" + String.valueOf(i) + ".gif";
  44.                 cache.put(s, getCroppedImage(img, x, 0, 80, 80, this));
  45.             }
  46.         }
  47.     }
  48.  
  49.  
  50.     public static Image createImage(String fileName, Component cmp) {
  51.         URL url = DemoImages.class.getResource("images/" + fileName);
  52.         Image img = cmp.getToolkit().createImage(url);
  53.         trackImage(img, cmp);
  54.         return img;
  55.     }
  56.  
  57.  
  58.     public static Image getCroppedImage(Image img, 
  59.                                         int x, int y, 
  60.                                         int w, int h, 
  61.                                         Component cmp) {
  62.         ImageProducer imgP = img.getSource();
  63.         CropImageFilter cif = new CropImageFilter(x, y, w, h);
  64.         ImageProducer ip = new FilteredImageSource(imgP, cif);
  65.         Image croppedimage = cmp.getToolkit().createImage(ip);
  66.         trackImage(croppedimage, cmp);
  67.         return croppedimage;
  68.     }
  69.  
  70.        
  71.     private static void trackImage(Image img, Component cmp) {
  72.         MediaTracker tracker = new MediaTracker(cmp);
  73.         tracker.addImage(img, 0);
  74.         try {
  75.             tracker.waitForID(0);
  76.             if (tracker.isErrorAny()) {
  77.                 System.out.println("Error loading image");
  78.             }
  79.         } catch (Exception ex) { ex.printStackTrace(); } 
  80.     }
  81. }
  82.