protected void setRootPolars() { List<V> roots = TreeUtils.getRoots(graph); if(roots.size() == 1) { // its a Tree V root = roots.get(0); setRootPolar(root); setPolars(new ArrayList<V>(graph.getChildren(root)), getCenter(), getSize().width/2); } else if (roots.size() > 1) { // its a Forest setPolars(roots, getCenter(), getSize().width/2); } }
@Override public void setSize(Dimension size) { this.size = size; setRootPolars(); }
/** * @param v the vertex whose center is to be returned * @return the coordinates of {@code v}'s parent, or the center of this layout's area if it's a root. */ public Point2D getCenter(V v) { V parent = graph.getParent(v); if(parent == null) { return getCenter(); } return locations.getUnchecked(parent); }
public void paint(Graphics g) { g.setColor(Color.gray); Graphics2D g2d = (Graphics2D)g; Ellipse2D ellipse = new Ellipse2D.Double(); for(String v : layout.getGraph().getVertices()) { Double radius = layout.getRadii().get(v); if(radius == null) continue; Point2D p = layout.apply(v); ellipse.setFrame(-radius, -radius, 2*radius, 2*radius); AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY()); Shape shape = at.createTransformedShape(ellipse); MutableTransformer viewTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW); if(viewTransformer instanceof MutableTransformerDecorator) { shape = vv.getRenderContext().getMultiLayerTransformer().transform(shape); } else { shape = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT,shape); } g2d.draw(shape); } }
protected void setPolars(List<V> kids, Point2D parentLocation, double parentRadius) { int childCount = kids.size(); if(childCount == 0) return; // handle the 1-child case with 0 limit on angle. double angle = Math.max(0, Math.PI / 2 * (1 - 2.0/childCount)); double childRadius = parentRadius*Math.cos(angle) / (1 + Math.cos(angle)); double radius = parentRadius - childRadius; double rand = Math.random(); for(int i=0; i< childCount; i++) { V child = kids.get(i); double theta = i* 2*Math.PI/childCount + rand; radii.put(child, childRadius); PolarPoint pp = new PolarPoint(theta, radius); polarLocations.put(child, pp); Point2D p = PolarPoint.polarToCartesian(pp); p.setLocation(p.getX()+parentLocation.getX(), p.getY()+parentLocation.getY()); locations.put(child, p); setPolars(new ArrayList<V>(graph.getChildren(child)), p, childRadius); } }
radialLayout = new BalloonLayout<String,Integer>(graph); radialLayout.setSize(new Dimension(900,900)); vv = new VisualizationViewer<String,Integer>(layout, new Dimension(600,600)); vv.setBackground(Color.white);
protected void setRootPolar(V root) { PolarPoint pp = new PolarPoint(0,0); Point2D p = getCenter(); polarLocations.put(root, pp); locations.put(root, p); }
protected void setPolars(List<V> kids, Point2D parentLocation, double parentRadius) { int childCount = kids.size(); if(childCount == 0) return; // handle the 1-child case with 0 limit on angle. double angle = Math.max(0, Math.PI / 2 * (1 - 2.0/childCount)); double childRadius = parentRadius*Math.cos(angle) / (1 + Math.cos(angle)); double radius = parentRadius - childRadius; double rand = Math.random(); for(int i=0; i< childCount; i++) { V child = kids.get(i); double theta = i* 2*Math.PI/childCount + rand; radii.put(child, childRadius); PolarPoint pp = new PolarPoint(theta, radius); polarLocations.put(child, pp); Point2D p = PolarPoint.polarToCartesian(pp); p.setLocation(p.getX()+parentLocation.getX(), p.getY()+parentLocation.getY()); locations.put(child, p); setPolars(new ArrayList<V>(graph.getChildren(child)), p, childRadius); } }
protected void setRootPolars() { List<V> roots = TreeUtils.getRoots(graph); if(roots.size() == 1) { // its a Tree V root = roots.get(0); setRootPolar(root); setPolars(new ArrayList<V>(graph.getChildren(root)), getCenter(), getSize().width/2); } else if (roots.size() > 1) { // its a Forest setPolars(roots, getCenter(), getSize().width/2); } }
/** * Returns the coordinates of {@code v}'s parent, or the * center of this layout's area if it's a root. */ public Point2D getCenter(V v) { V parent = graph.getParent(v); if(parent == null) { return getCenter(); } return locations.get(parent); }
@Override public void setSize(Dimension size) { this.size = size; setRootPolars(); }
@Override public void setLocation(V v, Point2D location) { Point2D c = getCenter(v); Point2D pv = new Point2D.Double(location.getX()-c.getX(),location.getY()-c.getY()); PolarPoint newLocation = PolarPoint.cartesianToPolar(pv); polarLocations.get(v).setLocation(newLocation); Point2D center = getCenter(v); pv.setLocation(pv.getX()+center.getX(), pv.getY()+center.getY()); locations.put(v, pv); }
@Override public void setLocation(V v, Point2D location) { Point2D c = getCenter(v); Point2D pv = new Point2D.Double(location.getX()-c.getX(),location.getY()-c.getY()); PolarPoint newLocation = PolarPoint.cartesianToPolar(pv); polarLocations.getUnchecked(v).setLocation(newLocation); Point2D center = getCenter(v); pv.setLocation(pv.getX()+center.getX(), pv.getY()+center.getY()); locations.put(v, pv); }
protected void setRootPolar(V root) { PolarPoint pp = new PolarPoint(0,0); Point2D p = getCenter(); polarLocations.put(root, pp); locations.put(root, p); }