KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > renderer > GlyphRenderer


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.renderer;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics2D JavaDoc;
35
36 import com.jgoodies.animation.AnimationFunction;
37 import com.jgoodies.animation.AnimationFunctions;
38
39 /**
40  * Renders the glyphs of a string with individual scaling, transform, and color.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.3 $
44  */

45 public final class GlyphRenderer extends AbstractTextRenderer {
46
47     private final AnimationFunction colorFunction;
48     private final AnimationFunctions.FloatFunction scaleFunction;
49     private final long glyphDelay;
50
51     private long time;
52     
53     
54     // Instance Creation ******************************************************
55

56     /**
57      * Constructs a <code>GlyphRenderer</code> that paints
58      * individual glyphs with different transforms.
59      *
60      * @param text the initial text
61      * @param scaleFunction maps times to glyph scales
62      * @param translateFunction maps times to glyph translations
63      * @param colorFunction maps times to colors
64      * @param glyphDelay a time delay between the glyph animations
65      */

66     public GlyphRenderer(
67         String JavaDoc text,
68         AnimationFunction scaleFunction,
69         AnimationFunction translateFunction,
70         AnimationFunction colorFunction,
71         long glyphDelay) {
72         super(text);
73         this.scaleFunction = AnimationFunctions.asFloat(scaleFunction);
74         this.colorFunction = colorFunction;
75         this.glyphDelay = glyphDelay;
76         this.time = 0;
77     }
78
79     
80     // Accessors **************************************************************
81

82     public void setTime(long time) {
83         this.time = time;
84     }
85
86     private long relativeTime(int glyphIndex) {
87         return Math.max(0, time - glyphDelay * glyphIndex);
88     }
89
90     private float scaleAt(int glyphIndex) {
91         return scaleFunction.valueAt(relativeTime(glyphIndex));
92     }
93
94     private Color JavaDoc colorAt(int glyphIndex) {
95         return (Color JavaDoc) colorFunction.valueAt(relativeTime(glyphIndex));
96     }
97
98     /**
99      * Renders the text. Firstly, ensures a valid cache,
100      * then sets the color, and finally paints the cached glyph shaped,
101      * using individual transforms.
102      *
103      * @param g2 the graphics object to render on
104      * @param width the width of the graphics area
105      * @param height the height of the graphics area
106      */

107     public void render(Graphics2D JavaDoc g2, int width, int height) {
108         ensureValidCache(g2);
109
110         int glyphCount = cachedGlyphShapes.length;
111         float offsetX = (width - cachedTextWidth) / 2.0f;
112         float offsetY = (height + cachedTextHeight) / 2.0f - getAdjustedDescent();
113
114         g2.translate(offsetX, offsetY);
115         for (int i = glyphCount - 1; i >= 0; i--) {
116             float scale = scaleAt(i);
117             //float translate = translateAt(i);
118

119             g2.setColor(colorAt(i));
120             //g2.translate(translate, 0);
121

122             double glyphX = cachedGlyphVector.getGlyphPosition(i).getX();
123             double glyphY =
124                 cachedGlyphVector.getGlyphVisualBounds(i).getBounds2D().getHeight();
125             double adjustX = -glyphX * (scale - 1.0f);
126             double adjustY = glyphY * (scale - 1.0f) / 2.0f;
127             g2.translate(adjustX, adjustY);
128             g2.scale(scale, scale);
129             g2.fill(cachedGlyphShapes[i]);
130             g2.scale(1.0f / scale, 1.0f / scale);
131             g2.translate(-adjustX, -adjustY);
132             //g2.translate(-translate, 0);
133
}
134         g2.translate(-offsetX, -offsetY);
135     }
136 }
Popular Tags