home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.4 KB | 237 lines |
-
- /*
- * ImagePanel.java
- *
- * Copyright (C) 1996 Shaun Terry. All Rights Reserved.
- */
-
- package spt.gui;
-
- import java.lang.String;
- import java.awt.Panel;
- import java.awt.Image;
- import java.awt.Graphics;
- import java.awt.MediaTracker;
- import java.awt.Component;
- import java.applet.Applet;
- import java.net.URL;
- import java.awt.Color;
- import java.awt.image.ImageObserver;
- import java.awt.image.PixelGrabber;
- import java.awt.image.MemoryImageSource;
- import java.io.File;
- import java.io.FileNotFoundException;
-
-
- /**
- * A Panel that displays an Image.
- *
- * @author Shaun Terry
- */
-
- public class ImagePanel extends Panel {
- Image img;
-
- /** Creates an ImagePanel with the image in the specified file
- * (no good for applets).
- */
- public ImagePanel(String file) throws FileNotFoundException {
- setLayout(null);
-
- img = loadImage(this, file);
-
- resizeToImage();
- }
-
- /** Creates an ImagePanel with the image in the specified URL
- * (applets only).
- */
- public ImagePanel(Applet a, URL file) {
- setLayout(null);
-
- img = loadImage(a, file);
-
- resizeToImage();
- }
-
- /** Load an image.
- */
- static public Image loadImage(Component comp, String file) throws FileNotFoundException {
- Image i;
-
- File f = new File(file);
- if (!f.exists()) throw new FileNotFoundException(file);
-
- MediaTracker t = new MediaTracker(comp);
- i = comp.getToolkit().getImage(file);
- t.addImage(i, 0);
- try {
- t.waitForAll();
- } catch (InterruptedException ee) {}
-
- return i;
- }
-
- /** Load an image.
- */
- static public Image loadImage(Applet a, URL file) {
- Image i;
-
- MediaTracker t = new MediaTracker(a);
- i = a.getImage(file);
- t.addImage(i, 0);
- try {
- t.waitForAll();
- } catch (InterruptedException ee) {}
-
- return i;
- }
-
- /** Return a "grayed-out" version of the given image.
- */
- static public Image createDisabledImage(Image im, Component comp) {
- int w = im.getWidth(comp);
- int h = im.getHeight(comp);
- int pixels[] = new int[w * h];
- int emb[] = new int[w * h];
- PixelGrabber pg = new PixelGrabber(im, 0, 0, w, h, pixels, 0, w);
- try {
- pg.grabPixels();
- } catch (InterruptedException e) {
- return null;
- }
- if ((pg.status() & ImageObserver.ABORT) != 0) {
- return null;
- }
-
- Color bg = comp.getBackground();
- Color darker = bg.darker();
- Color brighter = bg.brighter();
-
- // Set the entire image to the bg color
- for (int i=0; i < w*h; ++i) emb[i] = bg.getRGB();
-
- // Pretty much a major hack here; assuming pixel[0] = the bg color!
- bg = new Color(pixels[0]);
-
- // Then fill in the outline of the image all white and
- // offset by 1 to the right and down
- for (int j = 0; j < h; j++) {
- for (int i = 0; i < w; i++) {
- Color c = new Color(pixels[j * w + i]);
- if (!bg.equals(c)) {
- if (i < w-1 && j < h-1)
- emb[(j+1) * w + i + 1] = brighter.getRGB();
- }
- }
- }
-
- // Then cover the original image area with a darker version of
- // the background color
- for (int j = 0; j < h; j++) {
- for (int i = 0; i < w; i++) {
- Color c = new Color(pixels[j * w + i]);
- if (!bg.equals(c))
- emb[j * w + i] = darker.getRGB();
- }
- }
-
- return comp.createImage(new MemoryImageSource(w, h, emb, 0, w));;
- }
-
- public void disableImage() {
- img = createDisabledImage(img, this);
- }
-
- /*
- static boolean isTransparent(Color c) {
- return ((c.getRGB() & 0xFF000000) >>> 24 == 255);
- }
-
- static public Image createShadowedImage(Image im, Component comp) {
- int w = im.getWidth(comp);
- int h = im.getHeight(comp);
- int wn = w+3, hn = h+3;
- int pixels[] = new int[w * h];
- int emb[] = new int[wn * hn];
- PixelGrabber pg = new PixelGrabber(im, 0, 0, w, h, pixels, 0, w);
- try {
- pg.grabPixels();
- } catch (InterruptedException e) {
- return null;
- }
- if ((pg.status() & ImageObserver.ABORT) != 0) {
- return null;
- }
-
- // Color bg = comp.getBackground();
- // if (bg == null) bg = Color.lightGray;
- Color darker = Color.lightGray.darker();
-
- // Set the entire image to the bg color
- for (int i=0; i < wn*hn; ++i) emb[i] = Color.lightGray.getRGB() & 0x00FFFFFF;
-
- // Then fill in the outline of the image darkened and
- // offset by 3 to the right and down
- for (int j = 0; j < h; j++) {
- for (int i = 0; i < w; i++) {
- // if (!bg.equals(new Color(pixels[j * w + i]))) {
- if (!isTransparent(pixels[j * w + i])) {
- // if (i < w-3 && j < h-3)
- emb[(j+3) * w + i + 3] = darker.getRGB();
- }
- }
- }
-
- // Then paste the orginal image on top
- for (int j = 0; j < h; j++) {
- for (int i = 0; i < w; i++) {
- if (!isTransparent(pixels[j * w + i]))
- // if (!bg.equals(new Color(pixels[j * w + i])))
- emb[j * w + i] = pixels[j * w + i];
- }
- }
-
- return comp.createImage(new MemoryImageSource(wn, hn, emb, 0, wn));
- }
-
- public void shadowImage() {
- img = createShadowedImage(img, this);
- resizeToImage();
- }
- */
-
- public Image getImage() { return img; }
-
- /** Scale the image. (1.0 = 100%).
- */
- public void scale(double pct) {
- int iw = (int) (img.getWidth(this) * pct);
- int ih = (int) (img.getHeight(this) * pct);
-
- resize(iw, ih);
- }
-
- /** Resize the Panel to the size of the current image.
- */
- public void resizeToImage() {
- int iw = img.getWidth(this);
- int ih = img.getHeight(this);
-
- resize(iw, ih);
- }
-
- public void paint(Graphics g) {
- int iw = img.getWidth(this);
- int ih = img.getHeight(this);
-
- if (iw > 0 && ih > 0) {
- // If we've never been sized set our size to that of the image
- if (size().width <= 0 || size().height <= 0) resizeToImage();
-
- g.drawImage(img, 0, 0, size().width, size().height, this);
- }
- }
- }
-
-