001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.utils.icon;
031:
032: import java.awt.*;
033: import java.awt.image.BufferedImage;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import javax.swing.Icon;
038: import javax.swing.ImageIcon;
039:
040: import org.jvnet.lafwidget.animation.FadeKind;
041: import org.jvnet.lafwidget.animation.FadeTracker;
042: import org.jvnet.substance.utils.SubstanceColorUtilities;
043: import org.jvnet.substance.utils.SubstanceCoreUtilities;
044:
045: public class GlowingIcon implements Icon {
046:
047: protected Icon delegate;
048:
049: protected Component component;
050:
051: protected Map<Float, Icon> iconMap;
052:
053: public GlowingIcon(Icon delegate, Component component) {
054: this .delegate = delegate;
055: this .component = component;
056: this .iconMap = new HashMap<Float, Icon>();
057: }
058:
059: public Icon getDelegate() {
060: return this .delegate;
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see javax.swing.Icon#getIconHeight()
067: */
068: public int getIconHeight() {
069: if (this .delegate == null)
070: return 0;
071: return this .delegate.getIconHeight();
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see javax.swing.Icon#getIconWidth()
078: */
079: public int getIconWidth() {
080: if (this .delegate == null)
081: return 0;
082: return this .delegate.getIconWidth();
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics,
089: * int, int)
090: */
091: /*
092: * (non-Javadoc)
093: *
094: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics,
095: * int, int)
096: */
097: public synchronized void paintIcon(Component c, Graphics g, int x,
098: int y) {
099: if (this .delegate == null)
100: return;
101: float fadePos = FadeTracker.getInstance().getFade10(
102: this .component, FadeKind.ICON_GLOW);
103: // System.out.println(fadePos);
104: Icon toPaint = this .iconMap.get(fadePos);
105: if (toPaint == null) {
106: int width = this .getIconWidth();
107: int height = this .getIconHeight();
108: BufferedImage image = SubstanceCoreUtilities.getBlankImage(
109: width, height);
110: Graphics2D graphics = (Graphics2D) image.getGraphics();
111: this .delegate.paintIcon(c, graphics, 0, 0);
112: for (int i = 0; i < width; i++) {
113: for (int j = 0; j < height; j++) {
114: int rgba = image.getRGB(i, j);
115: int transp = (rgba >>> 24) & 0xFF;
116: double coef = Math.sin(2.0 * Math.PI * fadePos
117: / 20.0) / 3.0;
118: Color newColor = (coef >= 0.0) ? SubstanceColorUtilities
119: .getLighterColor(new Color(rgba), coef)
120: : SubstanceColorUtilities.getDarkerColor(
121: new Color(rgba), -coef);
122: image.setRGB(i, j, (transp << 24)
123: | (newColor.getRed() << 16)
124: | (newColor.getGreen() << 8)
125: | newColor.getBlue());
126: }
127: }
128: toPaint = new ImageIcon(image);
129: this.iconMap.put(fadePos, toPaint);
130: }
131: toPaint.paintIcon(c, g, x, y);
132: }
133: }
|