001         package com.croftsoft.core.animation.animator;
002    
003         import java.awt.*;
004         import java.awt.geom.Rectangle2D;
005         import javax.swing.JComponent;
006    
007         import com.croftsoft.core.animation.ComponentAnimator;
008         import com.croftsoft.core.lang.NullArgumentException;
009    
010         import com.croftsoft.core.animation.model.ModelAccessor;
011    
012         /*********************************************************************
013         * The view for a Model.
014         *
015         * @version
016         *   2003-06-06
017         * @since
018         *   2003-04-01
019         * @author
020         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
021         *********************************************************************/
022    
023         public class  ModelAnimator
024           implements ComponentAnimator
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028    
029         protected final ModelAccessor  modelAccessor;
030    
031         protected final Rectangle      oldRepaintRectangle;
032    
033         protected final Rectangle      newRepaintRectangle;
034    
035         //
036    
037         protected Color    color;
038    
039         protected boolean  previouslyActive;
040    
041         //////////////////////////////////////////////////////////////////////
042         //////////////////////////////////////////////////////////////////////
043    
044         public  ModelAnimator (
045           ModelAccessor  modelAccessor,
046           Color          color )
047         //////////////////////////////////////////////////////////////////////
048         {
049           NullArgumentException.check ( this.modelAccessor = modelAccessor );
050    
051           this.color = color;
052    
053           oldRepaintRectangle = new Rectangle ( );
054    
055           newRepaintRectangle = new Rectangle ( );
056         }
057    
058         public  ModelAnimator ( ModelAccessor  modelAccessor )
059         //////////////////////////////////////////////////////////////////////
060         {
061           this ( modelAccessor, null );
062         }
063    
064         //////////////////////////////////////////////////////////////////////
065         //////////////////////////////////////////////////////////////////////
066    
067         public void  update ( JComponent  component )
068         //////////////////////////////////////////////////////////////////////
069         {
070           boolean  currentlyActive = modelAccessor.isActive ( );
071    
072           if ( !previouslyActive )
073           {
074             if ( currentlyActive )
075             {
076               // went from off to on
077    
078               getRepaintRectangle ( oldRepaintRectangle );
079    
080               component.repaint ( oldRepaintRectangle );
081    
082               previouslyActive = currentlyActive;
083             }
084    
085             // otherwise stayed off
086           }
087           else if ( !currentlyActive )
088           {
089             // went from on to off
090    
091             component.repaint ( oldRepaintRectangle );
092    
093             previouslyActive = currentlyActive;
094           }
095           else
096           {
097             // stayed on
098    
099             getRepaintRectangle ( newRepaintRectangle );
100    
101             if ( !oldRepaintRectangle.equals ( newRepaintRectangle ) )
102             {
103               Rectangle2D.union (
104                 oldRepaintRectangle,
105                 newRepaintRectangle,
106                 oldRepaintRectangle );
107    
108               component.repaint ( oldRepaintRectangle );
109    
110               oldRepaintRectangle.setBounds ( newRepaintRectangle );
111             }
112             else if ( isUpdated ( ) )
113             {
114               component.repaint ( newRepaintRectangle );
115             }
116           }
117         }
118    
119         protected void  getRepaintRectangle ( Rectangle  repaintRectangle )
120         //////////////////////////////////////////////////////////////////////
121         {
122           repaintRectangle.setBounds (
123             modelAccessor.getShape ( ).getBounds ( ) );
124         }
125    
126         protected boolean  isUpdated ( )
127         //////////////////////////////////////////////////////////////////////
128         {
129           return modelAccessor.isUpdated ( );
130         }
131    
132         public void  paint (
133           JComponent  component,
134           Graphics2D  graphics )
135         //////////////////////////////////////////////////////////////////////
136         {
137           if ( modelAccessor.isActive ( ) )
138           {
139             if ( color == null )
140             {
141               graphics.setColor ( component.getForeground ( ) );
142             }
143             else
144             {
145               graphics.setColor ( color );
146             }
147    
148             graphics.fill ( modelAccessor.getShape ( ) );
149           }
150         }
151    
152         //////////////////////////////////////////////////////////////////////
153         //////////////////////////////////////////////////////////////////////
154         }