Dissolver.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.FilteredImageSource;
import javax.swing.*;

/**
 * Initially displays an image.  Subsequent mouse clicks in the 
 * canvas containing the image cause the image to fade in or 
 * fade out, depending upon it's current state.

* * @version 1.0, Apr 1 1996 * @author David Geary * @see gjt.test.UnitTest * @see gjt.image.DissolveFilter * @see gjt.image.ImageDissolver */ public class Dissolver { Dissolver() { JFrame frame = new JFrame("Image FadeIn/FadeOut"); // use Swing to get the image ImageIcon icon = new ImageIcon("saint.gif"); Image im = icon.getImage(); JPanel panel = new DrawPanel(im); Container c = frame.getContentPane(); c.add( new JLabel("Click image to Fade Picture In/Out",JLabel.CENTER), BorderLayout.NORTH); c.add(panel,BorderLayout.CENTER); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } class DrawPanel extends JPanel { boolean isFaded = false; Image image; ImageDissolver dissolver; DrawPanel(Image im) { image = im; dissolver = new ImageDissolver(this, image); addMouseListener(new myMouse() ); } public void doFade() { if(isFaded) dissolver.fadeIn (0,0); else dissolver.fadeOut(0,0); isFaded = isFaded ? false : true; } public class myMouse extends MouseAdapter { public void mouseClicked(MouseEvent e) { doFade(); } } public void paintComponent(Graphics g) { //super.paintComponent(g); if( ! isFaded) g.drawImage(image, 0, 0, this); } public Dimension getPreferredSize() { return new Dimension(image.getWidth(this), image.getHeight(this)); } } static void main(String args[]) { new Dissolver(); } }

ImageDissolver.java

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

/**
 * Given an image, an ImageDissolver produces an array of
 * images of varying opacity that are used in the fadeIn()
 * and fadeOut() methods for fading the image in and out
 * respectively.

* * As a convenience, ImageDissolver has a static method: * Image[] createImages() that creates the array of images * mentioned above, in case clients would like to create their * own array of images instead of using an ImageDissolver * directly.

* * @version 1.0, Apr 1 1996 * @author David Geary * @see ThreeDBorder * @see ImageButtonController * @see SpringyImageButtonController * @see StickyImageButtonController * @see BleachImageFilter * @see gjt.test.ImageButtonTest */ public class ImageDissolver { private static int _defaultNumImages = 10, _defaultPause = 50; JComponent comp; int numImages, pauseInterval; Image image, offscreen; Image[] dissolvedImages; static public Image[] createImages(Image image, int numImages, JComponent component) { Image images[] = new Image[numImages]; MediaTracker tracker = new MediaTracker(component); DissolveFilter filter; FilteredImageSource fis; for(int i=0; i < numImages; ++i) { filter = new DissolveFilter((255/(numImages-1))*i); fis = new FilteredImageSource(image.getSource(), filter); images[i] = component.createImage(fis); tracker.addImage(images[i], i); } try { tracker.waitForAll(); } catch(InterruptedException e) { } return images; } public static void waitForImage(JComponent component, Image im) { MediaTracker tracker = new MediaTracker(component); try { tracker.addImage(im, 0); tracker.waitForID(0); } catch(InterruptedException e) { System.out.println(e.toString()); } } public ImageDissolver(JComponent comp, Image image) { this(comp, image, _defaultNumImages, _defaultPause); } public ImageDissolver(JComponent comp, Image im, int numImages, int pause) { this.image = im; this.comp = comp; this.numImages = numImages; dissolvedImages = new Image[numImages]; pauseInterval = pause; waitForImage(comp, im); dissolvedImages = createImages(image, numImages, comp); } public void fadeIn(int x, int y) { if(offscreen == null) offscreen = comp.createImage(image.getWidth(comp), image.getHeight(comp)); Graphics offg = offscreen.getGraphics(); Graphics compg = comp.getGraphics(); if(offg != null && compg != null) { clearComponent(compg, x, y); for(int i=0; i < numImages; ++i) { blitImage(compg, offg, x, y, i); pause (); } blitOpaqueImage(compg, offg, x, y); } } public void fadeOut(int x, int y) { if(offscreen == null) offscreen = comp.createImage(image.getWidth(comp), image.getHeight(comp)); Graphics offg = offscreen.getGraphics(); Graphics compg = comp.getGraphics(); if(offg != null && compg != null) { blitOpaqueImage(compg, offg, x, y); for(int i=numImages-1; i >= 0; --i) { clearOffscreen(); blitImage (compg, offg, x, y, i); pause (); } } } private void blitImage(Graphics compg, Graphics offg, int x, int y, int index) { offg.drawImage (dissolvedImages[index], 0, 0, comp); compg.drawImage(offscreen, x, y, comp); } private void blitOpaqueImage(Graphics compg, Graphics offg, int x, int y) { offg.drawImage(image, 0, 0, comp); compg.drawImage(offscreen, x, y, comp); } private void clearComponent(Graphics compg, int x, int y) { clearOffscreen(); compg.drawImage(offscreen, x, y, comp); } private void clearOffscreen() { Graphics offg = offscreen.getGraphics(); offg.setColor(comp.getBackground()); offg.fillRect(0, 0, image.getWidth(comp), image.getHeight(comp)); } private void pause() { try { Thread.currentThread().sleep(pauseInterval); } catch(InterruptedException e) { } } }

DissolveFilter

import java.awt.image.*;

/**
 * A derivation of RGBImageFilter that partially or wholly
 * dissolves an image.

* * Extent of dissolving is set by the setOpacity(int) method, * which is passed an integer between 0 and 255 (inclusive). * The integer represents the alpha value to be applied to * every color in the image.

* * An alpha value of 255 signifies an opaque color, while an * alpha value of 0 signifies a translucent color.

* * @version 1.0, Apr 1 1996 * @author David Geary * @see RGBImageFilter */ public class DissolveFilter extends RGBImageFilter { private int opacity; public DissolveFilter() { this(0); } public DissolveFilter(int opacity) { canFilterIndexColorModel = true; setOpacity(opacity); } public void setOpacity(int opacity) { this.opacity = opacity; } public int filterRGB(int x, int y, int rgb) { DirectColorModel cm = (DirectColorModel)ColorModel.getRGBdefault(); int alpha = cm.getAlpha(rgb); int red = cm.getRed (rgb); int green = cm.getGreen(rgb); int blue = cm.getBlue (rgb); alpha = opacity; return alpha << 24 | red << 16 | green << 8 | blue; } }


Maintained by John Loomis, last updated 15 June 2000