View Javadoc

1   package com.melloware.jukes.file.image;
2   
3   import java.awt.AlphaComposite;
4   import java.awt.Graphics2D;
5   import java.awt.Image;
6   import java.awt.image.BufferedImage;
7   import java.awt.image.ImageObserver;
8   
9   import javax.swing.Icon;
10  import javax.swing.ImageIcon;
11  
12  /**
13   * Utility to blend to images together.  It overlays one image over another.
14   * <p>
15   * Copyright (c) 2006
16   * Melloware, Inc. <http://www.melloware.com>
17   * @author Emil A. Lefkof III <info@melloware.com>
18   * @version 4.0
19   */
20  public final class ImageBlender {
21  
22      // Graphic Display constants
23      public static final float BLEND_OPAQUE = 1.0f;
24      public static final float BLEND_TRANSPARENT = 0.5f;
25  
26      /**
27       * Default constructor. Private so no instantiation.
28       */
29      private ImageBlender() {
30          super();
31      }
32  
33      public static Icon blendIcons(final Icon source,
34                                    final Icon destination,
35                                    final float transparency,
36                                    final ImageObserver imageObserver) {
37          final ImageIcon sourceIcon = (ImageIcon)source;
38          final ImageIcon destinationIcon = (ImageIcon)destination;
39          final Image sourceImage = sourceIcon.getImage();
40          final Image destinationImage = destinationIcon.getImage();
41  
42          final BufferedImage dest = new BufferedImage(destinationIcon.getIconWidth(), destinationIcon.getIconHeight(),
43                                                       BufferedImage.TYPE_INT_ARGB);
44  
45          final Graphics2D destG = dest.createGraphics();
46          destG.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
47          destG.drawImage(destinationImage, 0, 0, imageObserver);
48          destG.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
49          destG.drawImage(sourceImage, 0, 0, imageObserver);
50          return new ImageIcon(dest);
51      }
52  
53  }