home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / Puzzle15 / Src / Pz15Images.Java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  2.7 KB  |  105 lines

  1. //
  2. // (c) 1998 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 Pz15Images implements Pz15Consts
  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_HOT];
  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.waitForAll(); } catch(InterruptedException e) { }
  52.             img[idx] = image;
  53.         }
  54.         return image;
  55.     }
  56.  
  57.     public final static Image getHot(int idx)
  58.     {
  59.         if ( idx >= NUM_IMAGES_HOT )
  60.             return null;
  61.  
  62.         Image image = himg[idx];
  63.  
  64.         if ( image == null ) {
  65.             ImageFilter filter = new SDKHotImages();
  66.  
  67.             if ( applet != null )
  68.                 image = applet.createImage( new FilteredImageSource(get(idx).getSource(), 
  69.                                                                     filter) );
  70.             else
  71.                 image = tk.createImage( new FilteredImageSource(get(idx).getSource(), 
  72.                                                                 filter) );
  73.             tracker.addImage(image, 0);
  74.             try { tracker.waitForAll(); } catch(InterruptedException e) { }
  75.             himg[idx] = image;
  76.         }
  77.         return image;
  78.     }
  79. }
  80.  
  81. class SDKHotImages extends RGBImageFilter
  82. {
  83.     public SDKHotImages()
  84.     {
  85.         // The filter's operation does not depend on the pixel's location, so
  86.         //   IndexColorModels can be filtered directly.
  87.         canFilterIndexColorModel  =  true;
  88.     }
  89.  
  90.     public int filterRGB(int  x,  int  y,  int  rgb)
  91.     {
  92.         int red, blue, green;
  93.  
  94.         red = ((rgb & 0xff0000) >> 16) - 30;
  95.         red = (red < 0 ? 0 : red);
  96.         red = red << 16;
  97.         green = ((rgb & 0xff00) >> 8) + 30;
  98.         green = (green > 0xff ? 0xff: green);
  99.         green = green << 8;
  100.         blue = (rgb & 0xff) - 30;
  101.         blue = (blue < 0 ? 0 : blue);
  102.  
  103.         return ( (rgb & 0xff000000) | red | green | blue );
  104.     }
  105. }