001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. 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 JGoodies Karsten Lentzsch 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:
031: package com.jgoodies.animation.components;
032:
033: import java.awt.Color;
034: import java.awt.Graphics;
035: import java.awt.Graphics2D;
036: import java.awt.RenderingHints;
037:
038: import javax.swing.JComponent;
039:
040: import com.jgoodies.animation.AnimationFunction;
041: import com.jgoodies.animation.AnimationFunctions;
042: import com.jgoodies.animation.renderer.GlyphRenderer;
043: import com.jgoodies.animation.renderer.HeightMode;
044:
045: /**
046: * A Swing component that can transform a text's individual glyphs.
047: *
048: * @author Karsten Lentzsch
049: * @version $Revision: 1.1 $
050: */
051: public final class GlyphLabel extends JComponent {
052:
053: // Names of the bound bean properties *************************************
054:
055: public static final String PROPERTYNAME_HEIGHT_MODE = "heightMode";
056: public static final String PROPERTYNAME_TEXT = "text";
057: public static final String PROPERTYNAME_TIME = "time";
058:
059: /**
060: * Refers to the renderer that paints the individual glyphs.
061: */
062: private final GlyphRenderer renderer;
063:
064: // Instance Creation ******************************************************
065:
066: /**
067: * Creates a <code>GlyphLabel</code> for the given text, duration and
068: * delay between the individual glyphs.
069: *
070: * @param text the initial text
071: * @param duration the duration of the whole animation
072: * @param glyphDelay a delay between the animation of the individual glyphs
073: */
074: public GlyphLabel(String text, long duration, long glyphDelay) {
075: this (text, duration, glyphDelay, Color.DARK_GRAY);
076: }
077:
078: /**
079: * Creates a <code>GlyphLabel</code> for the given text, duration, base color
080: * and delay between the individual glyphs.
081: *
082: * @param text the initial text
083: * @param duration the duration of the whole animation
084: * @param glyphDelay a delay between the animation of the individual glyphs
085: * @param baseColor the color used as a basis for the translucent
086: * glyph foreground colors
087: */
088: public GlyphLabel(String text, long duration, long glyphDelay,
089: Color baseColor) {
090: renderer = new GlyphRenderer(text,
091: defaultScaleFunction(duration),
092: AnimationFunctions.ZERO, defaultColorFunction(duration,
093: baseColor), glyphDelay);
094: }
095:
096: /**
097: * Creates and returns the default scale function for the given duration.
098: *
099: * @param duration the duration of the whole animation
100: * @return an animation function that maps times to glyph scales
101: */
102: public static AnimationFunction defaultScaleFunction(long duration) {
103: return AnimationFunctions.linear(duration, new Float[] {
104: new Float(5.0f), new Float(0.8f), new Float(1.0f),
105: new Float(1.0f) }, new float[] { 0.0f, 0.1f, 0.12f,
106: 1.0f });
107: }
108:
109: /**
110: * Creates and returns the default color function for the given duration
111: * and base color.
112: *
113: * @param duration the duration of the animation
114: * @param baseColor the color used as a basis for the translucent colors
115: * @return an animation function that maps times to translucent glyph colors
116: */
117: public static AnimationFunction defaultColorFunction(long duration,
118: Color baseColor) {
119: return AnimationFunctions.alphaColor(AnimationFunctions.linear(
120: duration, new Integer[] { new Integer(0),
121: new Integer(255), new Integer(255) },
122: new float[] { 0.0f, 0.15f, 1.0f }), baseColor);
123: }
124:
125: // Accessors **************************************************************
126:
127: public HeightMode getHeightMode() {
128: return renderer.getHeightMode();
129: }
130:
131: public String getText() {
132: return renderer.getText();
133: }
134:
135: public long getTime() {
136: return renderer.getTime();
137: }
138:
139: public void setHeightMode(HeightMode newHeightMode) {
140: HeightMode oldHeightMode = getHeightMode();
141: renderer.setHeightMode(newHeightMode);
142: firePropertyChange(PROPERTYNAME_HEIGHT_MODE, oldHeightMode,
143: newHeightMode);
144: repaint();
145: }
146:
147: public void setText(String newText) {
148: String oldText = getText();
149: renderer.setText(newText);
150: firePropertyChange(PROPERTYNAME_TEXT, oldText, newText);
151: repaint();
152: }
153:
154: public void setTime(long newTime) {
155: long oldTime = getTime();
156: renderer.setTime(newTime);
157: firePropertyChange(PROPERTYNAME_TIME, oldTime, newTime);
158: repaint();
159: }
160:
161: // Rendering **************************************************************
162:
163: /**
164: * Paints the component. Sets high-fidelity rendering hints,
165: * then invoke the renderer to render the glyphs.
166: *
167: * @param g the Graphics object to render on
168: */
169: public void paintComponent(Graphics g) {
170: Graphics2D g2 = (Graphics2D) g;
171:
172: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
173: RenderingHints.VALUE_ANTIALIAS_ON);
174: g2.setRenderingHint(RenderingHints.KEY_RENDERING,
175: RenderingHints.VALUE_RENDER_QUALITY);
176:
177: renderer.setFont(getFont());
178: renderer.render(g2, getWidth(), getHeight());
179: }
180:
181: }
|