Advertisement
Guest User

Untitled

a guest
Jan 10th, 2011
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2003, the JUNG Project and the Regents of the University
  3.  * of California
  4.  * All rights reserved.
  5.  *
  6.  * This software is open-source under the BSD license; see either
  7.  * "license.txt" or
  8.  * http://jung.sourceforge.net/license.txt for a description.
  9.  */
  10. /*
  11.  * Created on Dec 4, 2003
  12.  */
  13. package jung;
  14.  
  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;
  22. import java.util.Map;
  23.  
  24. import org.apache.commons.collections15.Factory;
  25. import org.apache.commons.collections15.map.LazyMap;
  26.  
  27. import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
  28. import edu.uci.ics.jung.graph.Graph;
  29.  
  30.  
  31.  
  32. /**
  33.  * A {@code Layout} implementation that positions vertices equally spaced on a regular circle.
  34.  *
  35.  * @author Masanori Harada
  36.  */
  37. public class AntHillLayout<V, E> extends AbstractLayout<V,E> {
  38.  
  39.     private double radius;
  40.     private List<V> vertex_ordered_list;
  41.     private V antHill;
  42.    
  43.     Map<V, CircleVertexData> circleVertexDataMap =
  44.             LazyMap.decorate(new HashMap<V,CircleVertexData>(),
  45.             new Factory<CircleVertexData>() {
  46.                 public CircleVertexData create() {
  47.                     return new CircleVertexData();
  48.                 }});   
  49.  
  50.     /**
  51.      * Creates an instance for the specified graph.
  52.      */
  53.     public AntHillLayout(Graph<V,E> g, V antHill) {
  54.         super(g);
  55.         this.antHill = antHill;
  56.     }
  57.  
  58.     /**
  59.      * Returns the radius of the circle.
  60.      */
  61.     public double getRadius() {
  62.         return radius;
  63.     }
  64.  
  65.     /**
  66.      * Sets the radius of the circle.  Must be called before
  67.      * {@code initialize()} is called.
  68.      */
  69.     public void setRadius(double radius) {
  70.         this.radius = radius;
  71.     }
  72.  
  73.     /**
  74.      * Sets the order of the vertices in the layout according to the ordering
  75.      * specified by {@code comparator}.
  76.      */
  77.     public void setVertexOrder(Comparator<V> comparator)
  78.     {
  79.         if (vertex_ordered_list == null)
  80.             vertex_ordered_list = new ArrayList<V>(getGraph().getVertices());
  81.         Collections.sort(vertex_ordered_list, comparator);
  82.     }
  83.  
  84.     /**
  85.      * Sets the order of the vertices in the layout according to the ordering
  86.      * of {@code vertex_list}.
  87.      */
  88.     public void setVertexOrder(List<V> vertex_list)
  89.     {
  90.         if (!vertex_list.containsAll(getGraph().getVertices()))
  91.             throw new IllegalArgumentException("Supplied list must include " +
  92.                     "all vertices of the graph");
  93.         this.vertex_ordered_list = vertex_list;
  94.     }
  95.    
  96.     public void reset() {
  97.         initialize();
  98.     }
  99.  
  100.     public void initialize()
  101.     {
  102.        
  103.         Dimension d = getSize();
  104.        
  105.        
  106.         if (d != null)
  107.         {
  108.             if (vertex_ordered_list == null)
  109.                 setVertexOrder(new ArrayList<V>(getGraph().getVertices()));
  110.  
  111.            
  112.             double height = d.getHeight();
  113.             double width = d.getWidth();
  114.  
  115.             if (radius <= 0) {
  116.                 radius = 0.45 * (height < width ? height : width);
  117.             }
  118.  
  119.             int i = 0;
  120.             for (V v : vertex_ordered_list)
  121.             {
  122.                
  123.                 Point2D coord = transform(v);
  124.                
  125.                 if(v.equals(antHill)) {
  126.                     coord.setLocation(width/2, height/2);
  127.                     continue;
  128.                 }
  129.                
  130.                 double angle = (2 * Math.PI * i) / (vertex_ordered_list.size() - 1);
  131.  
  132.                 coord.setLocation(Math.cos(angle) * radius + width / 2,
  133.                         Math.sin(angle) * radius + height / 2);
  134.  
  135.                 CircleVertexData data = getCircleData(v);
  136.                 data.setAngle(angle);
  137.                 i++;
  138.             }
  139.         }
  140.     }
  141.  
  142.     protected CircleVertexData getCircleData(V v) {
  143.         return circleVertexDataMap.get(v);
  144.     }
  145.  
  146.     protected static class CircleVertexData {
  147.         private double angle;
  148.  
  149.         protected double getAngle() {
  150.             return angle;
  151.         }
  152.  
  153.         protected void setAngle(double angle) {
  154.             this.angle = angle;
  155.         }
  156.  
  157.         @Override
  158.         public String toString() {
  159.             return "CircleVertexData: angle=" + angle;
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement