/* Copyright(C) 1998 Tom Whittaker. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License in file NOTICE for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.awt.*; import java.applet.*; import java.util.*; import java.lang.*; import java.awt.image.*; /** create an image with designated level transparent * * @author Tom Whittaker, SSEC * */ public class TransparentImage extends RGBImageFilter { Image imgShow,ti; private FilteredImageSource fis; private int transparent; private int tRedBottom, tRedTop; private int tGreenBottom, tGreenTop; private int tBlueBottom, tBlueTop; private int transAmount; private boolean single; int count = 0; /** create transparent image * * @param parent is the applet invoking this * @param source is the source image * @param ta is percent of opacity (0=transparent, 100=opaque) * @param trans is the color trans (rgb) to use as "transparent" * */ public TransparentImage(Component parent,Image source, int ta, int trans) { single = true; setUpImage(parent, source, ta, trans); } private void setUpImage(Component parent, Image source, int ta, int trans) { canFilterIndexColorModel = true; transparent = trans; if (ta < 0) ta = 0; if (ta > 100) ta = 100; transAmount = (((255*ta)/100 & 0xff) << 24); // convert 0-255 fis = new FilteredImageSource(source.getSource(), this) ; ti = parent.createImage(fis); MediaTracker track = new MediaTracker(parent); track.addImage(ti,0); try {track.waitForID(0); } catch (Exception e) {;} } public TransparentImage(Applet parent,Image source, int ta, int transBottom, int transTop) { // if bottom == top, use more efficient mode... if (! (transBottom == transTop) ) { // otherwise, use a range for each component single = false; tRedBottom = (transBottom & 0x00ff0000); tRedTop = (transTop & 0x00ff0000); tGreenBottom = (transBottom & 0x0000ff00); tGreenTop = (transTop & 0x0000ff00); tBlueBottom = (transBottom & 0x000000ff); tBlueTop = (transTop & 0x000000ff); } else { single = true; } setUpImage(parent, source, ta, transBottom); } /** return the transparent image * * @return imgShow is the Image * */ public Image getImage() { return ti; } public int filterRGB(int x, int y, int rgb) { if (single) { if ( (rgb & 0x00ffffff) == transparent) return (rgb & 0x00ffffff); } else { if ( (rgb & 0x00ff0000) >= tRedBottom && (rgb & 0x00ff0000) <= tRedTop && (rgb & 0x0000ff00) >= tGreenBottom && (rgb & 0x0000ff00) <= tGreenTop && (rgb & 0x000000ff) >= tBlueBottom && (rgb & 0x000000ff) <= tBlueTop) return (rgb & 0x00ffffff) ; } return ((rgb & 0x00ffffff) | transAmount); } }