KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.action.animate;
2
3 import prefuse.action.ItemAction;
4 import prefuse.util.ColorLib;
5 import prefuse.util.PrefuseLib;
6 import prefuse.util.collections.CopyOnWriteArrayList;
7 import prefuse.visual.VisualItem;
8
9
10 /**
11  * Animator that linearly interpolates between starting and ending colors
12  * for VisualItems during an animation. By default, interpolates the three
13  * primary color fields: {@link VisualItem#STROKECOLOR stroke color},
14  * {@link VisualItem#FILLCOLOR fill color}, and
15  * {@link VisualItem#TEXTCOLOR text color}.
16  *
17  * @author <a HREF="http://jheer.org">jeffrey heer</a>
18  */

19 public class ColorAnimator extends ItemAction {
20
21     private static final String JavaDoc[] DEFAULTS = new String JavaDoc[] {
22         VisualItem.STROKECOLOR, VisualItem.FILLCOLOR,
23         VisualItem.TEXTCOLOR };
24     
25     private CopyOnWriteArrayList m_colorFields;
26     
27     /**
28      * Create a new ColorAnimator that processes all data groups.
29      */

30     public ColorAnimator() {
31         super();
32         setColorFields(DEFAULTS);
33     }
34
35     /**
36      * Create a new ColorAnimator that processes the specified group.
37      * @param group the data group to process
38      */

39     public ColorAnimator(String JavaDoc group) {
40         super(group);
41         setColorFields(DEFAULTS);
42     }
43
44     /**
45      * Create a new ColorAnimator that processes the specified group and
46      * color field.
47      * @param group the data group to process
48      * @param field the color field to interpolate
49      */

50     public ColorAnimator(String JavaDoc group, String JavaDoc field) {
51         super(group);
52         setColorFields(new String JavaDoc[] {field});
53     }
54     
55     /**
56      * Create a new ColorAnimator that processes the specified group and
57      * color fields.
58      * @param group the data group to process
59      * @param fields the color fields to interpolate
60      */

61     public ColorAnimator(String JavaDoc group, String JavaDoc[] fields) {
62         super(group);
63         setColorFields(fields);
64     }
65     
66     /**
67      * Sets the color fields to interpolate.
68      * @param fields the color fields to interpolate
69      */

70     public void setColorFields(String JavaDoc[] fields) {
71         if ( fields == null ) {
72             throw new IllegalArgumentException JavaDoc();
73         }
74         
75         if ( m_colorFields == null )
76             m_colorFields = new CopyOnWriteArrayList();
77         else
78             m_colorFields.clear();
79         
80         for ( int i=0; i<fields.length; ++i ) {
81             m_colorFields.add(fields[i]);
82             m_colorFields.add(PrefuseLib.getStartField(fields[i]));
83             m_colorFields.add(PrefuseLib.getEndField(fields[i]));
84         }
85     }
86     
87     /**
88      * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
89      */

90     public void process(VisualItem item, double frac) {
91         if ( m_colorFields == null ) return;
92         
93         Object JavaDoc[] fields = m_colorFields.getArray();
94         for ( int i=0; i<fields.length; i += 3 ) {
95             String JavaDoc f = (String JavaDoc)fields[i];
96             String JavaDoc sf = (String JavaDoc)fields[i+1];
97             String JavaDoc ef = (String JavaDoc)fields[i+2];
98             
99             int sc = item.getInt(sf), ec = item.getInt(ef);
100             int cc = ColorLib.interp(sc, ec, frac);
101             item.setInt(f, cc);
102         }
103     }
104
105 } // end of class ColorAnimator
106
Popular Tags