1 30 31 package com.jgoodies.animation.renderer; 32 33 import java.awt.Color ; 34 import java.awt.Graphics2D ; 35 36 import com.jgoodies.animation.AnimationFunction; 37 import com.jgoodies.animation.AnimationFunctions; 38 39 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 56 66 public GlyphRenderer( 67 String 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 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 colorAt(int glyphIndex) { 95 return (Color ) colorFunction.valueAt(relativeTime(glyphIndex)); 96 } 97 98 107 public void render(Graphics2D 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 119 g2.setColor(colorAt(i)); 120 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 } 134 g2.translate(-offsetX, -offsetY); 135 } 136 } | Popular Tags |