KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > components > GlyphLabel


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.animation.components;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Graphics2D JavaDoc;
36 import java.awt.RenderingHints JavaDoc;
37
38 import javax.swing.JComponent JavaDoc;
39
40 import com.jgoodies.animation.AnimationFunction;
41 import com.jgoodies.animation.AnimationFunctions;
42 import com.jgoodies.animation.renderer.GlyphRenderer;
43 import com.jgoodies.animation.renderer.HeightMode;
44
45 /**
46  * A Swing component that can transform a text's individual glyphs.
47  *
48  * @author Karsten Lentzsch
49  * @version $Revision: 1.3 $
50  */

51 public final class GlyphLabel extends JComponent JavaDoc {
52
53     private final GlyphRenderer renderer;
54
55     
56     // Instance Creation ******************************************************
57

58     /**
59      * Creates a <code>GlyphLabel</code> for the given text, duration and
60      * delay between the individual glyphs.
61      *
62      * @param text the initial text
63      * @param duration the duration of the whole animation
64      * @param glyphDelay a delay between the animation of the individual glyphs
65      */

66     public GlyphLabel(String JavaDoc text, long duration, long glyphDelay) {
67         this(text, duration, glyphDelay, Color.darkGray);
68     }
69
70     /**
71      * Creates a <code>GlyphLabel</code> for the given text, duration, base color
72      * and delay between the individual glyphs.
73      *
74      * @param text the initial text
75      * @param duration the duration of the whole animation
76      * @param glyphDelay a delay between the animation of the individual glyphs
77      * @param baseColor the color used as a basis for the translucent
78      * glyph foreground colors
79      */

80     public GlyphLabel(
81         String JavaDoc text,
82         long duration,
83         long glyphDelay,
84         Color JavaDoc baseColor) {
85         renderer =
86             new GlyphRenderer(
87                 text,
88                 defaultScaleFunction(duration),
89                 AnimationFunctions.ZERO,
90                 defaultColorFunction(duration, baseColor),
91                 glyphDelay);
92     }
93
94     /**
95      * Creates and returns the default scale function for the given duration.
96      *
97      * @param duration the duration of the whole animation
98      * @return an animation function that maps times to glyph scales
99      */

100     public static AnimationFunction defaultScaleFunction(long duration) {
101         return AnimationFunctions.linear(
102             duration,
103             new Float JavaDoc[] {
104                 new Float JavaDoc(5.0f),
105                 new Float JavaDoc(0.8f),
106                 new Float JavaDoc(1.0f),
107                 new Float JavaDoc(1.0f)},
108             new float[] { 0.0f, 0.1f, 0.12f, 1.0f });
109     }
110
111     /**
112      * Creates and returns the default color function for the given duration
113      * and base color.
114      *
115      * @param duration the duration of the animation
116      * @param baseColor the color used as a basis for the translucent colors
117      * @return an animation function that maps times to translucent glyph colors
118      */

119     public static AnimationFunction defaultColorFunction(
120         long duration,
121         Color JavaDoc baseColor) {
122         return AnimationFunctions.alphaColor(
123             AnimationFunctions.linear(
124                 duration,
125                 new Integer JavaDoc[] {
126                     new Integer JavaDoc(0),
127                     new Integer JavaDoc(255),
128                     new Integer JavaDoc(255)},
129                 new float[] { 0.0f, 0.15f, 1.0f }),
130             baseColor);
131     }
132     
133     
134     // Accessors **************************************************************
135

136     public HeightMode getHeightMode() {
137         return renderer.getHeightMode();
138     }
139     
140     public String JavaDoc getText() {
141         return renderer.getText();
142     }
143
144     public void setHeightMode(HeightMode heightMode) {
145         renderer.setHeightMode(heightMode);
146     }
147
148     public void setText(String JavaDoc newText) {
149         renderer.setText(newText);
150         repaint();
151     }
152
153     public void setTime(long time) {
154         renderer.setTime(time);
155         repaint();
156     }
157     
158     
159     // Rendering **************************************************************
160

161     /**
162      * Paints the component. Sets high-fidelity rendering hints,
163      * then invoke the renderer to render the glyphs.
164      *
165      * @param g the Graphics object to render on
166      */

167     public void paintComponent(Graphics JavaDoc g) {
168         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
169
170         g2.setRenderingHint(
171             RenderingHints.KEY_ANTIALIASING,
172             RenderingHints.VALUE_ANTIALIAS_ON);
173         g2.setRenderingHint(
174             RenderingHints.KEY_RENDERING,
175             RenderingHints.VALUE_RENDER_QUALITY);
176
177         renderer.setFont(getFont());
178         renderer.render(g2, getWidth(), getHeight());
179     }
180
181 }
Popular Tags