KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.LinkedList JavaDoc;
35 import java.util.List JavaDoc;
36
37 import com.jgoodies.animation.Animation;
38 import com.jgoodies.animation.AnimationUtils;
39 import com.jgoodies.animation.Animations;
40 import com.jgoodies.animation.components.BasicTextLabel;
41
42
43 /**
44  * Provides a text animation that shows an overlapping sequence of
45  * texts using a bunch of different effects: color fade, scaling, glyph spacing.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.4 $
49  *
50  * @see BasicTextAnimation
51  */

52 public final class BasicTextAnimations {
53     
54     private static final int FADE_TYPE = 0;
55     private static final int SCALE_TYPE = 1;
56     private static final int SPACE_TYPE = 2;
57     
58     
59     private BasicTextAnimations() {
60         // Override default constructor; prevents instantiation.
61
}
62     
63
64     /**
65      * Creates and answers the default color fade text sequence.
66      *
67      * @param label1 a text label used to blend over
68      * @param label2 a second text label used to blend over
69      * @param singleDuration the duration of a single animation
70      * @param beginOffset an initial animation time offset
71      * @param separatedTexts a sequence of texts in a string separated
72      * by the | character
73      * @param baseColor the base color for the fade
74      * @return a default fade animation
75      */

76     public static Animation defaultFade(
77         BasicTextLabel label1,
78         BasicTextLabel label2,
79         long singleDuration,
80         long beginOffset,
81         String JavaDoc separatedTexts,
82         Color JavaDoc baseColor) {
83             
84         return createTextSequence(label1, label2,
85                                    singleDuration, beginOffset,
86                                    separatedTexts, baseColor,
87                                    FADE_TYPE);
88     }
89     
90
91     /**
92      * Creates and answers the default scaling text sequence.
93      *
94      * @param label1 a text label used to blend over
95      * @param label2 a second text label used to blend over
96      * @param singleDuration the duration of a single animation
97      * @param beginOffset an initial animation time offset
98      * @param separatedTexts a sequence of texts in a string separated
99      * by the | character
100      * @param baseColor the base color for the fade
101      * @return a default scaling blend over animation
102      */

103     public static Animation defaultScale(
104         BasicTextLabel label1,
105         BasicTextLabel label2,
106         long singleDuration,
107         long beginOffset,
108         String JavaDoc separatedTexts,
109         Color JavaDoc baseColor) {
110             
111         return createTextSequence(label1, label2,
112                                    singleDuration, beginOffset,
113                                    separatedTexts, baseColor,
114                                    SCALE_TYPE);
115     }
116
117
118     /**
119      * Creates and answers the default glyph spacing text sequence.
120      *
121      * @param label1 a text label used to blend over
122      * @param label2 a second text label used to blend over
123      * @param singleDuration the duration of a single animation
124      * @param beginOffset an initial animation time offset
125      * @param separatedTexts a sequence of texts in a string separated
126      * by the | character
127      * @param baseColor the base color for the fade
128      * @return a default space blend over animation
129      */

130     public static Animation defaultSpace(
131         BasicTextLabel label1,
132         BasicTextLabel label2,
133         long singleDuration,
134         long beginOffset,
135         String JavaDoc separatedTexts,
136         Color JavaDoc baseColor) {
137             
138         return createTextSequence(label1, label2,
139                                    singleDuration, beginOffset,
140                                    separatedTexts, baseColor,
141                                    SPACE_TYPE);
142     }
143     
144
145     // Private Helper Code ****************************************************
146

147     /**
148      * Creates and returns the default glyph spacing text sequence.
149      *
150      * @param label1 the first label to render the sequence
151      * @param label2 the second label to render
152      * @param singleDuration the duration of a step in the sequence
153      * @param beginOffset an offset in ms between to steps
154      * @param separatedTexts a comma separated lists of texts to display
155      * @param baseColor the color used as a basis for the text
156      * @param type the type of the effect used to change
157      * @return a composed animation that displays a sequence of texts
158      */

159     private static Animation createTextSequence(
160         BasicTextLabel label1,
161         BasicTextLabel label2,
162         long singleDuration,
163         long beginOffset,
164         String JavaDoc separatedTexts,
165         Color JavaDoc baseColor,
166         int type) {
167             
168         Animation animation;
169         String JavaDoc[] texts = AnimationUtils.splitTexts(separatedTexts);
170         List JavaDoc animations = new LinkedList JavaDoc();
171         long beginTime = 0;
172         
173         BasicTextLabel label = label1;
174         for (int i = 0; i < texts.length; i++) {
175             label = i % 2 == 0 ? label1 : label2;
176             animation = animation(label, singleDuration, texts[i], baseColor, type);
177             animations.add(Animations.offset(beginTime, animation));
178             beginTime += singleDuration + beginOffset;
179         }
180
181         return Animations.parallel(animations);
182     }
183     
184     
185     private static Animation animation(BasicTextLabel label,
186                                  long duration,
187                                  String JavaDoc text,
188                                  Color JavaDoc baseColor,
189                                  int type) {
190         switch (type) {
191             case FADE_TYPE :
192                 return BasicTextAnimation.defaultFade(label, duration, text, baseColor);
193
194             case SCALE_TYPE :
195                 return BasicTextAnimation.defaultScale(label, duration, text, baseColor);
196
197             case SPACE_TYPE :
198                 return BasicTextAnimation.defaultSpace(label, duration, text, baseColor);
199
200             default :
201                 return null;
202         }
203     }
204
205     
206 }
Popular Tags