001 package com.croftsoft.apps.ajgp.anim;
002
003 import java.awt.Graphics2D;
004 import javax.swing.JComponent;
005
006 import com.croftsoft.core.animation.ComponentAnimator;
007 import com.croftsoft.core.lang.NullArgumentException;
008
009 /*********************************************************************
010 * ComponentAnimator implementation example.
011 *
012 * @version
013 * 2003-05-06
014 * @since
015 * 2003-05-06
016 * @author
017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
018 *********************************************************************/
019
020 public final class ExampleAnimator
021 implements ComponentAnimator
022 //////////////////////////////////////////////////////////////////////
023 //////////////////////////////////////////////////////////////////////
024 {
025
026 private final String text;
027
028 private final int deltaX;
029
030 private final int deltaY;
031
032 //
033
034 private int x;
035
036 private int y;
037
038 //////////////////////////////////////////////////////////////////////
039 //////////////////////////////////////////////////////////////////////
040
041 public ExampleAnimator (
042 String text,
043 int deltaX,
044 int deltaY )
045 //////////////////////////////////////////////////////////////////////
046 {
047 NullArgumentException.check ( this.text = text );
048
049 this.deltaX = deltaX;
050
051 this.deltaY = deltaY;
052 }
053
054 //////////////////////////////////////////////////////////////////////
055 //////////////////////////////////////////////////////////////////////
056
057 public void update ( JComponent component )
058 //////////////////////////////////////////////////////////////////////
059 {
060 x += deltaX;
061
062 y += deltaY;
063
064 int componentWidth = component.getWidth ( );
065
066 int componentHeight = component.getHeight ( );
067
068 if ( x > componentWidth )
069 {
070 x = 0;
071 }
072 else if ( x < 0 )
073 {
074 x = componentWidth;
075 }
076
077 if ( y > componentHeight )
078 {
079 y = 0;
080 }
081 else if ( y < 0 )
082 {
083 y = componentHeight;
084 }
085
086 component.repaint ( );
087 }
088
089 public void paint (
090 JComponent component,
091 Graphics2D graphics )
092 //////////////////////////////////////////////////////////////////////
093 {
094 graphics.setColor ( component.getForeground ( ) );
095
096 graphics.drawString ( text, x, y );
097 }
098
099 //////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////
101 }