1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.samples;
13 import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
14 import edu.uci.ics.jung.algorithms.layout.FRLayout;
15 import edu.uci.ics.jung.algorithms.layout.FRLayout2;
16 import edu.uci.ics.jung.algorithms.layout.SpringLayout;
17 import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
18 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
19 import edu.uci.ics.jung.graph.Graph;
20 import edu.uci.ics.jung.graph.ObservableGraph;
21 import edu.uci.ics.jung.graph.event.GraphEvent;
22 import edu.uci.ics.jung.graph.event.GraphEventListener;
23 import edu.uci.ics.jung.graph.util.Graphs;
24 import edu.uci.ics.jung.visualization.VisualizationViewer;
25 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
26 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
27 import edu.uci.ics.jung.visualization.renderers.Renderer;
28
29 import org.apache.commons.collections15.functors.ConstantTransformer;
30
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.Font;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.util.Timer;
38 import java.util.TimerTask;
39
40 import javax.swing.JButton;
41 import javax.swing.JFrame;
42 import javax.swing.JRootPane;
43
44
45
46
47
48
49 public class AddNodeDemo extends javax.swing.JApplet {
50
51
52
53
54 private static final long serialVersionUID = -5345319851341875800L;
55
56 private Graph<Number,Number> g = null;
57
58 private VisualizationViewer<Number,Number> vv = null;
59
60 private AbstractLayout<Number,Number> layout = null;
61
62 Timer timer;
63
64 boolean done;
65
66 protected JButton switchLayout;
67
68
69
70 public static final int EDGE_LENGTH = 100;
71
72 @Override
73 public void init() {
74
75
76 Graph<Number,Number> ig = Graphs.<Number,Number>synchronizedDirectedGraph(new DirectedSparseMultigraph<Number,Number>());
77
78 ObservableGraph<Number,Number> og = new ObservableGraph<Number,Number>(ig);
79 og.addGraphEventListener(new GraphEventListener<Number,Number>() {
80
81 public void handleGraphEvent(GraphEvent<Number, Number> evt) {
82 System.err.println("got "+evt);
83
84 }});
85 this.g = og;
86
87 layout = new FRLayout2<Number,Number>(g);
88
89
90 vv = new VisualizationViewer<Number,Number>(layout, new Dimension(600,600));
91
92 JRootPane rp = this.getRootPane();
93 rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
94
95 getContentPane().setLayout(new BorderLayout());
96 getContentPane().setBackground(java.awt.Color.lightGray);
97 getContentPane().setFont(new Font("Serif", Font.PLAIN, 12));
98
99 vv.getModel().getRelaxer().setSleepTime(500);
100 vv.setGraphMouse(new DefaultModalGraphMouse<Number,Number>());
101
102 vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
103 vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Number>());
104 vv.setForeground(Color.white);
105 getContentPane().add(vv);
106 switchLayout = new JButton("Switch to SpringLayout");
107 switchLayout.addActionListener(new ActionListener() {
108
109 @SuppressWarnings("unchecked")
110 public void actionPerformed(ActionEvent ae) {
111 Dimension d = new Dimension(600,600);
112 if (switchLayout.getText().indexOf("Spring") > 0) {
113 switchLayout.setText("Switch to FRLayout");
114 layout = new SpringLayout<Number,Number>(g,
115 new ConstantTransformer(EDGE_LENGTH));
116 layout.setSize(d);
117 vv.getModel().setGraphLayout(layout, d);
118 } else {
119 switchLayout.setText("Switch to SpringLayout");
120 layout = new FRLayout<Number,Number>(g, d);
121 vv.getModel().setGraphLayout(layout, d);
122 }
123 }
124 });
125
126 getContentPane().add(switchLayout, BorderLayout.SOUTH);
127
128 timer = new Timer();
129 }
130
131 @Override
132 public void start() {
133 validate();
134
135 timer.schedule(new RemindTask(), 1000, 1000);
136 vv.repaint();
137 }
138
139 Integer v_prev = null;
140
141 public void process() {
142
143 try {
144
145 if (g.getVertexCount() < 100) {
146 layout.lock(true);
147
148 Integer v1 = new Integer(g.getVertexCount());
149
150 Relaxer relaxer = vv.getModel().getRelaxer();
151 relaxer.pause();
152 g.addVertex(v1);
153 System.err.println("added node " + v1);
154
155
156 if (v_prev != null) {
157 g.addEdge(g.getEdgeCount(), v_prev, v1);
158
159 int rand = (int) (Math.random() * g.getVertexCount());
160 g.addEdge(g.getEdgeCount(), v1, rand);
161 }
162
163 v_prev = v1;
164
165 layout.initialize();
166 relaxer.resume();
167 layout.lock(false);
168 } else {
169 done = true;
170 }
171
172 } catch (Exception e) {
173 System.out.println(e);
174
175 }
176 }
177
178 class RemindTask extends TimerTask {
179
180 @Override
181 public void run() {
182 process();
183 if(done) cancel();
184
185 }
186 }
187
188 public static void main(String[] args) {
189 AddNodeDemo and = new AddNodeDemo();
190 JFrame frame = new JFrame();
191 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
192 frame.getContentPane().add(and);
193
194 and.init();
195 and.start();
196 frame.pack();
197 frame.setVisible(true);
198 }
199 }