KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > animations > BasicTextAnimation


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.animations;
32
33 import java.awt.Color JavaDoc;
34
35 import com.jgoodies.animation.AbstractAnimation;
36 import com.jgoodies.animation.AnimationFunction;
37 import com.jgoodies.animation.AnimationFunctions;
38 import com.jgoodies.animation.components.BasicTextLabel;
39
40 /**
41  * A basic text animation that fades in a text, changes the x and y scaling,
42  * the position, and the space between glyphs.
43  *
44  * @author Karsten Lentzsch
45  * @version $Revision: 1.4 $
46  *
47  * @see com.jgoodies.animation.Animation
48  * @see BasicTextLabel
49  * @see AnimationFunction
50  * @see com.jgoodies.animation.AnimationFunctions
51  */

52 public final class BasicTextAnimation extends AbstractAnimation {
53
54     private final BasicTextLabel label;
55     private final String JavaDoc text;
56     private final AnimationFunction colorFunction;
57     private final AnimationFunctions.FloatFunction offsetFunction;
58     private final AnimationFunctions.FloatFunction scaleXFunction;
59     private final AnimationFunctions.FloatFunction scaleYFunction;
60     private final AnimationFunctions.FloatFunction spaceFunction;
61
62     private boolean offsetEnabled = false;
63     
64     
65     // Instance Creation *****************************************************
66

67     /**
68      * Constructs a text animation, that fades in a text, scales it and
69      * fades it out. Uses the given color and scaling functions.
70      *
71      * @param label the animation target component
72      * @param duration the animation duration
73      * @param text the text to fade in
74      * @param colorFunction the animation function for the color
75      * @param scaleXFunction the animation function for the horizontal scale
76      * @param scaleYFunction the animation function for the vertical scale
77      * @param spaceFunction the animation function for the glyph space
78      */

79     public BasicTextAnimation(
80         BasicTextLabel label,
81         long duration,
82         String JavaDoc text,
83         AnimationFunction colorFunction,
84         AnimationFunction scaleXFunction,
85         AnimationFunction scaleYFunction,
86         AnimationFunction spaceFunction) {
87
88         super(duration);
89         this.label = label;
90         this.text = text;
91         this.colorFunction =
92             colorFunction != null
93                 ? colorFunction
94                 : defaultFadeColorFunction(duration, Color.darkGray);
95         this.scaleXFunction =
96             AnimationFunctions.asFloat(
97                 scaleXFunction != null
98                     ? scaleXFunction
99                     : AnimationFunctions.ONE);
100         this.scaleYFunction =
101             AnimationFunctions.asFloat(
102                 scaleYFunction != null
103                     ? scaleYFunction
104                     : AnimationFunctions.ONE);
105         this.spaceFunction =
106             AnimationFunctions.asFloat(
107                 spaceFunction != null
108                     ? spaceFunction
109                     : AnimationFunctions.ZERO);
110         this.offsetFunction =
111             AnimationFunctions.asFloat(defaultOffsetFunction());
112     }
113
114     /**
115      * Creates and answers an animation, that provides a text fade-in and -out.
116      *
117      * @param label the animation target component
118      * @param duration the animation duration
119      * @param text the text to fade in
120      * @param baseColor the base color for the fade effect
121      * @return an animation with a default text fade
122      */

123     public static BasicTextAnimation defaultFade(
124         BasicTextLabel label,
125         long duration,
126         String JavaDoc text,
127         Color JavaDoc baseColor) {
128
129         return new BasicTextAnimation(
130             label,
131             duration,
132             text,
133             cinemaFadeColorFunction(duration, baseColor),
134             //defaultFadeColorFunction(duration, baseColor),
135
null,
136             null,
137             null);
138     }
139
140     /**
141      * Creates and answers an animation, that provides a text fade-in
142      * and -out and scales the text while fading out.
143      *
144      * @param label the animation target component
145      * @param duration the animation duration
146      * @param text the text to fade in
147      * @param baseColor the base color for the fade effect
148      * @return an animation with a default scaling text effect
149      */

150     public static BasicTextAnimation defaultScale(
151         BasicTextLabel label,
152         long duration,
153         String JavaDoc text,
154         Color JavaDoc baseColor) {
155
156         return new BasicTextAnimation(
157             label,
158             duration,
159             text,
160             defaultScaleColorFunction(duration, baseColor),
161             defaultScaleFunction(duration),
162             defaultScaleFunction(duration),
163             null);
164     }
165
166     /**
167      * Creates and answers an animation, that provides a text fade-in
168      * and -out and increases the glyph spacing.
169      *
170      * @param label the animation target component
171      * @param duration the animation duration
172      * @param text the text to fade in
173      * @param baseColor the base color for the fade effect
174      * @return an animation with a default glyph spacing effect
175      */

176     public static BasicTextAnimation defaultSpace(
177         BasicTextLabel label,
178         long duration,
179         String JavaDoc text,
180         Color JavaDoc baseColor) {
181
182         return new BasicTextAnimation(
183             label,
184             duration,
185             text,
186             defaultSpaceColorFunction(duration, baseColor),
187             null,
188             null,
189             defaultSpaceFunction(duration));
190     }
191
192     /**
193      * Creates and answers the color animation function for the default fade.
194      *
195      * @param duration the animation duration
196      * @param baseColor the base color for the fade effect
197      * @return a Color-valued animation function for the default fade
198      */

199     public static AnimationFunction defaultFadeColorFunction(
200         long duration,
201         Color JavaDoc baseColor) {
202         return AnimationFunctions.alphaColor(
203             AnimationFunctions.linear(
204                 duration,
205                 new Integer JavaDoc[] {
206                     new Integer JavaDoc(0),
207                     new Integer JavaDoc(255),
208                     new Integer JavaDoc(255),
209                     new Integer JavaDoc(0)},
210                 new float[] { 0.0f, 0.3f, 0.7f, 1.0f }),
211             baseColor);
212     }
213
214     /**
215      * Creates and answers the color animation function for the default fade.
216      *
217      * @param duration the animation duration
218      * @param baseColor the base color for the fade effect
219      * @return a Color-valued animation function for the default fade
220      */

221     public static AnimationFunction cinemaFadeColorFunction(
222         long duration,
223         Color JavaDoc baseColor) {
224         return AnimationFunctions.alphaColor(
225             AnimationFunctions.linear(
226                 duration,
227                 new Integer JavaDoc[] {
228                     new Integer JavaDoc(0),
229                     new Integer JavaDoc(255),
230                     new Integer JavaDoc(255),
231                     new Integer JavaDoc(0)},
232                 new float[] { 0.0f, 0.3f, 0.85f, 1.0f }),
233             baseColor);
234     }
235
236     /**
237      * Creates and answers the animation function for the default scaling.
238      *
239      * @param duration the animation duration
240      * @param baseColor the base color for the fade effect
241      * @return a Color-valued animation function for the default scaling
242      */

243     public static AnimationFunction defaultScaleColorFunction(
244         long duration,
245         Color JavaDoc baseColor) {
246         return AnimationFunctions.alphaColor(
247             AnimationFunctions.linear(
248                 duration,
249                 new Integer JavaDoc[] {
250                     new Integer JavaDoc(0),
251                     new Integer JavaDoc(255),
252                     new Integer JavaDoc(255),
253                     new Integer JavaDoc(0)},
254                 new float[] { 0.0f, 0.2f, 0.85f, 1.0f }),
255             baseColor);
256     }
257
258     /**
259      * Creates and answers the color animation function for
260      * the default spacing animation.
261      *
262      * @param duration the animation duration
263      * @param baseColor the base color for the fade effect
264      * @return a Color-valued animation function for the default spacing
265      */

266     public static AnimationFunction defaultSpaceColorFunction(
267         long duration,
268         Color JavaDoc baseColor) {
269         return AnimationFunctions.alphaColor(
270             AnimationFunctions.linear(
271                 duration,
272                 new Integer JavaDoc[] {
273                     new Integer JavaDoc(0),
274                     new Integer JavaDoc(255),
275                     new Integer JavaDoc(255),
276                     new Integer JavaDoc(0)},
277                 new float[] { 0.0f, 0.2f, 0.8f, 1.0f }),
278             baseColor);
279     }
280
281     /**
282      * Returns the animation function for the default random position offset.
283      *
284      * @return an animation function for a default random offset
285      */

286     public static AnimationFunction defaultOffsetFunction() {
287         return AnimationFunctions.random(-2, 2, 0.5f);
288     }
289
290     /**
291      * Creates and answers the default scaling animation function.
292      *
293      * @param duration the animation duration
294      * @return an animation function for the default scaling effect
295      */

296     public static AnimationFunction defaultScaleFunction(long duration) {
297         return AnimationFunctions.linear(
298             duration,
299             new Float JavaDoc[] { new Float JavaDoc(1.0f), new Float JavaDoc(1.0f), new Float JavaDoc(1.8f)},
300             new float[] { 0.0f, 0.85f, 1.0f });
301     }
302
303     /**
304      * Creates and answers the default spacing animation function.
305      *
306      * @param duration the animation duration
307      * @return an animation function for the default spacing effect
308      */

309     public static AnimationFunction defaultSpaceFunction(long duration) {
310         return AnimationFunctions.fromTo(duration, 0, 10);
311     }
312     
313     
314     // ************************************************************************
315

316     /**
317      * Applies the effect: sets color, spacing, scaling and offset,
318      * the latter only if enabled.
319      *
320      * @param time the render time
321      */

322     protected void applyEffect(long time) {
323         label.setText(time == 0 ? " " : text);
324         label.setColor((Color JavaDoc) colorFunction.valueAt(time));
325         label.setScaleX(scaleXFunction.valueAt(time));
326         label.setScaleY(scaleYFunction.valueAt(time));
327         label.setSpace(spaceFunction.valueAt(time));
328         if (isOffsetEnabled()) {
329             label.setOffsetX(offsetFunction.valueAt(time));
330             label.setOffsetY(offsetFunction.valueAt(time));
331         }
332     }
333
334     /**
335      * Answers whether the random position offset is enabled.
336      *
337      * @return true indicates offset enabled, false disabled
338      */

339     public boolean isOffsetEnabled() {
340         return offsetEnabled;
341     }
342     
343     /**
344      * Enables or disables the random position offset
345      *
346      * @param b the new enablement
347      */

348     public void setOffsetEnabled(boolean b) {
349         offsetEnabled = b;
350     }
351
352 }
Popular Tags