1 package prefuse.util.display; 2 3 import java.awt.geom.Point2D ; 4 import java.awt.geom.Rectangle2D ; 5 import java.util.Iterator ; 6 7 import prefuse.Display; 8 import prefuse.visual.VisualItem; 9 10 11 16 public class DisplayLib { 17 18 private DisplayLib() { 19 } 21 22 30 public static Rectangle2D getBounds( 31 Iterator iter, double margin, Rectangle2D b) 32 { 33 b.setFrame(Double.NaN,Double.NaN,Double.NaN,Double.NaN); 34 if ( iter.hasNext() ) { 36 VisualItem item = (VisualItem)iter.next(); 37 Rectangle2D nb = item.getBounds(); 38 b.setFrame(nb); 39 } 40 while ( iter.hasNext() ) { 41 VisualItem item = (VisualItem)iter.next(); 42 Rectangle2D 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 63 public static Rectangle2D getBounds(Iterator iter, double margin) 64 { 65 Rectangle2D b = new Rectangle2D.Double (); 66 return getBounds(iter, margin, b); 67 } 68 69 76 public static Point2D getCentroid(Iterator iter, Point2D 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 105 public static Point2D getCentroid(Iterator iter) 106 { 107 return getCentroid(iter, new Point2D.Double ()); 108 } 109 110 117 public static void fitViewToBounds(Display display, Rectangle2D bounds, 118 long duration) 119 { 120 fitViewToBounds(display, bounds, null, duration); 121 } 122 123 132 public static void fitViewToBounds(Display display, Rectangle2D bounds, 133 Point2D center, long duration) 134 { 135 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 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 double scale = Math.min(w/(2*wb),h/(2*hb)) / display.getScale(); 151 152 if ( center == null ) 154 center = new Point2D.Double (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 } | Popular Tags |