1 30 31 package com.jgoodies.animation.animations; 32 33 import java.awt.Color ; 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 52 public final class BasicTextAnimation extends AbstractAnimation { 53 54 private final BasicTextLabel label; 55 private final String 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 67 79 public BasicTextAnimation( 80 BasicTextLabel label, 81 long duration, 82 String 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 123 public static BasicTextAnimation defaultFade( 124 BasicTextLabel label, 125 long duration, 126 String text, 127 Color baseColor) { 128 129 return new BasicTextAnimation( 130 label, 131 duration, 132 text, 133 cinemaFadeColorFunction(duration, baseColor), 134 null, 136 null, 137 null); 138 } 139 140 150 public static BasicTextAnimation defaultScale( 151 BasicTextLabel label, 152 long duration, 153 String text, 154 Color 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 176 public static BasicTextAnimation defaultSpace( 177 BasicTextLabel label, 178 long duration, 179 String text, 180 Color 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 199 public static AnimationFunction defaultFadeColorFunction( 200 long duration, 201 Color baseColor) { 202 return AnimationFunctions.alphaColor( 203 AnimationFunctions.linear( 204 duration, 205 new Integer [] { 206 new Integer (0), 207 new Integer (255), 208 new Integer (255), 209 new Integer (0)}, 210 new float[] { 0.0f, 0.3f, 0.7f, 1.0f }), 211 baseColor); 212 } 213 214 221 public static AnimationFunction cinemaFadeColorFunction( 222 long duration, 223 Color baseColor) { 224 return AnimationFunctions.alphaColor( 225 AnimationFunctions.linear( 226 duration, 227 new Integer [] { 228 new Integer (0), 229 new Integer (255), 230 new Integer (255), 231 new Integer (0)}, 232 new float[] { 0.0f, 0.3f, 0.85f, 1.0f }), 233 baseColor); 234 } 235 236 243 public static AnimationFunction defaultScaleColorFunction( 244 long duration, 245 Color baseColor) { 246 return AnimationFunctions.alphaColor( 247 AnimationFunctions.linear( 248 duration, 249 new Integer [] { 250 new Integer (0), 251 new Integer (255), 252 new Integer (255), 253 new Integer (0)}, 254 new float[] { 0.0f, 0.2f, 0.85f, 1.0f }), 255 baseColor); 256 } 257 258 266 public static AnimationFunction defaultSpaceColorFunction( 267 long duration, 268 Color baseColor) { 269 return AnimationFunctions.alphaColor( 270 AnimationFunctions.linear( 271 duration, 272 new Integer [] { 273 new Integer (0), 274 new Integer (255), 275 new Integer (255), 276 new Integer (0)}, 277 new float[] { 0.0f, 0.2f, 0.8f, 1.0f }), 278 baseColor); 279 } 280 281 286 public static AnimationFunction defaultOffsetFunction() { 287 return AnimationFunctions.random(-2, 2, 0.5f); 288 } 289 290 296 public static AnimationFunction defaultScaleFunction(long duration) { 297 return AnimationFunctions.linear( 298 duration, 299 new Float [] { new Float (1.0f), new Float (1.0f), new Float (1.8f)}, 300 new float[] { 0.0f, 0.85f, 1.0f }); 301 } 302 303 309 public static AnimationFunction defaultSpaceFunction(long duration) { 310 return AnimationFunctions.fromTo(duration, 0, 10); 311 } 312 313 314 316 322 protected void applyEffect(long time) { 323 label.setText(time == 0 ? " " : text); 324 label.setColor((Color ) 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 339 public boolean isOffsetEnabled() { 340 return offsetEnabled; 341 } 342 343 348 public void setOffsetEnabled(boolean b) { 349 offsetEnabled = b; 350 } 351 352 } | Popular Tags |