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
19 import javax.swing.BorderFactory;
20 import javax.swing.JApplet;
21 import javax.swing.JButton;
22 import javax.swing.JFrame;
23 import javax.swing.JPanel;
24
25 import edu.uci.ics.jung.algorithms.layout.FRLayout;
26 import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
27 import edu.uci.ics.jung.algorithms.layout.Layout;
28 import edu.uci.ics.jung.graph.Graph;
29 import edu.uci.ics.jung.graph.util.TestGraphs;
30 import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
31 import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
32 import edu.uci.ics.jung.visualization.VisualizationModel;
33 import edu.uci.ics.jung.visualization.VisualizationViewer;
34 import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
35 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
36 import edu.uci.ics.jung.visualization.control.ScalingControl;
37 import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
38 import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
39 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
40 import edu.uci.ics.jung.visualization.picking.MultiPickedState;
41 import edu.uci.ics.jung.visualization.picking.PickedState;
42 import edu.uci.ics.jung.visualization.transform.MutableTransformer;
43
44
45
46
47
48
49
50
51 @SuppressWarnings("serial")
52 public class TwoModelDemo extends JApplet {
53
54
55
56
57 Graph<String,Number> graph;
58
59
60
61
62 VisualizationViewer<String,Number> vv1;
63 VisualizationViewer<String,Number> vv2;
64
65
66
67
68 MutableTransformer layoutTransformer;
69
70 Dimension preferredSize = new Dimension(300,300);
71
72
73
74
75
76
77 public TwoModelDemo() {
78
79
80
81 graph = TestGraphs.getOneComponentGraph();
82
83
84 Layout<String,Number> layout1 = new FRLayout<String,Number>(graph);
85 Layout<String,Number> layout2 = new ISOMLayout<String,Number>(graph);
86
87
88 VisualizationModel<String,Number> vm1 =
89 new DefaultVisualizationModel<String,Number>(layout1, preferredSize);
90 VisualizationModel<String,Number> vm2 =
91 new DefaultVisualizationModel<String,Number>(layout2, preferredSize);
92
93
94
95 vv1 = new VisualizationViewer<String,Number>(vm1, preferredSize);
96 vv2 = new VisualizationViewer<String,Number>(vm2, preferredSize);
97 vv1.setRenderContext(vv2.getRenderContext());
98
99
100
101
102
103
104
105
106 vv2.getRenderContext().setMultiLayerTransformer(vv1.getRenderContext().getMultiLayerTransformer());
107 vv2.getRenderContext().getMultiLayerTransformer().addChangeListener(vv1);
108
109 vv1.setBackground(Color.white);
110 vv2.setBackground(Color.white);
111
112
113 PickedState<String> ps = new MultiPickedState<String>();
114 vv1.setPickedVertexState(ps);
115 vv2.setPickedVertexState(ps);
116 PickedState<Number> pes = new MultiPickedState<Number>();
117 vv1.setPickedEdgeState(pes);
118 vv2.setPickedEdgeState(pes);
119
120
121 vv1.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv1.getPickedEdgeState(), Color.black, Color.red));
122 vv1.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(vv1.getPickedVertexState(),
123 Color.red, Color.yellow));
124
125 vv1.setVertexToolTipTransformer(new ToStringLabeller());
126 vv2.setVertexToolTipTransformer(new ToStringLabeller());
127
128 Container content = getContentPane();
129 JPanel panel = new JPanel(new GridLayout(1,0));
130 panel.add(new GraphZoomScrollPane(vv1));
131 panel.add(new GraphZoomScrollPane(vv2));
132
133 content.add(panel);
134
135
136 final DefaultModalGraphMouse gm1 = new DefaultModalGraphMouse();
137
138 DefaultModalGraphMouse gm2 = new DefaultModalGraphMouse();
139
140 vv1.setGraphMouse(gm1);
141 vv2.setGraphMouse(gm2);
142
143
144
145 final ScalingControl scaler = new CrossoverScalingControl();
146
147 JButton plus = new JButton("+");
148 plus.addActionListener(new ActionListener() {
149 public void actionPerformed(ActionEvent e) {
150 scaler.scale(vv1, 1.1f, vv1.getCenter());
151 }
152 });
153 JButton minus = new JButton("-");
154 minus.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent e) {
156 scaler.scale(vv1, 1/1.1f, vv1.getCenter());
157 }
158 });
159
160 JPanel zoomPanel = new JPanel(new GridLayout(1,2));
161 zoomPanel.setBorder(BorderFactory.createTitledBorder("Zoom"));
162
163 JPanel modePanel = new JPanel();
164 modePanel.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
165 gm1.getModeComboBox().addItemListener(gm2.getModeListener());
166 modePanel.add(gm1.getModeComboBox());
167
168 JPanel controls = new JPanel();
169 zoomPanel.add(plus);
170 zoomPanel.add(minus);
171 controls.add(zoomPanel);
172 controls.add(modePanel);
173 content.add(controls, BorderLayout.SOUTH);
174 }
175
176
177
178
179 public static void main(String[] args) {
180 JFrame f = new JFrame();
181 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
182 f.getContentPane().add(new TwoModelDemo());
183 f.pack();
184 f.setVisible(true);
185 }
186 }