1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.visualization.renderers;
11
12 import java.awt.Shape;
13 import java.awt.geom.AffineTransform;
14 import java.awt.geom.GeneralPath;
15 import java.awt.geom.Line2D;
16 import java.awt.geom.PathIterator;
17 import java.awt.geom.Point2D;
18
19 import edu.uci.ics.jung.visualization.RenderContext;
20
21 public class CenterEdgeArrowRenderingSupport<V,E> implements EdgeArrowRenderingSupport<V,E> {
22
23
24
25
26
27 public AffineTransform getArrowTransform(RenderContext<V,E> rc, Shape edgeShape, Shape vertexShape) {
28 GeneralPath path = new GeneralPath(edgeShape);
29 float[] seg = new float[6];
30 Point2D p1=null;
31 Point2D p2=null;
32 AffineTransform at = new AffineTransform();
33
34 int middleSegment = 0;
35 int current = 0;
36 for(PathIterator i=path.getPathIterator(null,1); !i.isDone(); i.next()) {
37 current++;
38 }
39 middleSegment = current/2;
40
41 current = 0;
42 for(PathIterator i=path.getPathIterator(null,1); !i.isDone(); i.next()) {
43 current++;
44 int ret = i.currentSegment(seg);
45 if(ret == PathIterator.SEG_MOVETO) {
46 p2 = new Point2D.Float(seg[0],seg[1]);
47 } else if(ret == PathIterator.SEG_LINETO) {
48 p1 = p2;
49 p2 = new Point2D.Float(seg[0],seg[1]);
50 }
51 if(current > middleSegment) {
52 at = getArrowTransform(rc, new Line2D.Float(p1,p2),vertexShape);
53 break;
54 }
55
56 }
57 return at;
58 }
59
60
61
62
63
64 public AffineTransform getReverseArrowTransform(RenderContext<V,E> rc, Shape edgeShape, Shape vertexShape) {
65 return getReverseArrowTransform(rc, edgeShape, vertexShape, true);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79 public AffineTransform getReverseArrowTransform(RenderContext<V,E> rc, Shape edgeShape, Shape vertexShape,
80 boolean passedGo) {
81 GeneralPath path = new GeneralPath(edgeShape);
82 float[] seg = new float[6];
83 Point2D p1=null;
84 Point2D p2=null;
85 AffineTransform at = new AffineTransform();
86
87 int middleSegment = 0;
88 int current = 0;
89 for(PathIterator i=path.getPathIterator(null,1); !i.isDone(); i.next()) {
90 current++;
91 }
92 middleSegment = current/2;
93
94 current = 0;
95 for(PathIterator i=path.getPathIterator(null,1); !i.isDone(); i.next()) {
96 current++;
97 int ret = i.currentSegment(seg);
98 if(ret == PathIterator.SEG_MOVETO) {
99 p2 = new Point2D.Float(seg[0],seg[1]);
100 } else if(ret == PathIterator.SEG_LINETO) {
101 p1 = p2;
102 p2 = new Point2D.Float(seg[0],seg[1]);
103 }
104 if(current > middleSegment) {
105 at = getReverseArrowTransform(rc, new Line2D.Float(p1,p2),vertexShape);
106 break;
107 }
108 }
109 return at;
110 }
111
112
113
114
115
116
117
118
119
120
121 public AffineTransform getArrowTransform(RenderContext<V,E> rc, Line2D edgeShape, Shape vertexShape) {
122
123
124 Line2D left = new Line2D.Float();
125 Line2D right = new Line2D.Float();
126 this.subdivide(edgeShape, left, right);
127 edgeShape = right;
128 float dx = (float) (edgeShape.getX1()-edgeShape.getX2());
129 float dy = (float) (edgeShape.getY1()-edgeShape.getY2());
130 double atheta = Math.atan2(dx,dy)+Math.PI/2;
131 AffineTransform at =
132 AffineTransform.getTranslateInstance(edgeShape.getX1(), edgeShape.getY1());
133 at.rotate(-atheta);
134 return at;
135 }
136
137
138
139
140
141
142
143
144
145 protected AffineTransform getReverseArrowTransform(RenderContext<V,E> rc, Line2D edgeShape, Shape vertexShape) {
146
147 Line2D left = new Line2D.Float();
148 Line2D right = new Line2D.Float();
149 this.subdivide(edgeShape, left, right);
150 edgeShape = right;
151 float dx = (float) (edgeShape.getX1()-edgeShape.getX2());
152 float dy = (float) (edgeShape.getY1()-edgeShape.getY2());
153
154 double atheta = Math.atan2(dx,dy)-Math.PI/2;
155 AffineTransform at = AffineTransform.getTranslateInstance(edgeShape.getX1(),edgeShape.getY1());
156 at.rotate(-atheta);
157 return at;
158 }
159
160
161
162
163
164
165
166
167 protected void subdivide(Line2D src,
168 Line2D left,
169 Line2D right) {
170 double x1 = src.getX1();
171 double y1 = src.getY1();
172 double x2 = src.getX2();
173 double y2 = src.getY2();
174
175 double mx = x1 + (x2-x1)/2.0;
176 double my = y1 + (y2-y1)/2.0;
177 if (left != null) {
178 left.setLine(x1, y1, mx, my);
179 }
180 if (right != null) {
181 right.setLine(mx, my, x2, y2);
182 }
183 }
184
185 }