1 8 9 package com.jgoodies.animation.components; 10 11 import java.awt.*; 12 13 import javax.swing.JLabel ; 14 import javax.swing.JPanel ; 15 import javax.swing.SwingConstants ; 16 import javax.swing.SwingUtilities ; 17 import javax.swing.plaf.FontUIResource ; 18 19 import com.jgoodies.animation.AbstractAnimation; 20 import com.jgoodies.animation.Animation; 21 import com.jgoodies.animation.AnimationAdapter; 22 import com.jgoodies.animation.AnimationEvent; 23 import com.jgoodies.animation.Animator; 24 25 36 37 public final class AnimatedLabel extends JPanel { 38 39 public static final int RIGHT = SwingConstants.RIGHT; 40 public static final int CENTER = SwingConstants.CENTER; 41 public static final int LEFT = SwingConstants.LEFT; 42 43 public static final Color DEFAULT_BASE_COLOR = new Color(64, 64, 64); 44 public static final int DEFAULT_FONT_EXTRA_SIZE = 8; 45 46 private static final int DEFAULT_DURATION = 300; 47 private static final int DEFAULT_ANIMATION_FPS = 30; 48 49 private JLabel [] labels; 50 private int foreground = 0; 51 private int background = 1; 52 53 private Color baseColor; 54 private boolean animationEnabled; 55 private int orientation; 56 private long duration; 57 private int fps; 58 private Animation animation; 59 60 62 72 public AnimatedLabel( 73 Color baseColor, 74 int fontExtraSize, 75 String text, 76 int orientation, 77 int duration, 78 int frames_per_second) { 79 this.baseColor = baseColor; 80 this.orientation = orientation; 81 this.duration = duration; 82 this.fps = frames_per_second; 83 this.animationEnabled = true; 84 initComponents(fontExtraSize); 85 build(); 86 setTextImmediately(text); 87 } 88 89 98 public AnimatedLabel( 99 Color baseColor, 100 int fontExtraSize, 101 String text, 102 int orientation) { 103 this( 104 baseColor, 105 fontExtraSize, 106 text, 107 orientation, 108 DEFAULT_DURATION, 109 DEFAULT_ANIMATION_FPS); 110 } 111 112 120 public AnimatedLabel(Color baseColor, int fontExtraSize, String text) { 121 this(baseColor, fontExtraSize, text, LEFT); 122 } 123 124 128 public AnimatedLabel() { 129 this(DEFAULT_BASE_COLOR, DEFAULT_FONT_EXTRA_SIZE, ""); 130 } 131 132 134 139 public boolean isAnimationEnabled() { 140 return animationEnabled; 141 } 142 143 151 public void setAnimationEnabled(boolean enable) { 152 boolean oldValue = animationEnabled; 153 animationEnabled = enable; 154 firePropertyChange("animationEnabled", oldValue, enable); 155 } 156 157 162 public Color getForeground() { 163 return baseColor; 164 } 165 166 171 public void setForeground(Color newForeground) { 172 Color oldForeground = getForeground(); 173 baseColor = newForeground; 174 firePropertyChange("foreground", oldForeground, newForeground); 175 } 176 177 182 public long getDuration() { 183 return duration; 184 } 185 186 191 public void setDuration(int newDuration) { 192 long oldDuration = duration; 193 duration = newDuration; 194 animation = null; 195 firePropertyChange("duration", oldDuration, newDuration); 196 } 197 198 200 205 public synchronized String getText() { 206 return labels[foreground].getText(); 207 } 208 209 215 public synchronized void setText(String newText) { 216 if (!isAnimationEnabled()) { 217 setTextImmediately(newText); 218 return; 219 } 220 221 String oldText = getText(); 222 labels[background].setText(newText); 223 foreground = 1 - foreground; 224 background = 1 - background; 225 new Animator(animation(), fps).start(); 226 firePropertyChange("text", oldText, newText); 227 } 228 229 234 public void setTextImmediately(String newText) { 235 String oldText = getText(); 236 labels[background].setText(newText); 237 foreground = 1 - foreground; 238 background = 1 - background; 239 setAlpha(255, 0); 240 firePropertyChange("text", oldText, newText); 241 } 242 243 248 private Animation animation() { 249 if (animation == null) { 250 animation = new BlendOverAnimation(duration); 251 animation.addAnimationListener(new AnimationAdapter() { 252 public void animationStopped(AnimationEvent e) { 253 setAlpha(255, 0); 254 } 255 }); 256 } 257 return animation; 258 } 259 260 262 269 private void initComponents(int fontExtraSize) { 270 labels = new JLabel [2]; 271 labels[foreground] = 272 createBoldLabel(fontExtraSize, getTranslucentColor(255)); 273 labels[background] = 274 createBoldLabel(fontExtraSize, getTranslucentColor(255)); 275 } 276 277 private void build() { 278 setOpaque(false); 279 setLayout(new GridBagLayout()); 280 GridBagConstraints gbc = new GridBagConstraints(); 281 gbc.anchor = anchor(); 282 gbc.gridx = 0; 283 gbc.gridy = 0; 284 add(labels[foreground], gbc); 285 add(labels[background], gbc); 286 } 287 288 private int anchor() { 289 if (orientation == RIGHT) { 290 return GridBagConstraints.EAST; 291 } else if (orientation == CENTER) { 292 return GridBagConstraints.CENTER; 293 } else { 294 return GridBagConstraints.WEST; 295 } 296 } 297 298 307 private JLabel createBoldLabel(int sizeIncrement, Color aForeground) { 308 JLabel label = new AntiAliasedLabel("", Font.BOLD, sizeIncrement); 309 label.setForeground(aForeground); 310 return label; 311 } 312 313 315 322 private Color getTranslucentColor(int alpha) { 323 return new Color( 324 baseColor.getRed(), 325 baseColor.getGreen(), 326 baseColor.getBlue(), 327 alpha); 328 } 329 330 336 private void setAlpha0(int foregroundAlpha, int backgroundAlpha) { 337 labels[foreground].setForeground(getTranslucentColor(foregroundAlpha)); 338 labels[background].setForeground(getTranslucentColor(backgroundAlpha)); 339 } 340 341 347 private void setAlpha( 348 final int foregroundAlpha, 349 final int backgroundAlpha) { 350 if (SwingUtilities.isEventDispatchThread()) { 351 setAlpha0(foregroundAlpha, backgroundAlpha); 352 return; 353 } 354 Runnable runnable = new Runnable () { 355 public void run() { 356 setAlpha0(foregroundAlpha, backgroundAlpha); 357 } 358 }; 359 SwingUtilities.invokeLater(runnable); 360 } 361 362 364 368 private class BlendOverAnimation extends AbstractAnimation { 369 370 376 public BlendOverAnimation(long duration) { 377 super(duration, true); 378 } 379 380 385 protected void applyEffect(long time) { 386 int foregroundAlpha = (int) (255 * time / duration()); 387 int backgroundAlpha = 255 - foregroundAlpha; 388 setAlpha(foregroundAlpha, backgroundAlpha); 389 } 390 } 391 392 394 private static class AntiAliasedLabel extends JLabel { 395 396 private final int fontExtraSize; 397 private final int fontStyle; 398 399 408 private AntiAliasedLabel( 409 String text, 410 int fontStyle, 411 int fontExtraSize) { 412 super(text); 413 this.fontStyle = fontStyle; 414 this.fontExtraSize = fontExtraSize; 415 updateUI(); 416 } 417 418 423 public void paint(Graphics g) { 424 Graphics2D g2 = (Graphics2D) g; 425 Object oldHint = 426 g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING); 427 g2.setRenderingHint( 428 RenderingHints.KEY_ANTIALIASING, 429 RenderingHints.VALUE_ANTIALIAS_ON); 430 super.paint(g2); 431 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldHint); 432 } 433 434 437 public void updateUI() { 438 super.updateUI(); 439 Font font = getFont(); 440 if (0 == fontExtraSize) { 441 if (font.getStyle() != fontStyle) 442 setFont(new FontUIResource (font.deriveFont(fontStyle))); 443 } else 444 setFont( 445 new FontUIResource ( 446 new Font( 447 font.getName(), 448 fontStyle, 449 font.getSize() + fontExtraSize))); 450 } 451 } 452 453 } | Popular Tags |