001         package com.croftsoft.core.animation.animator;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005         import javax.swing.*;
006    
007         import com.croftsoft.core.animation.ComponentAnimator;
008    
009         /*********************************************************************
010         * Animates an Icon at the mouse position.
011         *
012         * @version
013         *   2003-07-12
014         * @since
015         *   2002-02-26
016         * @author
017         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
018         *********************************************************************/
019    
020         public class  CursorAnimator
021           implements ComponentAnimator, MouseListener, MouseMotionListener
022         //////////////////////////////////////////////////////////////////////
023         //////////////////////////////////////////////////////////////////////
024         {
025    
026         private final Rectangle  oldPaintBounds;
027    
028         private final Rectangle  newPaintBounds;
029    
030         //
031    
032         private Icon     currentIcon;
033    
034         private Icon     nextIcon;
035    
036         private Icon     mouseReleasedIcon;
037    
038         private Icon     mousePressedIcon;
039    
040         private int      hotSpotX;
041    
042         private int      hotSpotY;
043    
044         private int      x;
045    
046         private int      y;
047    
048         private boolean  updated;
049    
050         //////////////////////////////////////////////////////////////////////
051         // constructor methods
052         //////////////////////////////////////////////////////////////////////
053    
054         public  CursorAnimator (
055           Icon       mouseReleasedIcon,
056           Icon       mousePressedIcon,
057           Point      hotSpot,
058           Component  component )
059         //////////////////////////////////////////////////////////////////////
060         {
061           setMouseReleasedIcon ( mouseReleasedIcon, hotSpot );
062    
063           setMousePressedIcon  ( mousePressedIcon , hotSpot );
064    
065           oldPaintBounds = new Rectangle ( );
066    
067           newPaintBounds = new Rectangle ( );
068    
069           if ( component != null )
070           {
071             component.addMouseMotionListener ( this );
072    
073             component.addMouseListener       ( this );
074    
075             component.setCursor ( new Cursor ( Cursor.CROSSHAIR_CURSOR ) );
076           }
077         }
078    
079         //////////////////////////////////////////////////////////////////////
080         // accessor methods
081         //////////////////////////////////////////////////////////////////////
082    
083         public Icon  getMouseReleasedIcon ( ) { return mouseReleasedIcon; }
084    
085         public Icon  getMousePressedIcon  ( ) { return mousePressedIcon;  }
086    
087         //////////////////////////////////////////////////////////////////////
088         // mutator methods
089         //////////////////////////////////////////////////////////////////////
090    
091         public void  setMouseReleasedIcon (
092           Icon   mouseReleasedIcon,
093           Point  hotSpot )
094         //////////////////////////////////////////////////////////////////////
095         {
096           this.mouseReleasedIcon = mouseReleasedIcon;
097    
098           updateHotSpot ( hotSpot );
099         }
100    
101         public void  setMousePressedIcon (
102           Icon   mousePressedIcon,
103           Point  hotSpot )
104         //////////////////////////////////////////////////////////////////////
105         {
106           this.mousePressedIcon = mousePressedIcon;
107    
108           updateHotSpot ( hotSpot );
109         }
110    
111         //////////////////////////////////////////////////////////////////////
112         // interface ComponentAnimator methods
113         //////////////////////////////////////////////////////////////////////
114    
115         public void  update ( JComponent  component )
116         //////////////////////////////////////////////////////////////////////
117         {
118           if ( !updated )
119           {
120             return;
121           }
122    
123           updated = false;
124    
125           currentIcon = nextIcon;
126    
127           if ( currentIcon != null )
128           {
129             newPaintBounds.x      = x;
130    
131             newPaintBounds.y      = y;
132    
133             newPaintBounds.width  = currentIcon.getIconWidth  ( );
134    
135             newPaintBounds.height = currentIcon.getIconHeight ( );
136           }
137           else
138           {
139             newPaintBounds.width = 0;
140           }
141    
142           component.repaint ( oldPaintBounds );
143    
144           component.repaint ( newPaintBounds );
145    
146           oldPaintBounds.setBounds ( newPaintBounds );
147         }
148    
149         public void  paint (
150           JComponent  component,
151           Graphics2D  graphics )
152         //////////////////////////////////////////////////////////////////////
153         {
154           if ( currentIcon != null )
155           {
156             currentIcon.paintIcon ( component, graphics, x, y );
157           }
158         }
159    
160         //////////////////////////////////////////////////////////////////////
161         // interface MouseListener methods
162         //////////////////////////////////////////////////////////////////////
163    
164         public void  mouseClicked ( MouseEvent  mouseEvent )
165         //////////////////////////////////////////////////////////////////////
166         {
167         }
168    
169         public void  mouseEntered ( MouseEvent  mouseEvent )
170         //////////////////////////////////////////////////////////////////////
171         {
172           updated = true;
173    
174           nextIcon = mouseReleasedIcon;
175         }
176    
177         public void  mouseExited ( MouseEvent  mouseEvent )
178         //////////////////////////////////////////////////////////////////////
179         {
180           updated = true;
181    
182           nextIcon = null;
183         }
184    
185         public void  mousePressed ( MouseEvent  mouseEvent )
186         //////////////////////////////////////////////////////////////////////
187         {
188           updated = true;
189    
190           nextIcon = mousePressedIcon;       
191         }
192    
193         public void  mouseReleased ( MouseEvent  mouseEvent )
194         //////////////////////////////////////////////////////////////////////
195         {
196           updated = true;
197    
198           nextIcon = mouseReleasedIcon;
199         }
200    
201         //////////////////////////////////////////////////////////////////////
202         // interface MouseMotionListener methods
203         //////////////////////////////////////////////////////////////////////
204    
205         public void  mouseDragged ( MouseEvent  mouseEvent )
206         //////////////////////////////////////////////////////////////////////
207         {
208           mouseMoved ( mouseEvent );
209         }
210    
211         public void  mouseMoved ( MouseEvent  mouseEvent )
212         //////////////////////////////////////////////////////////////////////
213         {
214           if ( currentIcon == null )
215           {
216             return;
217           }       
218    
219           updated = true;
220    
221           x = mouseEvent.getX ( ) - hotSpotX;
222    
223           y = mouseEvent.getY ( ) - hotSpotY;       
224         }
225    
226         //////////////////////////////////////////////////////////////////////
227         // private methods
228         //////////////////////////////////////////////////////////////////////
229    
230         private void  updateHotSpot ( Point  hotSpot )
231         //////////////////////////////////////////////////////////////////////
232         {
233           updated = true;
234    
235           if ( hotSpot != null )
236           {
237             hotSpotX = hotSpot.x;
238    
239             hotSpotY = hotSpot.y;
240           }
241           else if ( mouseReleasedIcon != null )
242           {
243             hotSpotX = mouseReleasedIcon.getIconWidth  ( ) / 2;
244    
245             hotSpotY = mouseReleasedIcon.getIconHeight ( ) / 2;
246           }
247           else if ( mousePressedIcon != null )
248           {
249             hotSpotX = mousePressedIcon.getIconWidth  ( ) / 2;
250    
251             hotSpotY = mousePressedIcon.getIconHeight ( ) / 2;
252           }
253           else
254           {
255             hotSpotX = 0;
256    
257             hotSpotY = 0;
258           }
259         }
260    
261         //////////////////////////////////////////////////////////////////////
262         //////////////////////////////////////////////////////////////////////
263         }