1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.visualization.renderers;
11
12 import java.awt.Component;
13 import java.awt.Dimension;
14 import java.awt.Rectangle;
15 import java.awt.Shape;
16 import java.awt.geom.Point2D;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.apache.commons.collections15.Transformer;
21
22 import edu.uci.ics.jung.algorithms.layout.Layout;
23 import edu.uci.ics.jung.graph.Graph;
24 import edu.uci.ics.jung.graph.util.Context;
25 import edu.uci.ics.jung.visualization.Layer;
26 import edu.uci.ics.jung.visualization.RenderContext;
27 import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class VertexLabelAsShapeRenderer<V,E>
41 implements Renderer.VertexLabel<V,E>, Transformer<V,Shape> {
42
43 protected Map<V,Shape> shapes = new HashMap<V,Shape>();
44 protected RenderContext<V,E> rc;
45
46 public VertexLabelAsShapeRenderer(RenderContext<V, E> rc) {
47 this.rc = rc;
48 }
49
50 public Component prepareRenderer(RenderContext<V,E> rc, VertexLabelRenderer graphLabelRenderer, Object value,
51 boolean isSelected, V vertex) {
52 return rc.getVertexLabelRenderer().<V>getVertexLabelRendererComponent(rc.getScreenDevice(), value,
53 rc.getVertexFontTransformer().transform(vertex), isSelected, vertex);
54 }
55
56
57
58
59
60
61
62
63
64 public void labelVertex(RenderContext<V,E> rc, Layout<V,E> layout, V v, String label) {
65 Graph<V,E> graph = layout.getGraph();
66 if (rc.getVertexIncludePredicate().evaluate(Context.<Graph<V,E>,V>getInstance(graph,v)) == false) {
67 return;
68 }
69 GraphicsDecorator g = rc.getGraphicsContext();
70 Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(), label,
71 rc.getPickedVertexState().isPicked(v), v);
72 Dimension d = component.getPreferredSize();
73
74 int h_offset = -d.width / 2;
75 int v_offset = -d.height / 2;
76
77 Point2D p = layout.transform(v);
78 p = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p);
79
80 int x = (int)p.getX();
81 int y = (int)p.getY();
82
83 g.draw(component, rc.getRendererPane(), x+h_offset, y+v_offset, d.width, d.height, true);
84
85 Dimension size = component.getPreferredSize();
86 Rectangle bounds = new Rectangle(-size.width/2 -2, -size.height/2 -2, size.width+4, size.height);
87 shapes.put(v, bounds);
88 }
89
90 public Shape transform(V v) {
91 Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(), rc.getVertexLabelTransformer().transform(v),
92 rc.getPickedVertexState().isPicked(v), v);
93 Dimension size = component.getPreferredSize();
94 Rectangle bounds = new Rectangle(-size.width/2 -2, -size.height/2 -2, size.width+4, size.height);
95 return bounds;
96
97
98
99
100
101 }
102
103 public Renderer.VertexLabel.Position getPosition() {
104 return Renderer.VertexLabel.Position.CNTR;
105 }
106
107 public Renderer.VertexLabel.Positioner getPositioner() {
108 return new Positioner() {
109 public Renderer.VertexLabel.Position getPosition(float x, float y, Dimension d) {
110 return Renderer.VertexLabel.Position.CNTR;
111 }};
112 }
113
114 public void setPosition(Renderer.VertexLabel.Position position) {
115
116 }
117
118 public void setPositioner(Renderer.VertexLabel.Positioner positioner) {
119
120 }
121 }