2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
11 * Created on Dec 4, 2003
13 package edu.uci.ics.jung.algorithms.layout;
15 import java.awt.Dimension;
16 import java.awt.geom.Point2D;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.HashMap;
21 import java.util.List;
24 import org.apache.commons.collections15.Factory;
25 import org.apache.commons.collections15.map.LazyMap;
27 import edu.uci.ics.jung.graph.Graph;
32 * A {@code Layout} implementation that positions vertices equally spaced on a regular circle.
34 * @author Masanori Harada
36 public class CircleLayout<V, E> extends AbstractLayout<V,E> {
38 private double radius;
39 private List<V> vertex_ordered_list;
41 Map<V, CircleVertexData> circleVertexDataMap =
42 LazyMap.decorate(new HashMap<V,CircleVertexData>(),
43 new Factory<CircleVertexData>() {
44 public CircleVertexData create() {
45 return new CircleVertexData();
49 * Creates an instance for the specified graph.
51 public CircleLayout(Graph<V,E> g) {
56 * Returns the radius of the circle.
58 public double getRadius() {
63 * Sets the radius of the circle. Must be called before
64 * {@code initialize()} is called.
66 public void setRadius(double radius) {
71 * Sets the order of the vertices in the layout according to the ordering
72 * specified by {@code comparator}.
74 public void setVertexOrder(Comparator<V> comparator)
76 if (vertex_ordered_list == null)
77 vertex_ordered_list = new ArrayList<V>(getGraph().getVertices());
78 Collections.sort(vertex_ordered_list, comparator);
82 * Sets the order of the vertices in the layout according to the ordering
83 * of {@code vertex_list}.
85 public void setVertexOrder(List<V> vertex_list)
87 if (!vertex_list.containsAll(getGraph().getVertices()))
88 throw new IllegalArgumentException("Supplied list must include " +
89 "all vertices of the graph");
90 this.vertex_ordered_list = vertex_list;
97 public void initialize()
99 Dimension d = getSize();
103 if (vertex_ordered_list == null)
104 setVertexOrder(new ArrayList<V>(getGraph().getVertices()));
106 double height = d.getHeight();
107 double width = d.getWidth();
110 radius = 0.45 * (height < width ? height : width);
114 for (V v : vertex_ordered_list)
116 Point2D coord = transform(v);
118 double angle = (2 * Math.PI * i) / vertex_ordered_list.size();
120 coord.setLocation(Math.cos(angle) * radius + width / 2,
121 Math.sin(angle) * radius + height / 2);
123 CircleVertexData data = getCircleData(v);
124 data.setAngle(angle);
130 protected CircleVertexData getCircleData(V v) {
131 return circleVertexDataMap.get(v);
134 protected static class CircleVertexData {
135 private double angle;
137 protected double getAngle() {
141 protected void setAngle(double angle) {
146 public String toString() {
147 return "CircleVertexData: angle=" + angle;