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.Container;
14 import java.awt.Dimension;
15 import java.awt.GridLayout;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.net.URL;
19
20 import javax.swing.BorderFactory;
21 import javax.swing.JApplet;
22 import javax.swing.JButton;
23 import javax.swing.JComboBox;
24 import javax.swing.JFrame;
25 import javax.swing.JPanel;
26
27 import org.apache.commons.collections15.Transformer;
28
29 import edu.uci.ics.jung.algorithms.layout.FRLayout;
30 import edu.uci.ics.jung.graph.DirectedGraph;
31 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
32 import edu.uci.ics.jung.graph.util.EdgeType;
33 import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
34 import edu.uci.ics.jung.visualization.VisualizationViewer;
35 import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
36 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
37 import edu.uci.ics.jung.visualization.control.ScalingControl;
38 import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
39 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
40 import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
41 import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
42
43
44
45
46
47
48
49 public class ImageEdgeLabelDemo extends JApplet {
50
51
52
53
54 private static final long serialVersionUID = -4332663871914930864L;
55
56 private static final int VERTEX_COUNT=11;
57
58
59
60
61 DirectedGraph<Number, Number> graph;
62
63
64
65
66 VisualizationViewer<Number, Number> vv;
67
68 public ImageEdgeLabelDemo() {
69
70
71 graph = new DirectedSparseMultigraph<Number,Number>();
72 createGraph(VERTEX_COUNT);
73
74 FRLayout<Number, Number> layout = new FRLayout<Number, Number>(graph);
75 layout.setMaxIterations(100);
76 vv = new VisualizationViewer<Number, Number>(layout, new Dimension(400,400));
77
78 vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.cyan));
79
80 vv.setBackground(Color.white);
81
82 vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
83 vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));
84 vv.getRenderContext().setEdgeLabelTransformer(new Transformer<Number,String>() {
85 URL url = getClass().getResource("/images/lightning-s.gif");
86 public String transform(Number input) {
87 return "<html><img src="+url+" height=10 width=21>";
88 }});
89
90
91 vv.setVertexToolTipTransformer(new ToStringLabeller<Number>());
92 vv.setEdgeToolTipTransformer(new ToStringLabeller<Number>());
93 Container content = getContentPane();
94 final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
95 content.add(panel);
96
97 final DefaultModalGraphMouse<Number, Number> graphMouse = new DefaultModalGraphMouse<Number, Number>();
98 vv.setGraphMouse(graphMouse);
99 vv.addKeyListener(graphMouse.getModeKeyListener());
100 final ScalingControl scaler = new CrossoverScalingControl();
101
102 JButton plus = new JButton("+");
103 plus.addActionListener(new ActionListener() {
104 public void actionPerformed(ActionEvent e) {
105 scaler.scale(vv, 1.1f, vv.getCenter());
106 }
107 });
108 JButton minus = new JButton("-");
109 minus.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 scaler.scale(vv, 1/1.1f, vv.getCenter());
112 }
113 });
114
115 JComboBox modeBox = graphMouse.getModeComboBox();
116 JPanel modePanel = new JPanel();
117 modePanel.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
118 modePanel.add(modeBox);
119
120 JPanel scaleGrid = new JPanel(new GridLayout(1,0));
121 scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));
122 JPanel controls = new JPanel();
123 scaleGrid.add(plus);
124 scaleGrid.add(minus);
125 controls.add(scaleGrid);
126 controls.add(modePanel);
127 content.add(controls, BorderLayout.SOUTH);
128 }
129
130
131
132
133
134
135 private void createGraph(int vertexCount) {
136 for (int i = 0; i < vertexCount; i++) {
137 graph.addVertex(i);
138 }
139 int j=0;
140 graph.addEdge(j++, 0, 1, EdgeType.DIRECTED);
141 graph.addEdge(j++, 3, 0, EdgeType.DIRECTED);
142 graph.addEdge(j++, 0, 4, EdgeType.DIRECTED);
143 graph.addEdge(j++, 4, 5, EdgeType.DIRECTED);
144 graph.addEdge(j++, 5, 3, EdgeType.DIRECTED);
145 graph.addEdge(j++, 2, 1, EdgeType.DIRECTED);
146 graph.addEdge(j++, 4, 1, EdgeType.DIRECTED);
147 graph.addEdge(j++, 8, 2, EdgeType.DIRECTED);
148 graph.addEdge(j++, 3, 8, EdgeType.DIRECTED);
149 graph.addEdge(j++, 6, 7, EdgeType.DIRECTED);
150 graph.addEdge(j++, 7, 5, EdgeType.DIRECTED);
151 graph.addEdge(j++, 0, 9, EdgeType.DIRECTED);
152 graph.addEdge(j++, 9, 8, EdgeType.DIRECTED);
153 graph.addEdge(j++, 7, 6, EdgeType.DIRECTED);
154 graph.addEdge(j++, 6, 5, EdgeType.DIRECTED);
155 graph.addEdge(j++, 4, 2, EdgeType.DIRECTED);
156 graph.addEdge(j++, 5, 4, EdgeType.DIRECTED);
157 graph.addEdge(j++, 4, 10, EdgeType.DIRECTED);
158 graph.addEdge(j++, 10, 4, EdgeType.DIRECTED);
159 }
160
161
162
163
164 public static void main(String[] args) {
165 JFrame frame = new JFrame();
166 Container content = frame.getContentPane();
167 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
168
169 content.add(new ImageEdgeLabelDemo());
170 frame.pack();
171 frame.setVisible(true);
172 }
173 }