KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > display > DisplayLib


1 package prefuse.util.display;
2
3 import java.awt.geom.Point2D JavaDoc;
4 import java.awt.geom.Rectangle2D JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import prefuse.Display;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * Library routines pertaining to a prefuse Display.
13  *
14  * @author <a HREF="http://jheer.org">jeffrey heer</a>
15  */

16 public class DisplayLib {
17
18     private DisplayLib() {
19         // don't instantiate
20
}
21     
22     /**
23      * Get a bounding rectangle of the VisualItems in the input iterator.
24      * @param iter an iterator of VisualItems
25      * @param margin a margin to add on to the bounding rectangle
26      * @param b the Rectangle instance in which to store the result
27      * @return the bounding rectangle. This is the same object as the
28      * parameter <code>b</code>.
29      */

30     public static Rectangle2D JavaDoc getBounds(
31         Iterator JavaDoc iter, double margin, Rectangle2D JavaDoc b)
32     {
33         b.setFrame(Double.NaN,Double.NaN,Double.NaN,Double.NaN);
34         // TODO: synchronization?
35
if ( iter.hasNext() ) {
36             VisualItem item = (VisualItem)iter.next();
37             Rectangle2D JavaDoc nb = item.getBounds();
38             b.setFrame(nb);
39         }
40         while ( iter.hasNext() ) {
41             VisualItem item = (VisualItem)iter.next();
42             Rectangle2D JavaDoc nb = item.getBounds();
43             double x1 = (nb.getMinX()<b.getMinX() ? nb.getMinX() : b.getMinX());
44             double x2 = (nb.getMaxX()>b.getMaxX() ? nb.getMaxX() : b.getMaxX());
45             double y1 = (nb.getMinY()<b.getMinY() ? nb.getMinY() : b.getMinY());
46             double y2 = (nb.getMaxY()>b.getMaxY() ? nb.getMaxY() : b.getMaxY());
47             b.setFrame(x1,y1,x2-x1,y2-y1);
48         }
49         b.setFrame(b.getMinX() - margin,
50                    b.getMinY() - margin,
51                    b.getWidth() + 2*margin,
52                    b.getHeight() + 2*margin);
53         return b;
54     }
55     
56     /**
57      * Get a bounding rectangle of the VisualItems in the input iterator.
58      * @param iter an iterator of VisualItems
59      * @param margin a margin to add on to the bounding rectangle
60      * @return the bounding rectangle. A new Rectangle2D instance is
61      * allocated and returned.
62      */

63     public static Rectangle2D JavaDoc getBounds(Iterator JavaDoc iter, double margin)
64     {
65         Rectangle2D JavaDoc b = new Rectangle2D.Double JavaDoc();
66         return getBounds(iter, margin, b);
67     }
68     
69     /**
70      * Return the centroid (averaged location) of a group of items.
71      * @param iter an iterator of VisualItems
72      * @param p a Point2D instance in which to store the result
73      * @return the centroid point. This is the same object as the
74      * parameter <code>p</code>.
75      */

76     public static Point2D JavaDoc getCentroid(Iterator JavaDoc iter, Point2D JavaDoc p) {
77         double cx = 0, cy = 0;
78         int count = 0;
79         
80         while ( iter.hasNext() ) {
81             VisualItem item = (VisualItem)iter.next();
82             double x = item.getX(), y = item.getY();
83             if ( !(Double.isInfinite(x) || Double.isNaN(x)) &&
84                  !(Double.isInfinite(y) || Double.isNaN(y)) )
85             {
86                 cx += x;
87                 cy += y;
88                 count++;
89             }
90         }
91         if ( count > 0 ) {
92             cx /= count;
93             cy /= count;
94         }
95         p.setLocation(cx, cy);
96         return p;
97     }
98     
99     /**
100      * Return the centroid (averaged location) of a group of items.
101      * @param iter an iterator of VisualItems
102      * @return the centroid point. A new Point2D instance is allocated
103      * and returned.
104      */

105     public static Point2D JavaDoc getCentroid(Iterator JavaDoc iter)
106     {
107         return getCentroid(iter, new Point2D.Double JavaDoc());
108     }
109     
110     /**
111      * Set the display view such that the given bounds are within view.
112      * @param display the Display instance
113      * @param bounds the bounds that should be visible in the Display view
114      * @param duration the duration of an animated transition. A value of
115      * zero will result in an instantaneous change.
116      */

117     public static void fitViewToBounds(Display display, Rectangle2D JavaDoc bounds,
118             long duration)
119     {
120         fitViewToBounds(display, bounds, null, duration);
121     }
122
123     /**
124      * Set the display view such that the given bounds are within view, subject
125      * to a given center point being maintained.
126      * @param display the Display instance
127      * @param bounds the bounds that should be visible in the Display view
128      * @param center the point that should be the center of the Display
129      * @param duration the duration of an animated transition. A value of
130      * zero will result in an instantaneous change.
131      */

132     public static void fitViewToBounds(Display display, Rectangle2D JavaDoc bounds,
133             Point2D JavaDoc center, long duration)
134     {
135         // init variables
136
double w = display.getWidth(), h = display.getHeight();
137         double cx = (center==null? bounds.getCenterX() : center.getX());
138         double cy = (center==null? bounds.getCenterY() : center.getY());
139         
140         // compute half-widths of final bounding box around
141
// the desired center point
142
double wb = Math.max(cx-bounds.getMinX(),
143                              bounds.getMaxX()-cx);
144         double hb = Math.max(cy-bounds.getMinY(),
145                              bounds.getMaxY()-cy);
146         
147         // compute scale factor
148
// - figure out if z or y dimension takes priority
149
// - then balance against the current scale factor
150
double scale = Math.min(w/(2*wb),h/(2*hb)) / display.getScale();
151
152         // animate to new display settings
153
if ( center == null )
154             center = new Point2D.Double JavaDoc(cx,cy);
155         if ( duration > 0 ) {
156             display.animatePanAndZoomToAbs(center,scale,duration);
157         } else {
158             display.panToAbs(center);
159             display.zoomAbs(center, scale);
160         }
161     }
162     
163 } // end of class DisplayLib
164
Popular Tags