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.awt.event.ItemEvent;
19 import java.awt.event.ItemListener;
20
21 import javax.swing.BorderFactory;
22 import javax.swing.JApplet;
23 import javax.swing.JButton;
24 import javax.swing.JComboBox;
25 import javax.swing.JFrame;
26 import javax.swing.JMenuBar;
27 import javax.swing.JPanel;
28
29 import edu.uci.ics.jung.algorithms.layout.FRLayout;
30 import edu.uci.ics.jung.algorithms.layout.Layout;
31 import edu.uci.ics.jung.graph.Graph;
32 import edu.uci.ics.jung.graph.util.TestGraphs;
33 import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
34 import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
35 import edu.uci.ics.jung.visualization.VisualizationModel;
36 import edu.uci.ics.jung.visualization.VisualizationViewer;
37 import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
38 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
39 import edu.uci.ics.jung.visualization.control.ScalingControl;
40 import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
41 import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
42 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
43 import edu.uci.ics.jung.visualization.picking.PickedState;
44 import edu.uci.ics.jung.visualization.renderers.Renderer;
45
46
47
48
49
50
51
52
53
54
55 @SuppressWarnings("serial")
56 public class VertexLabelPositionDemo extends JApplet {
57
58
59
60
61 Graph<String,Number> graph;
62
63 Layout<String,Number> graphLayout;
64
65
66
67
68 VisualizationViewer<String,Number> vv;
69
70 ScalingControl scaler;
71
72
73
74
75
76
77 public VertexLabelPositionDemo() {
78
79
80 graph = TestGraphs.getOneComponentGraph();
81
82 graphLayout = new FRLayout<String,Number>(graph);
83 ((FRLayout)graphLayout).setMaxIterations(1000);
84
85 Dimension preferredSize = new Dimension(600,600);
86
87 final VisualizationModel<String,Number> visualizationModel =
88 new DefaultVisualizationModel<String,Number>(graphLayout, preferredSize);
89 vv = new VisualizationViewer<String,Number>(visualizationModel, preferredSize);
90
91 PickedState<String> ps = vv.getPickedVertexState();
92 PickedState<Number> pes = vv.getPickedEdgeState();
93 vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(ps, Color.red, Color.yellow));
94 vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(pes, Color.black, Color.cyan));
95 vv.setBackground(Color.white);
96 vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.W);
97
98 vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
99
100
101 vv.setVertexToolTipTransformer(new ToStringLabeller());
102
103 Container content = getContentPane();
104 GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);
105 content.add(gzsp);
106
107
108
109
110 final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
111
112 vv.setGraphMouse(graphMouse);
113 vv.addKeyListener(graphMouse.getModeKeyListener());
114
115 final ScalingControl scaler = new CrossoverScalingControl();
116
117 JButton plus = new JButton("+");
118 plus.addActionListener(new ActionListener() {
119 public void actionPerformed(ActionEvent e) {
120 scaler.scale(vv, 1.1f, vv.getCenter());
121 }
122 });
123 JButton minus = new JButton("-");
124 minus.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
126 scaler.scale(vv, 1/1.1f, vv.getCenter());
127 }
128 });
129 JPanel positionPanel = new JPanel();
130 positionPanel.setBorder(BorderFactory.createTitledBorder("Label Position"));
131 JMenuBar menubar = new JMenuBar();
132 menubar.add(graphMouse.getModeMenu());
133 gzsp.setCorner(menubar);
134 JComboBox cb = new JComboBox();
135 cb.addItem(Renderer.VertexLabel.Position.N);
136 cb.addItem(Renderer.VertexLabel.Position.NE);
137 cb.addItem(Renderer.VertexLabel.Position.E);
138 cb.addItem(Renderer.VertexLabel.Position.SE);
139 cb.addItem(Renderer.VertexLabel.Position.S);
140 cb.addItem(Renderer.VertexLabel.Position.SW);
141 cb.addItem(Renderer.VertexLabel.Position.W);
142 cb.addItem(Renderer.VertexLabel.Position.NW);
143 cb.addItem(Renderer.VertexLabel.Position.N);
144 cb.addItem(Renderer.VertexLabel.Position.CNTR);
145 cb.addItem(Renderer.VertexLabel.Position.AUTO);
146 cb.addItemListener(new ItemListener() {
147 public void itemStateChanged(ItemEvent e) {
148 Renderer.VertexLabel.Position position =
149 (Renderer.VertexLabel.Position)e.getItem();
150 vv.getRenderer().getVertexLabelRenderer().setPosition(position);
151 vv.repaint();
152 }});
153 cb.setSelectedItem(Renderer.VertexLabel.Position.SE);
154 positionPanel.add(cb);
155 JPanel controls = new JPanel();
156 JPanel zoomControls = new JPanel(new GridLayout(2,1));
157 zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom"));
158 zoomControls.add(plus);
159 zoomControls.add(minus);
160
161 controls.add(zoomControls);
162 controls.add(positionPanel);
163 content.add(controls, BorderLayout.SOUTH);
164 }
165
166
167
168
169 public static void main(String[] args) {
170 JFrame f = new JFrame();
171 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
172 f.getContentPane().add(new VertexLabelPositionDemo());
173 f.pack();
174 f.setVisible(true);
175 }
176 }