001 package com.croftsoft.core.animation.animator;
002
003 import java.awt.Graphics2D;
004 import java.io.Serializable;
005 import javax.swing.JComponent;
006
007 import com.croftsoft.core.animation.ComponentAnimator;
008
009 /*********************************************************************
010 * Animates text.
011 *
012 * @version
013 * 2002-03-31
014 * @since
015 * 2002-03-31
016 * @author
017 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
018 *********************************************************************/
019
020 public class TextAnimator
021 implements ComponentAnimator, Serializable
022 //////////////////////////////////////////////////////////////////////
023 //////////////////////////////////////////////////////////////////////
024 {
025
026 private static final long serialVersionUID = 0L;
027
028 //
029
030 private String text;
031
032 private int deltaX;
033
034 private int deltaY;
035
036 private int x;
037
038 private int y;
039
040 //////////////////////////////////////////////////////////////////////
041 //////////////////////////////////////////////////////////////////////
042
043 public TextAnimator ( )
044 //////////////////////////////////////////////////////////////////////
045 {
046 }
047
048 //////////////////////////////////////////////////////////////////////
049 //////////////////////////////////////////////////////////////////////
050
051 public int getDeltaX ( ) { return deltaX; }
052
053 public int getDeltaY ( ) { return deltaY; }
054
055 public String getText ( ) { return text; }
056
057 public int getX ( ) { return x; }
058
059 public int getY ( ) { return y; }
060
061 //////////////////////////////////////////////////////////////////////
062 //////////////////////////////////////////////////////////////////////
063
064 public void setDeltaX ( int deltaX ) { this.deltaX = deltaX; }
065
066 public void setDeltaY ( int deltaY ) { this.deltaY = deltaY; }
067
068 public void setText ( String text ) { this.text = text; }
069
070 public void setX ( int x ) { this.x = x; }
071
072 public void setY ( int y ) { this.y = y; }
073
074 //////////////////////////////////////////////////////////////////////
075 //////////////////////////////////////////////////////////////////////
076
077 public void update ( JComponent component )
078 //////////////////////////////////////////////////////////////////////
079 {
080 x += deltaX;
081
082 y += deltaY;
083
084 int componentWidth = component.getWidth ( );
085
086 int componentHeight = component.getHeight ( );
087
088 if ( x > componentWidth )
089 {
090 x = 0;
091 }
092 else if ( x < 0 )
093 {
094 x = componentWidth;
095 }
096
097 if ( y > componentHeight )
098 {
099 y = 0;
100 }
101 else if ( y < 0 )
102 {
103 y = componentHeight;
104 }
105
106 component.repaint ( );
107 }
108
109 public void paint (
110 JComponent component,
111 Graphics2D graphics )
112 //////////////////////////////////////////////////////////////////////
113 {
114 graphics.setColor ( component.getForeground ( ) );
115
116 graphics.drawString ( text, x, y );
117 }
118
119 //////////////////////////////////////////////////////////////////////
120 //////////////////////////////////////////////////////////////////////
121 }