1
2
3
4
5
6
7
8
9 package edu.uci.ics.jung.samples;
10
11 import java.awt.Color;
12 import java.awt.Container;
13 import java.awt.Dimension;
14 import java.awt.Image;
15 import java.awt.geom.Point2D;
16
17 import javax.swing.Icon;
18 import javax.swing.ImageIcon;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
21
22 import org.apache.commons.collections15.functors.ConstantTransformer;
23
24 import edu.uci.ics.jung.algorithms.layout.KKLayout;
25 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
26 import edu.uci.ics.jung.graph.util.EdgeType;
27 import edu.uci.ics.jung.visualization.VisualizationImageServer;
28 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
29 import edu.uci.ics.jung.visualization.renderers.GradientVertexRenderer;
30 import edu.uci.ics.jung.visualization.renderers.Renderer;
31 import edu.uci.ics.jung.visualization.renderers.BasicVertexLabelRenderer.InsidePositioner;
32
33
34
35
36
37
38
39 public class VisualizationImageServerDemo {
40
41
42
43
44 DirectedSparseMultigraph<String, Number> graph;
45
46
47
48
49 VisualizationImageServer<String, Number> vv;
50
51
52
53
54 public VisualizationImageServerDemo() {
55
56
57 graph = new DirectedSparseMultigraph<String, Number>();
58 String[] v = createVertices(10);
59 createEdges(v);
60
61 vv = new VisualizationImageServer<String,Number>(new KKLayout<String,Number>(graph), new Dimension(600,600));
62
63 vv.getRenderer().setVertexRenderer(
64 new GradientVertexRenderer<String,Number>(
65 Color.white, Color.red,
66 Color.white, Color.blue,
67 vv.getPickedVertexState(),
68 false));
69 vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.lightGray));
70 vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
71 vv.getRenderContext().setArrowDrawPaintTransformer(new ConstantTransformer(Color.lightGray));
72
73 vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
74 vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
75 vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);
76
77
78 final JFrame frame = new JFrame();
79 Container content = frame.getContentPane();
80
81 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82
83 Image im = vv.getImage(new Point2D.Double(300,300), new Dimension(600,600));
84 Icon icon = new ImageIcon(im);
85 JLabel label = new JLabel(icon);
86 content.add(label);
87 frame.pack();
88 frame.setVisible(true);
89 }
90
91
92
93
94
95
96 private String[] createVertices(int count) {
97 String[] v = new String[count];
98 for (int i = 0; i < count; i++) {
99 v[i] = "V"+i;
100 graph.addVertex(v[i]);
101 }
102 return v;
103 }
104
105
106
107
108
109 void createEdges(String[] v) {
110 graph.addEdge(new Double(Math.random()), v[0], v[1], EdgeType.DIRECTED);
111 graph.addEdge(new Double(Math.random()), v[0], v[3], EdgeType.DIRECTED);
112 graph.addEdge(new Double(Math.random()), v[0], v[4], EdgeType.DIRECTED);
113 graph.addEdge(new Double(Math.random()), v[4], v[5], EdgeType.DIRECTED);
114 graph.addEdge(new Double(Math.random()), v[3], v[5], EdgeType.DIRECTED);
115 graph.addEdge(new Double(Math.random()), v[1], v[2], EdgeType.DIRECTED);
116 graph.addEdge(new Double(Math.random()), v[1], v[4], EdgeType.DIRECTED);
117 graph.addEdge(new Double(Math.random()), v[8], v[2], EdgeType.DIRECTED);
118 graph.addEdge(new Double(Math.random()), v[3], v[8], EdgeType.DIRECTED);
119 graph.addEdge(new Double(Math.random()), v[6], v[7], EdgeType.DIRECTED);
120 graph.addEdge(new Double(Math.random()), v[7], v[5], EdgeType.DIRECTED);
121 graph.addEdge(new Double(Math.random()), v[0], v[9], EdgeType.DIRECTED);
122 graph.addEdge(new Double(Math.random()), v[9], v[8], EdgeType.DIRECTED);
123 graph.addEdge(new Double(Math.random()), v[7], v[6], EdgeType.DIRECTED);
124 graph.addEdge(new Double(Math.random()), v[6], v[5], EdgeType.DIRECTED);
125 graph.addEdge(new Double(Math.random()), v[4], v[2], EdgeType.DIRECTED);
126 graph.addEdge(new Double(Math.random()), v[5], v[4], EdgeType.DIRECTED);
127 }
128
129
130
131
132 public static void main(String[] args)
133 {
134 new VisualizationImageServerDemo();
135
136 }
137 }