home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.output;
-
- import sub_arctic.lib.manager;
-
- import java.awt.Graphics;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Color;
- import java.awt.Rectangle;
- import java.awt.Polygon;
- import java.awt.Image;
- import java.awt.image.ImageObserver;
- import java.awt.image.ImageFilter;
- import java.awt.image.RGBImageFilter;
- import java.awt.image.ImageProducer;
- import java.awt.image.FilteredImageSource;
-
- /**
- * A drawable object that draws everything with increased transparency. Well,
- * almost everything -- AWT does not support transparent text so text is
- * always drawn at full opacity.
- *
- * @see java.awt.image.RGBImageFilter
- * @author Scott Hudson
- */
- public class transparent_drawable extends drawable {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Max transparency that we paint everything with. 225 is opaque, 0 is
- * fully transparent.
- */
- protected int _alpha_value = 127;
-
- /** Copy of _alpha_value pre-shifted to apply directly as alpha value in
- * RGB-alpha encoding.
- */
- protected int alpha_mask = (127) << 24;
-
- /** Max transparency that we paint everything with. 225 is opaque, 0 is
- * fully transparent.
- */
- public int alpha_factor() {return _alpha_value;}
-
- /** Set max transparency that we paint everything with. 225 is opaque, 0 is
- * fully transparent.
- */
- public void set_alpha_value(int a)
- {
- if (a < 0) a = 0;
- if (a > 255) a = 255;
- _alpha_value = a;
- alpha_mask = a<<24;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Filter to increase transparency of images */
- protected ImageFilter trans_filter;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Apply transparency value to a color. The result has the min of the
- * alpha already in the color and ours.
- *
- * @param Color c the color to be transformed.
- * @return Color the resulting color.
- */
- protected Color apply_alpha(Color c)
- {
- int encoding, alpha;
-
- /* Get the alpha-RGB encoding and strip out the alpha */
- encoding = c.getRGB();
- alpha = (encoding >> 24)& 0xff;
-
- /* use min of our alpha and theirs */
- if (alpha < _alpha_value)
- return c;
- else
- return new Color((encoding & 0x00ffffff) | alpha_mask);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Full constructor.
- * @param Graphics wrappee the Graphics object we wrap.
- * @param int alph the minimum alpha value (0 = fully transparent,
- * 255 = fully opaque) that this drawable draws in.
- */
- public transparent_drawable(
- Graphics wrappee,
- int alph)
- {
- /* let super class initialize based on a copy */
- super(wrappee.create());
-
- /* save our transparency factor */
- set_alpha_value(alph);
-
- /* apply transparency to the current color */
- g.setColor(apply_alpha(g.getColor()));
-
- /* construct a filter for our transparency */
- trans_filter = new transp_filter(_alpha_value);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Constructor that defaults to alpha value of 50%
- * @param Graphics wrappee the Graphics object we wrap.
- */
- public transparent_drawable(Graphics wrappee)
- {
- this(wrappee, 127);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Full constructor from a drawable.
- * @param drawable wrappee the drawable object we wrap.
- * @param int alph the minimum alpha value (0 = fully transparent,
- * 255 = fully opaque) that this drawable draws in.
- */
- public transparent_drawable(
- drawable wrappee,
- int alph)
- {
- /* let super class initialize based on a copy */
- super(wrappee.g.create());
-
- /* save our transparency factor */
- set_alpha_value(alph);
-
- /* apply transparency to the current color */
- g.setColor(apply_alpha(g.getColor()));
-
- /* construct a filter for our transparency */
- trans_filter = new transp_filter(_alpha_value);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Constructor from drawable that defaults to alpha value of 50%
- *
- * @param drawable wrappee the drawable object we wrap.
- */
- public transparent_drawable(drawable wrappee)
- {
- this(wrappee, 127);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override copy() to create a new wrapper also.
- * @return drawable the new transparent_drawable copied from the old.
- */
- public drawable copy()
- {
- /* create new wrapper */
- return new transparent_drawable(g.create(), _alpha_value);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override copy() to create a new wrapper also.
- *
- * @param int x x origin for new drawable.
- * @param int y y origin for new drawable.
- * @param int w width of new drawable.
- * @param int h height of new drawable.
- * @return drawable the new transparent_drawable object based on the old.
- */
- public drawable copy(int x, int y, int w, int h)
- {
- /* create new wrapper */
- return new transparent_drawable(g.create(x,y,w,h), _alpha_value);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override create() to create a new wrapper also
- * @return Graphics the new transparent_drawable copied from the old.
- */
- public Graphics create()
- {
- /* create new wrapper */
- return new transparent_drawable(g.create(), _alpha_value);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override create() to create a new wrapper also.
- *
- * @param int x x origin for new drawable.
- * @param int y y origin for new drawable.
- * @param int w width of new drawable.
- * @param int h height of new drawable.
- * @return Graphics the new transparent_drawable object based on the old.
- */
- public Graphics create(int x, int y, int w, int h)
- {
- /* create new wrapper */
- return new transparent_drawable(g.create(x,y,w,h), _alpha_value);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override to set all colors to one with modified transparency.
- * @param Color c base color we are setting current color to.
- */
- public void setColor(Color c) {g.setColor(apply_alpha(c));}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- // note we may not be getting copyArea right, look into that later...
- // not clear what transparent copyArea should do
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Filter the given image to add our transparency to it.
- * @param Image from_img the original image.
- * @result Image the resulting image.
- */
- protected Image make_trans(Image from_img)
- {
- ImageProducer trans_prod;
- Image result;
-
- trans_prod = new FilteredImageSource(from_img.getSource(), trans_filter);
- result = manager.default_toolkit().createImage(trans_prod);
- manager.wait_for_image(result);
- return result;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override to filter images, modifying transparency.
- *
- * @param Image img the image to be drawn.
- * @param int x x position to place the image at.
- * @param int y y position to place the image at.
- * @param ImageObserver observer observer to notify us of progress drawing
- * the Image
- * @return boolean undocumented AWT drawImage return value.
- */
- public boolean drawImage(Image img, int x, int y, ImageObserver observer)
- {
- return g.drawImage(make_trans(img),x,y,observer);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override to filter images, modifying transparency.
- *
- * @param Image img the image to be drawn.
- * @param int x x position to place the image at.
- * @param int y y position to place the image at.
- * @param int w width that image will be scaled to.
- * @param int h height that image will be scaled to.
- * @param ImageObserver observer observer to notify us of progress drawing
- * the Image
- * @return boolean undocumented AWT drawImage return value.
- */
- public boolean drawImage(Image img, int x, int y, int w, int h,
- ImageObserver obs)
- {
- return g.drawImage(make_trans(img),x,y,w,h,obs);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override to filter images, modifying transparency.
- *
- * @param Image img the image to be drawn.
- * @param int x x position to place the image at.
- * @param int y y position to place the image at.
- * @param Color bgcolor background color.
- * @param ImageObserver observer observer to notify us of progress drawing
- * the Image
- * @return boolean undocumented AWT drawImage return value.
- */
- public boolean drawImage(Image img, int x, int y, Color bgcolor,
- ImageObserver obs)
- {
- return g.drawImage(make_trans(img),x,y,bgcolor,obs);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override to filter images, modifying transparency.
- *
- * @param Image img the image to be drawn.
- * @param int x x position to place the image at.
- * @param int y y position to place the image at.
- * @param int w width that image will be scaled to.
- * @param int h height that image will be scaled to.
- * @param Color bgcolor background color.
- * @param ImageObserver observer observer to notify us of progress drawing
- * the Image
- * @return boolean undocumented AWT drawImage return value.
- */
- public boolean drawImage(Image img, int x, int y, int w, int h, Color bgcolor,
- ImageObserver obs)
- {
- return g.drawImage(make_trans(img),x,y,w,h,bgcolor,obs);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Override tiled image drawing, modifying transparency.
- * @param loaded_image pattern the pattern we tile with.
- * @param int x x position to place the tiling at.
- * @param int y y position to place the tiling at.
- * @param int w width of tiled area.
- * @param int h height of tiled area.
- * @return boolean undocumented AWT drawImage return value from the last draw.
- */
- public boolean tileImage(loaded_image pattern, int x, int y, int w, int h)
- {
- Image img = pattern.image();
- int size_x = pattern.width();
- int size_y = pattern.height();
- int xpt, ypt;
- Graphics clipped_g;
- boolean v = true;
-
- /* Build a transparency modified copy of the pattern */
- Image trans_pat = make_trans(pattern.image());
-
- /* Build a copy of our Graphics object that clips to exactly the size
- * we are clipping into
- */
- clipped_g = g.create();
- clipped_g.clipRect(x,y,w,h);
-
- /* loop over image area drawing tiles until we have filled it */
- for (ypt = y; ypt-y < h; ypt += size_y)
- for (xpt = x; xpt-x < w; xpt += size_x)
- v = clipped_g.drawImage(trans_pat, xpt, ypt, _ignore);
-
- /* return the last return value we got */
- return v;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
-
- /*---------------------------------------------------------------------*/
-
- /**
- * An image filter class to modify transparency of each pixel to be the min
- * of the original value and a given value.
- *
- * @see sub_arctic.output.transparent_drawable
- * @see java.awt.image.RGBImageFilter
- * @author Scott Hudson
- */
- class transp_filter extends RGBImageFilter {
-
- /** The transparency value we use. 255 is opaque, 0 is fully transparent. */
- protected int alpha_value;
-
- /** trasnp_factor pre-shifted for use directly in RBG-alpha encoding. */
- protected int alpha_mask;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Constructor.
- * @param int alpha minimum alpha (transparency) value (0 = fully transparent,
- * 255 = fully opaque).
- */
- public transp_filter(int alph)
- {
- /* flag for the superclass */
- canFilterIndexColorModel = true;
-
- alpha_value = alph;
- alpha_mask = alph << 24;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The actual filter that processes each pixel.
- * @param int x the x position of the pixel (we ignore this).
- * @param int y the y position of the pixel (we ignore this).
- * @param int rgb the RGB+Alpha encoding of the pixel's color.
- * @return int the resulting color's encoded value.
- */
- public int filterRGB(int x, int y, int rgb)
- {
- int alpha;
-
- /* strip out the alpha */
- alpha = (rgb >> 24) & 0xff;
-
- /* use min of our alpha and theirs */
- if (alpha < alpha_value)
- return rgb;
- else
- return (rgb & 0x00ffffff) | alpha_mask;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-