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 com.google.common.base.Function;
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.ScalingControl;
35 import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
36 import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
37 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
38 import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
39 import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
40
41
42
43
44
45
46
47 public class DrawnIconVertexDemo {
48
49
50
51
52 Graph<Integer,Number> graph;
53
54
55
56
57 VisualizationViewer<Integer,Number> vv;
58
59 public DrawnIconVertexDemo() {
60
61
62 graph = new DirectedSparseGraph<Integer,Number>();
63 Integer[] v = createVertices(10);
64 createEdges(v);
65
66 vv = new VisualizationViewer<Integer,Number>(new FRLayout<Integer,Number>(graph));
67 vv.getRenderContext().setVertexLabelTransformer(new Function<Integer,String>(){
68
69 public String apply(Integer v) {
70 return "Vertex "+v;
71 }});
72 vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
73 vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));
74
75 vv.getRenderContext().setVertexIconTransformer(new Function<Integer,Icon>() {
76
77
78
79
80
81 public Icon apply(final Integer v) {
82 return new Icon() {
83
84 public int getIconHeight() {
85 return 20;
86 }
87
88 public int getIconWidth() {
89 return 20;
90 }
91
92 public void paintIcon(Component c, Graphics g,
93 int x, int y) {
94 if(vv.getPickedVertexState().isPicked(v)) {
95 g.setColor(Color.yellow);
96 } else {
97 g.setColor(Color.red);
98 }
99 g.fillOval(x, y, 20, 20);
100 if(vv.getPickedVertexState().isPicked(v)) {
101 g.setColor(Color.black);
102 } else {
103 g.setColor(Color.white);
104 }
105 g.drawString(""+v, x+6, y+15);
106
107 }};
108 }});
109
110 vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.white, Color.yellow));
111 vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.lightGray));
112
113 vv.setBackground(Color.white);
114
115
116 vv.setVertexToolTipTransformer(new ToStringLabeller());
117
118
119 final JFrame frame = new JFrame();
120 Container content = frame.getContentPane();
121 final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
122 content.add(panel);
123 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124
125 final DefaultModalGraphMouse<Integer, Number> gm
126 = 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(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 public static void main(String[] args)
194 {
195 new DrawnIconVertexDemo();
196 }
197 }