001         package com.croftsoft.core.animation.animator;
002    
003         import java.awt.*;
004         import javax.swing.*;
005    
006         import com.croftsoft.core.lang.NullArgumentException;
007         import com.croftsoft.core.animation.ComponentAnimator;
008         import com.croftsoft.core.animation.painter.TilePainter;
009    
010         /*********************************************************************
011         * Paints a sliding tile pattern.
012         *
013         * @version
014         *   2003-07-05
015         * @since
016         *   2002-02-20
017         * @author
018         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
019         *********************************************************************/
020    
021         public final class  TileAnimator
022           implements ComponentAnimator
023         //////////////////////////////////////////////////////////////////////
024         //////////////////////////////////////////////////////////////////////
025         {
026    
027         private final TilePainter  tilePainter;
028    
029         private final int          deltaX;
030    
031         private final int          deltaY;
032    
033         //////////////////////////////////////////////////////////////////////
034         //////////////////////////////////////////////////////////////////////
035    
036         /*********************************************************************
037         * Main constructor.
038         *********************************************************************/
039         public  TileAnimator (
040           TilePainter  tilePainter,
041           int          deltaX,
042           int          deltaY )
043         //////////////////////////////////////////////////////////////////////
044         {
045           NullArgumentException.check ( this.tilePainter = tilePainter );
046    
047           this.deltaX = deltaX;
048    
049           this.deltaY = deltaY;
050    
051           if ( ( deltaX == 0 )
052             && ( deltaY == 0 ) )
053           {
054             throw new IllegalArgumentException ( "deltaX and deltaY both 0" );
055           }
056         }
057    
058         /*********************************************************************
059         * Convenience constructor.
060         *********************************************************************/
061         public  TileAnimator (
062           int    offsetX,
063           int    offsetY,
064           Icon   icon,
065           Shape  tileShape,
066           int    deltaX,
067           int    deltaY )
068         //////////////////////////////////////////////////////////////////////
069         {
070           this (
071             new TilePainter ( offsetX, offsetY, icon, tileShape ),
072             deltaX,
073             deltaY );
074         }
075    
076         //////////////////////////////////////////////////////////////////////
077         //////////////////////////////////////////////////////////////////////
078    
079         public void  update ( JComponent  component )
080         //////////////////////////////////////////////////////////////////////
081         {
082           int  offsetX = tilePainter.getOffsetX ( );
083    
084           int  offsetY = tilePainter.getOffsetY ( );
085    
086           int  x = offsetX - deltaX;
087    
088           int  y = offsetY - deltaY;
089    
090           int  tileWidth  = tilePainter.getTileWidth ( );
091    
092           int  tileHeight = tilePainter.getTileHeight ( );
093    
094           int  tilesWide  = tilePainter.getTileColumns ( );
095    
096           int  tilesHigh  = tilePainter.getTileRows    ( );
097    
098           if ( x < 0 )
099           {
100             x += ( tileWidth * tilesWide );
101           }
102           else if ( x >= tileWidth * tilesWide )
103           {
104             x -= ( tileWidth * tilesWide );
105           }
106    
107           if ( y < 0 )
108           {
109             y += ( tilesHigh * tileHeight );
110           }
111           else if ( y >= tilesHigh * tileHeight )
112           {
113             y -= ( tilesHigh * tileHeight );
114           }
115    
116           tilePainter.setOffsetX ( x );
117    
118           tilePainter.setOffsetY ( y );
119    
120           component.repaint ( );
121         }
122    
123         public void  paint (
124           JComponent  component,
125           Graphics2D  graphics )
126         //////////////////////////////////////////////////////////////////////
127         {
128           tilePainter.paint ( component, graphics );
129         }
130    
131         //////////////////////////////////////////////////////////////////////
132         //////////////////////////////////////////////////////////////////////
133         }