001         package com.croftsoft.apps.cyborg;
002    
003         import java.awt.*;
004         import javax.swing.JComponent;
005    
006         import com.croftsoft.core.animation.*;
007         import com.croftsoft.core.animation.collector.BooleanRepaintCollector;
008         import com.croftsoft.core.lang.NullArgumentException;
009    
010         /*********************************************************************
011         * Animated Swing component.
012         *
013         * @version
014         *   $Date: 2008/04/19 21:30:58 $
015         * @since
016         *   2005-08-12
017         * @author
018         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
019         *********************************************************************/
020    
021         public class  CyborgComponent
022           extends JComponent
023         //////////////////////////////////////////////////////////////////////
024         //////////////////////////////////////////////////////////////////////
025         {
026           
027         private static final long  serialVersionUID = 0L;
028         
029         //      
030    
031         protected ComponentAnimator  componentAnimator;
032    
033         protected RepaintCollector   repaintCollector;
034    
035         //////////////////////////////////////////////////////////////////////
036         // constructor methods
037         //////////////////////////////////////////////////////////////////////
038    
039         /*********************************************************************
040         * Main constructor.
041         *********************************************************************/
042         public  CyborgComponent ( )
043         //////////////////////////////////////////////////////////////////////
044         {
045           setRepaintCollector ( new BooleanRepaintCollector ( ) );
046    
047           setOpaque ( true );
048           
049           setFont ( CyborgConfig.FONT );
050           
051           setCursor ( CyborgConfig.CURSOR );
052           
053           setPreferredSize ( new Dimension (
054             CyborgConfig.COMPONENT_MIN_WIDTH,
055             CyborgConfig.COMPONENT_MIN_HEIGHT ) );
056         }
057    
058         //////////////////////////////////////////////////////////////////////
059         // mutator methods
060         //////////////////////////////////////////////////////////////////////
061    
062         public synchronized ComponentAnimator  setComponentAnimator (
063           ComponentAnimator  componentAnimator )
064         //////////////////////////////////////////////////////////////////////
065         {
066           NullArgumentException.check ( componentAnimator );
067    
068           ComponentAnimator  oldComponentAnimator = this.componentAnimator;
069    
070           this.componentAnimator = componentAnimator;
071    
072           return oldComponentAnimator;
073         }
074    
075         public synchronized RepaintCollector  setRepaintCollector (
076           RepaintCollector  repaintCollector )
077         //////////////////////////////////////////////////////////////////////
078         {
079           NullArgumentException.check ( repaintCollector );
080    
081           RepaintCollector  oldRepaintCollector = this.repaintCollector;
082    
083           this.repaintCollector = repaintCollector;
084    
085           return oldRepaintCollector;
086         }
087    
088         //////////////////////////////////////////////////////////////////////
089         // overridden JComponent methods
090         //////////////////////////////////////////////////////////////////////
091    
092         public void  paintComponent ( Graphics  graphics )
093         //////////////////////////////////////////////////////////////////////
094         {
095           componentAnimator.paint ( this, ( Graphics2D ) graphics );
096         }
097    
098         public void  repaint ( )
099         //////////////////////////////////////////////////////////////////////
100         {
101           repaintCollector.repaint ( );
102         }
103    
104         public void  repaint ( long  tm )
105         //////////////////////////////////////////////////////////////////////
106         {
107           repaintCollector.repaint ( );
108         }
109    
110         public void  repaint (
111           int  x,
112           int  y,
113           int  width,
114           int  height )
115         //////////////////////////////////////////////////////////////////////
116         {
117           repaintCollector.repaint ( x, y, width, height );
118         }
119    
120         public void  repaint (
121           long  tm,
122           int   x,
123           int   y,
124           int   width,
125           int   height )
126         //////////////////////////////////////////////////////////////////////
127         {
128           repaintCollector.repaint ( x, y, width, height );
129         }
130    
131         public void  repaint ( Rectangle  r )
132         //////////////////////////////////////////////////////////////////////
133         {
134           repaintCollector.repaint ( r.x, r.y, r.width, r.height );
135         }
136    
137         //////////////////////////////////////////////////////////////////////
138         //////////////////////////////////////////////////////////////////////
139    
140         public void  update ( )
141         //////////////////////////////////////////////////////////////////////
142         {
143           componentAnimator.update ( this );
144    
145           int  count = repaintCollector.getCount ( );
146    
147           Rectangle [ ]  repaintRegions
148             = repaintCollector.getRepaintRegions ( );
149    
150           for ( int  i = 0; i < count; i++ )
151           {
152             paintImmediately ( repaintRegions [ i ] );
153           }
154    
155           repaintCollector.reset ( );
156         }
157    
158         //////////////////////////////////////////////////////////////////////
159         //////////////////////////////////////////////////////////////////////
160         }