home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc102 / Buttons / Src / SDKImages.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  2.7 KB  |  103 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.MediaTracker;
  7. import java.awt.image.RGBImageFilter;
  8. import java.awt.image.FilteredImageSource;
  9. import java.awt.image.ImageFilter;
  10. import java.net.URL;
  11. import com.ms.ui.*;
  12.  
  13. //
  14. // This class assumes that an Images directory containing NUM_IMAGES 
  15. // images is contained in the application/applet directory.
  16. //
  17. public class SDKImages implements SDKConsts
  18. {
  19.     private static UIApplet applet = null;
  20.     private static MediaTracker tracker = null;
  21.     private static URL url = null;
  22.     private static Toolkit tk = null;
  23.     private static String filesep = System.getProperty("file.separator");
  24.  
  25.     private static Image img[] = new Image[NUM_IMAGES];
  26.     private static Image himg[] = new Image[NUM_IMAGES];
  27.  
  28.     public final static void init(UIApplet aplt, UIFrame frame)
  29.     {
  30.         if ( aplt != null ) {
  31.             applet = aplt;
  32.             tracker = new MediaTracker(applet.getApplet());
  33.             url = applet.getCodeBase();
  34.         }
  35.         else {
  36.             tracker = new MediaTracker(frame.getFrame());
  37.             tk = frame.getToolkit();
  38.         }
  39.     }
  40.  
  41.     public final static Image get(int idx)
  42.     {
  43.         Image image = img[idx];
  44.  
  45.         if ( image == null ) {
  46.             if ( applet != null )
  47.                 image = applet.getImage(url, "Images" + filesep + IMG_NAMES[idx]);
  48.             else
  49.                 image = tk.getImage("Images" + filesep + IMG_NAMES[idx]);
  50.             tracker.addImage(image, 0);
  51.             try { tracker.waitForID(0); } catch(InterruptedException e) { }
  52.             img[idx] = image;
  53.         }
  54.         return image;
  55.     }
  56.  
  57.     public final static Image getHot(int idx)
  58.     {
  59.         Image image = himg[idx];
  60.  
  61.         if ( image == null ) {
  62.             ImageFilter filter = new SDKHotImages();
  63.  
  64.             if ( applet != null )
  65.                 image = applet.createImage( new FilteredImageSource(get(idx).getSource(), 
  66.                                                                     filter) );
  67.             else
  68.                 image = tk.createImage( new FilteredImageSource(get(idx).getSource(), 
  69.                                                                 filter) );
  70.             tracker.addImage(image, 0);
  71.             try { tracker.waitForAll(); } catch(InterruptedException e) { }
  72.             himg[idx] = image;
  73.         }
  74.         return image;
  75.     }
  76. }
  77.  
  78. class SDKHotImages extends RGBImageFilter
  79. {
  80.     public SDKHotImages()
  81.     {
  82.         // The filter's operation does not depend on the pixel's location, so
  83.         //   IndexColorModels can be filtered directly.
  84.         canFilterIndexColorModel  =  true;
  85.     }
  86.  
  87.     public int filterRGB(int  x,  int  y,  int  rgb)
  88.     {
  89.         int red, blue, green;
  90.  
  91.         red = ((rgb & 0xff0000) >> 16) - 30;
  92.         red = (red < 0 ? 0 : red);
  93.         red = red << 16;
  94.         green = ((rgb & 0xff00) >> 8) + 30;
  95.         green = (green > 0xff ? 0xff: green);
  96.         green = green << 8;
  97.         blue = (rgb & 0xff) - 30;
  98.         blue = (blue < 0 ? 0 : blue);
  99.  
  100.         return ( (rgb & 0xff000000) | red | green | blue );
  101.     }
  102. }
  103.