001: package com.jgoodies.animation.tutorial.component;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Graphics;
006: import java.awt.image.BufferedImage;
007: import java.awt.image.ConvolveOp;
008: import java.awt.image.Kernel;
009:
010: import javax.swing.Icon;
011: import javax.swing.ImageIcon;
012:
013: import com.jgoodies.binding.beans.Model;
014:
015: public final class BlurredIcon extends Model implements Icon {
016:
017: public static final String EAGER_INITIALIZATION = "eager";
018: public static final String LAZY_INITIALIZATION = "lazy";
019:
020: private static final int PAD = 0;
021:
022: // Parameters
023: private final ImageIcon icon;
024: private final long duration;
025: private final int numOfBuffers;
026:
027: private int index;
028: private long time;
029:
030: // Cached data
031: private boolean hasValidCache = false;
032:
033: private BufferedImage[] buffer = null;
034:
035: // Instance Creation ******************************************************
036:
037: public BlurredIcon(ImageIcon icon, long duration, int numOfBuffers) {
038: this (icon, duration, numOfBuffers, EAGER_INITIALIZATION);
039: }
040:
041: public BlurredIcon(ImageIcon icon, long duration, int numOfBuffers,
042: String initializationMode) {
043: this .icon = icon;
044: this .duration = duration;
045: this .numOfBuffers = numOfBuffers;
046: index = 0;
047: if (EAGER_INITIALIZATION.equals(initializationMode))
048: validateCache();
049: }
050:
051: // ************************************************************************
052:
053: private ConvolveOp createSimpleBlur() {
054: float f = 1.0f / 8.9f;
055: return new ConvolveOp(new Kernel(3, 3, new float[] { f, f, f,
056: f, f, f, f, f, f }), ConvolveOp.EDGE_NO_OP, null);
057: }
058:
059: private void ensureValidCache() {
060: if (hasValidCache)
061: return;
062:
063: validateCache();
064: }
065:
066: private int computeIndex(long time) {
067: float f = (float) time / duration;
068: return (int) ((buffer.length - 1) * f);
069: }
070:
071: private void initialize() {
072: int iconWidth = icon.getIconWidth();
073: int iconHeight = icon.getIconHeight();
074: int paddedWidth = iconWidth + 2 * PAD;
075: int paddedHeight = iconHeight + 2 * PAD;
076:
077: // Allocating the arrays.
078: buffer = new BufferedImage[numOfBuffers];
079:
080: ConvolveOp blur = createSimpleBlur();
081:
082: // Initialize the image.
083: for (int i = 0; i < numOfBuffers; i++) {
084: buffer[i] = /*0 == i ? icon.getImage() :*/
085: new BufferedImage(paddedWidth, paddedHeight,
086: BufferedImage.TYPE_INT_ARGB);
087: if (i == 0) {
088: Graphics g = buffer[i].getGraphics();
089: g.setColor(Color.WHITE);
090: g.fillRect(0, 0, paddedWidth, paddedHeight);
091: g.drawImage(icon.getImage(), PAD, PAD, null);
092: } else {
093: blur.filter(buffer[i - 1], buffer[i]);
094: }
095: }
096: }
097:
098: public void release() {
099: hasValidCache = false;
100: buffer = null;
101: //System.out.println("Releasing cache for text " + text);
102: }
103:
104: private void validateCache() {
105: initialize();
106: hasValidCache = true;
107: //System.out.println("Validating cache for text " + text);
108: }
109:
110: // Implementing the Icon Interface ****************************************
111:
112: public int getIconWidth() {
113: return icon.getIconWidth() + 2 * PAD;
114: }
115:
116: public int getIconHeight() {
117: return icon.getIconHeight() + 2 * PAD;
118: }
119:
120: public void paintIcon(Component c, Graphics g, int x, int y) {
121: ensureValidCache();
122:
123: //int x0 = (width - buffer[0].getWidth()) / 2;
124: //int y0 = (height - buffer[0].getHeight()) / 2;
125:
126: g.drawImage(buffer[getIndex()], x, y, null);
127: }
128:
129: // API ********************************************************************
130:
131: public int getIndex() {
132: return index;
133: }
134:
135: public long getTime() {
136: return time;
137: }
138:
139: public void setTime(long newTime) {
140: long oldTime = getTime();
141: time = newTime;
142: index = computeIndex(time);
143: firePropertyChange("time", oldTime, newTime);
144: }
145:
146: }
|