1
2
3
4
5
6
7
8 package edu.uci.ics.jung.algorithms.layout;
9
10 import java.awt.Dimension;
11 import java.awt.geom.Point2D;
12 import java.util.ConcurrentModificationException;
13
14 import org.apache.commons.collections15.Transformer;
15
16 import edu.uci.ics.jung.graph.Graph;
17
18
19
20
21
22
23
24
25
26
27
28
29 public class SpringLayout2<V, E> extends SpringLayout<V,E>
30 {
31 protected int currentIteration;
32 protected int averageCounter;
33 protected int loopCountMax = 4;
34 protected boolean done;
35
36 protected Point2D averageDelta = new Point2D.Double();
37
38
39
40
41
42
43 @SuppressWarnings("unchecked")
44 public SpringLayout2(Graph<V,E> g) {
45 super(g);
46 }
47
48
49
50
51
52
53
54 public SpringLayout2(Graph<V,E> g, Transformer<E, Integer> length_function)
55 {
56 super(g, length_function);
57 }
58
59
60
61
62 @Override
63 public void step() {
64 super.step();
65 currentIteration++;
66 testAverageDeltas();
67 }
68
69 private void testAverageDeltas() {
70 double dx = this.averageDelta.getX();
71 double dy = this.averageDelta.getY();
72 if(Math.abs(dx) < .001 && Math.abs(dy) < .001) {
73 done = true;
74 System.err.println("done, dx="+dx+", dy="+dy);
75 }
76 if(currentIteration > loopCountMax) {
77 this.averageDelta.setLocation(0,0);
78 averageCounter = 0;
79 currentIteration = 0;
80 }
81 }
82
83 @Override
84 protected void moveNodes() {
85 synchronized (getSize()) {
86 try {
87 for (V v : getGraph().getVertices()) {
88 if (isLocked(v)) continue;
89 SpringVertexData vd = springVertexData.get(v);
90 if(vd == null) continue;
91 Point2D xyd = transform(v);
92
93 vd.dx += vd.repulsiondx + vd.edgedx;
94 vd.dy += vd.repulsiondy + vd.edgedy;
95
96
97
98
99
100 averageDelta.setLocation(
101 ((averageDelta.getX() * averageCounter) + vd.dx) / (averageCounter+1),
102 ((averageDelta.getY() * averageCounter) + vd.dy) / (averageCounter+1)
103 );
104
105
106 averageCounter++;
107
108
109 xyd.setLocation(xyd.getX()+Math.max(-5, Math.min(5, vd.dx)),
110 xyd.getY()+Math.max(-5, Math.min(5, vd.dy)));
111
112 Dimension d = getSize();
113 int width = d.width;
114 int height = d.height;
115
116 if (xyd.getX() < 0) {
117 xyd.setLocation(0, xyd.getY());
118 } else if (xyd.getX() > width) {
119 xyd.setLocation(width, xyd.getY());
120 }
121 if (xyd.getY() < 0) {
122 xyd.setLocation(xyd.getX(),0);
123 } else if (xyd.getY() > height) {
124 xyd.setLocation(xyd.getX(), height);
125 }
126
127 }
128 } catch(ConcurrentModificationException cme) {
129 moveNodes();
130 }
131 }
132 }
133
134 @Override
135 public boolean done() {
136 return done;
137 }
138
139 }