KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > animate > PolarLocationAnimator


1 package prefuse.action.animate;
2
3 import java.awt.geom.Point2D JavaDoc;
4
5 import prefuse.Display;
6 import prefuse.action.ItemAction;
7 import prefuse.util.MathLib;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * Animator that interpolates between starting and ending display locations
13  * by linearly interpolating between polar coordinates.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public class PolarLocationAnimator extends ItemAction {
18     
19     private Point2D JavaDoc m_anchor = new Point2D.Double JavaDoc();
20     private String JavaDoc m_linear = null;
21     
22     // temp variables
23
private double ax, ay, sx, sy, ex, ey, x, y;
24     private double dt1, dt2, sr, st, er, et, r, t, stt, ett;
25     
26     /**
27      * Creates a PolarLocationAnimator that operates on all VisualItems
28      * within a Visualization.
29      */

30     public PolarLocationAnimator() {
31         super();
32     }
33     
34     /**
35      * Creates a PolarLocationAnimator that operates on VisualItems
36      * within the specified group.
37      * @param group the data group to process
38      */

39     public PolarLocationAnimator(String JavaDoc group) {
40         super(group);
41     }
42     
43     /**
44      * Creates a PolarLocationAnimator that operates on VisualItems
45      * within the specified group, while using regular linear interpolation
46      * (in Cartesian (x,y) coordinates rather than polar coordinates) for
47      * items also contained within the specified linearGroup.
48      * @param group the data group to process
49      * @param linearGroup the group of items that should be interpolated
50      * in Cartesian (standard x,y) coordinates rather than polar coordinates.
51      * Note that this animator will not process any items in
52      * <code>linearGroup</code> that are not also in <code>group</code>.
53      */

54     public PolarLocationAnimator(String JavaDoc group, String JavaDoc linearGroup) {
55         super(group);
56         m_linear = linearGroup;
57     }
58
59     private void setAnchor() {
60         Display d = getVisualization().getDisplay(0);
61         m_anchor.setLocation(d.getWidth()/2,d.getHeight()/2);
62         d.getAbsoluteCoordinate(m_anchor, m_anchor);
63         ax = m_anchor.getX();
64         ay = m_anchor.getY();
65     }
66
67     /**
68      * @see prefuse.action.Action#run(double)
69      */

70     public void run(double frac) {
71         setAnchor();
72         super.run(frac);
73     }
74     
75     /**
76      * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
77      */

78     public void process(VisualItem item, double frac) {
79         if ( m_linear != null && item.isInGroup(m_linear) ) {
80             // perform linear interpolation instead
81
double s = item.getStartX();
82             item.setX(s + frac*(item.getEndX()-s));
83             s = item.getStartY();
84             item.setY(s + frac*(item.getEndY()-s));
85             return;
86         }
87         
88         // otherwise, interpolate in polar coordinates
89
sx = item.getStartX() - ax;
90         sy = item.getStartY() - ay;
91         ex = item.getEndX() - ax;
92         ey = item.getEndY() - ay;
93             
94         sr = Math.sqrt(sx*sx + sy*sy);
95         st = Math.atan2(sy,sx);
96             
97         er = Math.sqrt(ex*ex + ey*ey);
98         et = Math.atan2(ey,ex);
99         
100         stt = st < 0 ? st+MathLib.TWO_PI : st;
101         ett = et < 0 ? et+MathLib.TWO_PI : et;
102             
103         dt1 = et - st;
104         dt2 = ett - stt;
105             
106         if ( Math.abs(dt1) < Math.abs(dt2) ) {
107             t = st + frac * dt1;
108         } else {
109             t = stt + frac * dt2;
110         }
111         r = sr + frac * (er - sr);
112                         
113         x = Math.round(ax + r*Math.cos(t));
114         y = Math.round(ay + r*Math.sin(t));
115             
116         item.setX(x);
117         item.setY(y);
118     }
119
120 } // end of class PolarLocationAnimator
121
Popular Tags