KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > layout > CircleLayout


1 package prefuse.action.layout;
2
3 import java.awt.geom.Rectangle2D JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import prefuse.data.tuple.TupleSet;
7 import prefuse.visual.VisualItem;
8
9 /**
10  * Layout action that positions visual items along a circle. By default,
11  * items are sorted in the order in which they iterated over.
12  *
13  * @author <a HREF="http://jheer.org">jeffrey heer</a>
14  */

15 public class CircleLayout extends Layout {
16     
17     private double m_radius; // radius of the circle layout
18

19     /**
20      * Create a CircleLayout; the radius of the circle layout will be computed
21      * automatically based on the display size.
22      * @param group the data group to layout
23      */

24     public CircleLayout(String JavaDoc group) {
25         super(group);
26     }
27     
28     /**
29      * Create a CircleLayout; use the specified radius for the the circle layout,
30      * regardless of the display size.
31      * @param group the data group to layout
32      * @param radius the radius of the circle layout.
33      */

34     public CircleLayout(String JavaDoc group, double radius) {
35         super(group);
36         m_radius = radius;
37     }
38     
39     /**
40      * Return the radius of the layout circle.
41      * @return the circle radius
42      */

43     public double getRadius() {
44         return m_radius;
45     }
46
47     /**
48      * Set the radius of the layout circle.
49      * @param radius the circle radius to use
50      */

51     public void setRadius(double radius) {
52         m_radius = radius;
53     }
54     
55     /**
56      * @see prefuse.action.Action#run(double)
57      */

58     public void run(double frac) {
59         TupleSet ts = m_vis.getGroup(m_group);
60         
61         int nn = ts.getTupleCount();
62         
63         Rectangle2D JavaDoc r = getLayoutBounds();
64         double height = r.getHeight();
65         double width = r.getWidth();
66         double cx = r.getCenterX();
67         double cy = r.getCenterY();
68
69         double radius = m_radius;
70         if (radius <= 0) {
71             radius = 0.45 * (height < width ? height : width);
72         }
73
74         Iterator JavaDoc items = ts.tuples();
75         for (int i=0; items.hasNext(); i++) {
76             VisualItem n = (VisualItem)items.next();
77             double angle = (2*Math.PI*i) / nn;
78             double x = Math.cos(angle)*radius + cx;
79             double y = Math.sin(angle)*radius + cy;
80             setX(n, null, x);
81             setY(n, null, y);
82         }
83     }
84
85 } // end of class CircleLayout
86
Popular Tags