001: package com.xoetrope.swing.animation;
002:
003: import java.awt.Color;
004: import java.awt.Graphics2D;
005: import org.jdesktop.animation.timing.Animator;
006:
007: /**
008: * A colour fade-in
009: *
010: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
011: * the GNU Public License (GPL), please see license.txt for more details. If
012: * you make commercial use of this software you must purchase a commercial
013: * license from Xoetrope.</p>
014: * <p> $Revision: 1.8 $</p>
015: */
016: public class FadeInStep implements AnimationStep {
017: private float increment = 0.02f;
018: private float value;
019: private float hsbStart[];
020: private float hsbFinal[];
021: private Color actualColor, startColor, endColor;
022: private int ascent;
023: private boolean finished;
024:
025: /** Creates a new instance of FadeInStep */
026: public FadeInStep() {
027: init();
028: }
029:
030: /**
031: * Set the initial state
032: */
033: public void init() {
034: finished = false;
035: value = 0.0F;
036:
037: reset();
038: }
039:
040: /**
041: * Is the animation loop finished
042: *
043: * @param at the animation thread on which this object is running
044: * @return true if the animation has completed
045: */
046: public boolean isFinished(Animator at) {
047: return finished;
048: }
049:
050: /**
051: * Is the animation loop still active/running
052: *
053: * @param at the animation thread on which this object is running
054: * @return true if the animation has started and is still in progress
055: */
056: public boolean isAnimated(Animator at) {
057: return !finished;
058: }
059:
060: /**
061: * Set the color used at the start of the fade-in, normally this is the background color
062: * @param sc the start/initial color
063: */
064: public void setStartColor(Color sc) {
065: startColor = sc;
066: }
067:
068: /**
069: * Set the color used once the fade in has completed
070: * @param ec the end color
071: */
072: public void setEndColor(Color ec) {
073: endColor = ec;
074: }
075:
076: /**
077: * Advance to the next step in the animation
078: *
079: * @param g2 the graphics context
080: * @param at the animation thread on which this object is running
081: */
082: public void apply(Animator at, Graphics2D g2) {
083: actualColor = Color.getHSBColor(hsbStart[0], hsbStart[1],
084: hsbStart[2]);
085: Color frgdColor = new Color(actualColor.getRed(), actualColor
086: .getGreen(), actualColor.getBlue(), Math.min(255,
087: (int) (value * 255)));
088: g2.setColor(frgdColor);
089: //g2.setBackground( startColor );
090: }
091:
092: /**
093: * Reset the animation to its initial state
094: */
095: public void reset() {
096: float rgb[] = new float[3];
097: if (startColor == null)
098: startColor = Color.white;
099:
100: rgb = startColor.getRGBColorComponents(rgb);
101: hsbStart = Color.RGBtoHSB((int) (255 * rgb[0]),
102: (int) (255 * rgb[1]), (int) (255 * rgb[2]), null);
103:
104: if (endColor == null)
105: endColor = Color.black;
106:
107: float rgb2[] = endColor.getRGBColorComponents(rgb);
108: hsbFinal = Color.RGBtoHSB((int) (255 * rgb2[0]),
109: (int) (255 * rgb2[1]), (int) (255 * rgb2[2]), null);
110:
111: actualColor = Color.getHSBColor(hsbStart[0], hsbStart[1],
112: hsbStart[2]);
113: finished = false;
114: }
115:
116: /**
117: * Move to the next step in the animation
118: * @param at the animation thread
119: */
120: public void step(Animator at) {
121: if (actualColor == null)
122: reset();
123: else if (value >= 1.0F) {
124: finished = true;
125: return;
126: }
127:
128: value += increment;
129: hsbStart[0] += value * (hsbFinal[0] - hsbStart[0]);
130: hsbStart[1] += value * (hsbFinal[1] - hsbStart[1]);
131: hsbStart[2] += value * (hsbFinal[2] - hsbStart[2]);
132: }
133: }
|