home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / dek17tau / classes / spt / gui / imagepanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  5.4 KB  |  237 lines

  1.  
  2. /*
  3.  * ImagePanel.java
  4.  *
  5.  * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
  6.  */
  7.  
  8. package spt.gui;
  9.  
  10. import java.lang.String;
  11. import java.awt.Panel;
  12. import java.awt.Image;
  13. import java.awt.Graphics;
  14. import java.awt.MediaTracker;
  15. import java.awt.Component;
  16. import java.applet.Applet;
  17. import java.net.URL;
  18. import java.awt.Color;
  19. import java.awt.image.ImageObserver;
  20. import java.awt.image.PixelGrabber;
  21. import java.awt.image.MemoryImageSource;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24.  
  25.  
  26. /**
  27.  * A Panel that displays an Image.
  28.  *
  29.  * @author Shaun Terry
  30.  */
  31.  
  32. public class ImagePanel extends Panel {
  33.     Image img;
  34.  
  35.     /** Creates an ImagePanel with the image in the specified file
  36.      *    (no good for applets).
  37.      */
  38.     public ImagePanel(String file) throws FileNotFoundException {
  39.         setLayout(null);
  40.  
  41.         img = loadImage(this, file);
  42.  
  43.         resizeToImage();
  44.     }
  45.  
  46.     /** Creates an ImagePanel with the image in the specified URL
  47.      *    (applets only).
  48.      */
  49.     public ImagePanel(Applet a, URL file) {
  50.         setLayout(null);
  51.  
  52.         img = loadImage(a, file);
  53.  
  54.         resizeToImage();
  55.     }
  56.  
  57.     /** Load an image.
  58.      */
  59.     static public Image loadImage(Component comp, String file) throws FileNotFoundException {
  60.         Image i;
  61.  
  62.         File f = new File(file);
  63.         if (!f.exists()) throw new FileNotFoundException(file);
  64.  
  65.         MediaTracker t = new MediaTracker(comp);
  66.         i = comp.getToolkit().getImage(file);
  67.         t.addImage(i, 0);
  68.         try {
  69.             t.waitForAll();
  70.         } catch (InterruptedException ee) {}
  71.  
  72.         return i;
  73.     }
  74.  
  75.     /** Load an image.
  76.      */
  77.     static public Image loadImage(Applet a, URL file) {
  78.         Image i;
  79.  
  80.         MediaTracker t = new MediaTracker(a);
  81.         i = a.getImage(file);
  82.         t.addImage(i, 0);
  83.         try {
  84.             t.waitForAll();
  85.         } catch (InterruptedException ee) {}
  86.  
  87.         return i;
  88.     }
  89.  
  90.     /** Return a "grayed-out" version of the given image.
  91.      */
  92.     static public Image createDisabledImage(Image im, Component comp) {
  93.         int w = im.getWidth(comp);
  94.         int h = im.getHeight(comp);
  95.         int pixels[] = new int[w * h];
  96.         int emb[] = new int[w * h];
  97.         PixelGrabber pg = new PixelGrabber(im, 0, 0, w, h, pixels, 0, w);
  98.         try {
  99.             pg.grabPixels();
  100.         } catch (InterruptedException e) {
  101.             return null;
  102.         }
  103.         if ((pg.status() & ImageObserver.ABORT) != 0) {
  104.             return null;
  105.         }
  106.  
  107.         Color bg = comp.getBackground();
  108.         Color darker = bg.darker();
  109.         Color brighter = bg.brighter();
  110.  
  111.         // Set the entire image to the bg color
  112.         for (int i=0; i < w*h; ++i) emb[i] = bg.getRGB();
  113.  
  114. // Pretty much a major hack here; assuming pixel[0] = the bg color!
  115. bg = new Color(pixels[0]);
  116.  
  117.         // Then fill in the outline of the image all white and
  118.         // offset by 1 to the right and down
  119.         for (int j = 0; j < h; j++) {
  120.             for (int i = 0; i < w; i++) {
  121.                 Color c = new Color(pixels[j * w + i]);
  122.                 if (!bg.equals(c)) {
  123.                     if (i < w-1 && j < h-1)
  124.                         emb[(j+1) * w + i + 1] = brighter.getRGB();
  125.                 }
  126.             }
  127.         }
  128.  
  129.         // Then cover the original image area with a darker version of
  130.         // the background color
  131.         for (int j = 0; j < h; j++) {
  132.             for (int i = 0; i < w; i++) {
  133.                 Color c = new Color(pixels[j * w + i]);
  134.                 if (!bg.equals(c))
  135.                     emb[j * w + i] = darker.getRGB();
  136.             }
  137.         }
  138.  
  139.         return comp.createImage(new MemoryImageSource(w, h, emb, 0, w));;
  140.     }
  141.  
  142.     public void disableImage() {
  143.         img = createDisabledImage(img, this);
  144.     }
  145.  
  146. /*
  147.     static boolean isTransparent(Color c) {
  148.         return ((c.getRGB() & 0xFF000000) >>> 24 == 255);
  149.     }
  150.  
  151.     static public Image createShadowedImage(Image im, Component comp) {
  152.         int w = im.getWidth(comp);
  153.         int h = im.getHeight(comp);
  154.         int wn = w+3, hn = h+3;
  155.         int pixels[] = new int[w * h];
  156.         int emb[] = new int[wn * hn];
  157.         PixelGrabber pg = new PixelGrabber(im, 0, 0, w, h, pixels, 0, w);
  158.         try {
  159.             pg.grabPixels();
  160.         } catch (InterruptedException e) {
  161.             return null;
  162.         }
  163.         if ((pg.status() & ImageObserver.ABORT) != 0) {
  164.             return null;
  165.         }
  166.  
  167. //        Color bg = comp.getBackground();
  168. //        if (bg == null) bg = Color.lightGray;
  169.         Color darker = Color.lightGray.darker();
  170.  
  171.         // Set the entire image to the bg color
  172.         for (int i=0; i < wn*hn; ++i) emb[i] = Color.lightGray.getRGB() & 0x00FFFFFF;
  173.  
  174.         // Then fill in the outline of the image darkened and
  175.         // offset by 3 to the right and down
  176.         for (int j = 0; j < h; j++) {
  177.             for (int i = 0; i < w; i++) {
  178. //                if (!bg.equals(new Color(pixels[j * w + i]))) {
  179.                 if (!isTransparent(pixels[j * w + i])) {
  180. //                    if (i < w-3 && j < h-3)
  181.                         emb[(j+3) * w + i + 3] = darker.getRGB();
  182.                 }
  183.             }
  184.         }
  185.  
  186.         // Then paste the orginal image on top
  187.         for (int j = 0; j < h; j++) {
  188.             for (int i = 0; i < w; i++) {
  189.                 if (!isTransparent(pixels[j * w + i]))
  190. //                if (!bg.equals(new Color(pixels[j * w + i])))
  191.                     emb[j * w + i] = pixels[j * w + i];
  192.             }
  193.         }
  194.  
  195.         return comp.createImage(new MemoryImageSource(wn, hn, emb, 0, wn));
  196.     }
  197.  
  198.     public void shadowImage() {
  199.         img = createShadowedImage(img, this);
  200.         resizeToImage();
  201.     }
  202. */
  203.  
  204.     public Image getImage() { return img; }
  205.  
  206.     /** Scale the image. (1.0 = 100%).
  207.      */
  208.     public void scale(double pct) {
  209.         int iw =  (int) (img.getWidth(this) * pct);
  210.         int ih =  (int) (img.getHeight(this) * pct);
  211.         
  212.         resize(iw, ih);
  213.     }
  214.  
  215.     /** Resize the Panel to the size of the current image.
  216.      */
  217.     public void resizeToImage() {
  218.         int iw =  img.getWidth(this);
  219.         int ih =  img.getHeight(this);
  220.  
  221.         resize(iw, ih);
  222.     }
  223.  
  224.     public void paint(Graphics g) {
  225.         int iw =  img.getWidth(this);
  226.         int ih =  img.getHeight(this);
  227.  
  228.         if (iw > 0 && ih > 0) {
  229.             // If we've never been sized set our size to that of the image
  230.             if (size().width <= 0 || size().height <= 0) resizeToImage();
  231.  
  232.             g.drawImage(img, 0, 0, size().width, size().height, this);
  233.         }
  234.     }
  235. }
  236.  
  237.