001         package com.croftsoft.apps.exemplar;
002    
003         import java.awt.*;
004         import java.awt.event.ComponentAdapter;
005         import java.awt.event.ComponentEvent;
006         import javax.swing.JComponent;
007    
008         import com.croftsoft.core.animation.ComponentAnimator;
009         import com.croftsoft.core.animation.animator.FrameRateAnimator;
010         import com.croftsoft.core.lang.NullArgumentException;
011    
012         /*********************************************************************
013         * Exemplar ComponentAnimator.
014         *
015         * @version
016         *   $Date: 2006/06/22 13:38:21 $
017         * @since
018         *   2005-08-12
019         * @author
020         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
021         *********************************************************************/
022    
023         public final class  Animator
024           implements ComponentAnimator
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028             
029         private final Config             config;
030           
031         private final Accessor           accessor;
032         
033         private final Rectangle          componentBounds;
034         
035         private final FrameRateAnimator  frameRateAnimator;
036         
037         //
038         
039         private String  text;
040         
041         private int     x, y;
042    
043         //////////////////////////////////////////////////////////////////////
044         // constructor methods
045         //////////////////////////////////////////////////////////////////////
046    
047         /*********************************************************************
048         * Main constructor.
049         *********************************************************************/
050         public  Animator (
051           final Config      config,
052           final Accessor    accessor,
053           final JComponent  jComponent )
054         //////////////////////////////////////////////////////////////////////
055         {
056           NullArgumentException.checkArgs (
057             this.config   = config,
058             this.accessor = accessor,
059             jComponent );
060           
061           componentBounds = new Rectangle ( );
062           
063           jComponent.setOpaque ( true );
064           
065           jComponent.setFont ( config.getFont ( ) );
066             
067           jComponent.setCursor ( config.getCursor ( ) );
068           
069           jComponent.requestFocus ( );
070           
071           jComponent.addComponentListener (
072             new ComponentAdapter ( )
073             {
074               @Override
075               public void  componentResized ( ComponentEvent  componentEvent )
076               {
077                 update ( jComponent );
078                         
079                 jComponent.repaint ( );
080               }
081             } );
082           
083           frameRateAnimator = new FrameRateAnimator ( jComponent );
084           
085           update ( jComponent );       
086         }
087         
088         //////////////////////////////////////////////////////////////////////
089         // interface ComponentAnimator methods
090         //////////////////////////////////////////////////////////////////////
091         
092         public void  paint (
093           final JComponent  jComponent,
094           final Graphics2D  graphics2D )
095         //////////////////////////////////////////////////////////////////////
096         {
097           graphics2D.setColor ( config.getBackgroundColor ( ) );
098           
099           graphics2D.fill ( componentBounds );
100           
101           graphics2D.setColor ( config.getForegroundColor ( ) );
102           
103           graphics2D.drawString ( text, x, y );
104           
105           frameRateAnimator.paint ( jComponent, graphics2D );
106         }
107         
108         public void  update ( final JComponent  jComponent )
109         //////////////////////////////////////////////////////////////////////
110         {
111           jComponent.getBounds ( componentBounds );
112             
113           final int  offsetX = componentBounds.width  / 2;
114             
115           final int  offsetY = componentBounds.height / 2;
116             
117           final int  radius = Math.min ( offsetX, offsetY ) / 2;
118        
119           text = "Clicks:  " + accessor.getClickCount ( );
120             
121           x = offsetX + ( int ) Math.round (
122             radius * Math.sin ( accessor.getPhase ( ) ) );
123             
124           y = offsetY + ( int ) Math.round (
125             radius * Math.cos ( accessor.getPhase ( ) ) );
126             
127           frameRateAnimator.update ( jComponent );
128    
129           jComponent.paintImmediately ( componentBounds );
130         }
131    
132         //////////////////////////////////////////////////////////////////////
133         //////////////////////////////////////////////////////////////////////
134         }