/** * Scales to the given zoom level, 1.0 being 100%, centered on the given point. * * @param level The level to zoom to. * @param center The center point for the scaling operation. * @return The new zoom level. */ public float scaleTo(float level, Point2D center) { if (this.scrollPane == null) return 1.0f; float oldZoom = this.currentZoom; this.currentZoom = Math.max(minimumZoom, level); // this.absoluteViewScaler.scale( this, level, center ); Dimension viewSize; if (level < 1.0f) { viewSize = this.scrollPane.getSize(); } else { viewSize = this.scrollPane.getViewport().getExtentSize(); } Dimension newSize = new Dimension((int) (viewSize.width * currentZoom), (int) (viewSize.height * currentZoom)); this.setPreferredSize(newSize); this.setSize(newSize); // new LayoutScaler( this.getGraphLayout( )).setSize( newSize ); if (Float.compare(level, 1.0f) <= 0) this.center(); // translate the new view position so the mouse is in the same place // on the scaled view. JViewport vp = this.scrollPane.getViewport(); double centerX = center.getX(); double centerY = center.getY(); double viewPortMouseX = centerX - vp.getViewPosition().getX(); double viewPortMouseY = centerY - vp.getViewPosition().getY(); centerX *= currentZoom / oldZoom; centerY *= currentZoom / oldZoom; viewPortMouseX = centerX - viewPortMouseX; viewPortMouseY = centerY - viewPortMouseY; vp.setViewPosition(new Point((int) viewPortMouseX, (int) viewPortMouseY)); return this.currentZoom; }
/** Sets the initial position of the Graph nodes. */ public void initialize() { if (this.sampleGroups != null) { Dimension d = this.getSize(); List<List<V>> moleculeGroups = new ArrayList<List<V>>(); for (int i = 0; i < 3; i++) { moleculeGroups.add(new ArrayList<V>()); } if (d != null) { double height = d.getHeight(); double width = d.getWidth(); String groupName; for (V v : this.getGraph().getVertices()) { if (this.isUpRegulated(sampleGroups, v)) moleculeGroups.get(1).add(v); else if (this.isDownRegulated(sampleGroups, v)) moleculeGroups.get(2).add(v); else moleculeGroups.get(0).add(v); } this.radius = (Math.min(height, width)) * 0.3; int groupRadius = (int) (this.radius / Math.sqrt(moleculeGroups.size())); int j = 0, x, y; Point2D.Double graphCenter = new Point2D.Double(width / 2.0, height / 2.0); PolarPoint2D center = new PolarPoint2D(0, 0, graphCenter); PolarPoint2D coord = new PolarPoint2D(0, 0, center); double theta; for (List<V> group : moleculeGroups) { theta = (2 * Math.PI * j) / moleculeGroups.size(); j++; center.setLocation(this.radius, theta, PolarPoint2D.POLAR); int i = 0; for (V vertex : group) { theta = (2 * Math.PI * i) / group.size(); coord.setLocation(groupRadius, theta, PolarPoint2D.POLAR); this.setLocation(vertex, coord); i++; } } } } }
/** * Changes the size of the underlying Layout. * * @param size The new size for the layout. */ private void setSize(Dimension size) { ObservableCachingLayout<V, E> observableLayout = (ObservableCachingLayout) getGraphLayout(); Layout<V, E> tmpLayout = observableLayout; while (!AbstractLayout.class.isAssignableFrom(tmpLayout.getClass())) tmpLayout = ((LayoutDecorator<V, E>) tmpLayout).getDelegate(); AbstractLayout<V, E> layout = (AbstractLayout<V, E>) tmpLayout; // the first time the graph is resized, re-initialize the layout to make sure it gets // the right size. Any other time, just scale it. The first resize should be when the // graph is made visible and laid out. // this is kind of a hack; there may be a better way to handle this. // I tried listening for componentShown, but it didn't work properly. if (layoutInitialized < 1) { layout.getSize().setSize(size); layout.initialize(); layoutInitialized++; return; } // change the size of the layout without triggering the automatic resizing. double wScale = size.getWidth() / layout.getSize().getWidth(); double hScale = size.getHeight() / layout.getSize().getHeight(); double scale = Math.min(wScale, hScale); layout.getSize().setSize(size); Collection<V> vertices = new Vector(getVertices()); synchronized (graph) { for (V v : vertices) { double x = layout.getX(v) * scale; // Math.min( size.getWidth( ) - 10, Math.max( 10, layout.getX( v ) * // scale )); double y = layout.getY(v) * scale; // Math.min( size.getHeight( ) - 10, Math.max( 10, layout.getY( v ) * // scale )); layout.setLocation(v, new Point2D.Double(x, y)); } } // alert the ObservableLayout that things have changed. observableLayout.fireStateChanged(); }