1
2
3
4
5
6
7
8
9 package edu.uci.ics.jung.samples;
10
11 import java.awt.BorderLayout;
12 import java.awt.Color;
13 import java.awt.Component;
14 import java.awt.Container;
15 import java.awt.Graphics;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18
19 import javax.swing.Icon;
20 import javax.swing.JButton;
21 import javax.swing.JFrame;
22 import javax.swing.JPanel;
23
24 import org.apache.commons.collections15.Transformer;
25
26 import edu.uci.ics.jung.algorithms.layout.FRLayout;
27 import edu.uci.ics.jung.graph.DirectedSparseGraph;
28 import edu.uci.ics.jung.graph.Graph;
29 import edu.uci.ics.jung.graph.util.EdgeType;
30 import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
31 import edu.uci.ics.jung.visualization.VisualizationViewer;
32 import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
33 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
34 import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
35 import edu.uci.ics.jung.visualization.control.ScalingControl;
36 import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
37 import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
38 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
39 import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
40 import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
41
42
43
44
45
46
47
48 public class DrawnIconVertexDemo {
49
50
51
52
53 Graph<Integer,Number> graph;
54
55
56
57
58 VisualizationViewer<Integer,Number> vv;
59
60 public DrawnIconVertexDemo() {
61
62
63 graph = new DirectedSparseGraph<Integer,Number>();
64 Integer[] v = createVertices(10);
65 createEdges(v);
66
67 vv = new VisualizationViewer<Integer,Number>(new FRLayout<Integer,Number>(graph));
68 vv.getRenderContext().setVertexLabelTransformer(new Transformer<Integer,String>(){
69
70 public String transform(Integer v) {
71 return "Vertex "+v;
72 }});
73 vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
74 vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));
75
76 vv.getRenderContext().setVertexIconTransformer(new Transformer<Integer,Icon>() {
77
78
79
80
81
82 public Icon transform(final Integer v) {
83 return new Icon() {
84
85 public int getIconHeight() {
86 return 20;
87 }
88
89 public int getIconWidth() {
90 return 20;
91 }
92
93 public void paintIcon(Component c, Graphics g,
94 int x, int y) {
95 if(vv.getPickedVertexState().isPicked(v)) {
96 g.setColor(Color.yellow);
97 } else {
98 g.setColor(Color.red);
99 }
100 g.fillOval(x, y, 20, 20);
101 if(vv.getPickedVertexState().isPicked(v)) {
102 g.setColor(Color.black);
103 } else {
104 g.setColor(Color.white);
105 }
106 g.drawString(""+v, x+6, y+15);
107
108 }};
109 }});
110
111 vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.white, Color.yellow));
112 vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.lightGray));
113
114 vv.setBackground(Color.white);
115
116
117 vv.setVertexToolTipTransformer(new ToStringLabeller<Integer>());
118
119
120 final JFrame frame = new JFrame();
121 Container content = frame.getContentPane();
122 final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
123 content.add(panel);
124 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
125
126 final ModalGraphMouse gm = new DefaultModalGraphMouse<Integer,Number>();
127 vv.setGraphMouse(gm);
128
129 final ScalingControl scaler = new CrossoverScalingControl();
130
131 JButton plus = new JButton("+");
132 plus.addActionListener(new ActionListener() {
133 public void actionPerformed(ActionEvent e) {
134 scaler.scale(vv, 1.1f, vv.getCenter());
135 }
136 });
137 JButton minus = new JButton("-");
138 minus.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
140 scaler.scale(vv, 1/1.1f, vv.getCenter());
141 }
142 });
143
144 JPanel controls = new JPanel();
145 controls.add(plus);
146 controls.add(minus);
147 controls.add(((DefaultModalGraphMouse<Integer,Number>) gm).getModeComboBox());
148 content.add(controls, BorderLayout.SOUTH);
149
150 frame.pack();
151 frame.setVisible(true);
152 }
153
154
155
156
157
158
159
160 private Integer[] createVertices(int count) {
161 Integer[] v = new Integer[count];
162 for (int i = 0; i < count; i++) {
163 v[i] = new Integer(i);
164 graph.addVertex(v[i]);
165 }
166 return v;
167 }
168
169
170
171
172
173 void createEdges(Integer[] v) {
174 graph.addEdge(new Double(Math.random()), v[0], v[1], EdgeType.DIRECTED);
175 graph.addEdge(new Double(Math.random()), v[0], v[3], EdgeType.DIRECTED);
176 graph.addEdge(new Double(Math.random()), v[0], v[4], EdgeType.DIRECTED);
177 graph.addEdge(new Double(Math.random()), v[4], v[5], EdgeType.DIRECTED);
178 graph.addEdge(new Double(Math.random()), v[3], v[5], EdgeType.DIRECTED);
179 graph.addEdge(new Double(Math.random()), v[1], v[2], EdgeType.DIRECTED);
180 graph.addEdge(new Double(Math.random()), v[1], v[4], EdgeType.DIRECTED);
181 graph.addEdge(new Double(Math.random()), v[8], v[2], EdgeType.DIRECTED);
182 graph.addEdge(new Double(Math.random()), v[3], v[8], EdgeType.DIRECTED);
183 graph.addEdge(new Double(Math.random()), v[6], v[7], EdgeType.DIRECTED);
184 graph.addEdge(new Double(Math.random()), v[7], v[5], EdgeType.DIRECTED);
185 graph.addEdge(new Double(Math.random()), v[0], v[9], EdgeType.DIRECTED);
186 graph.addEdge(new Double(Math.random()), v[9], v[8], EdgeType.DIRECTED);
187 graph.addEdge(new Double(Math.random()), v[7], v[6], EdgeType.DIRECTED);
188 graph.addEdge(new Double(Math.random()), v[6], v[5], EdgeType.DIRECTED);
189 graph.addEdge(new Double(Math.random()), v[4], v[2], EdgeType.DIRECTED);
190 graph.addEdge(new Double(Math.random()), v[5], v[4], EdgeType.DIRECTED);
191 }
192
193
194
195
196 public static void main(String[] args)
197 {
198 new DrawnIconVertexDemo();
199 }
200 }