
edu.uci.ics.jung.samples.AnimatingAddNodeDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jung-samples Show documentation
Show all versions of jung-samples Show documentation
Sample programs using JUNG. Nearly all JUNG capabilities are demonstrated here.
Please study the source code for these examples prior to asking how to do something.
The newest version!
/*
* Copyright (c) 2003, The JUNG Authors
* All rights reserved.
*
* This software is open-source under the BSD license; see either "license.txt"
* or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
*
*/
package edu.uci.ics.jung.samples;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import com.google.common.base.Functions;
import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
import edu.uci.ics.jung.algorithms.layout.util.VisRunner;
import edu.uci.ics.jung.algorithms.util.IterativeContext;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.ObservableGraph;
import edu.uci.ics.jung.graph.event.GraphEvent;
import edu.uci.ics.jung.graph.event.GraphEventListener;
import edu.uci.ics.jung.graph.util.Graphs;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.layout.LayoutTransition;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.util.Animator;
/**
* A variation of AddNodeDemo that animates transitions between graph states.
*
* @author Tom Nelson
*/
public class AnimatingAddNodeDemo extends javax.swing.JApplet {
/**
*
*/
private static final long serialVersionUID = -5345319851341875800L;
private Graph g = null;
private VisualizationViewer vv = null;
private AbstractLayout layout = null;
Timer timer;
boolean done;
protected JButton switchLayout;
public static final int EDGE_LENGTH = 100;
@Override
public void init() {
//create a graph
Graph ig = Graphs.synchronizedDirectedGraph(new DirectedSparseMultigraph());
ObservableGraph og = new ObservableGraph(ig);
og.addGraphEventListener(new GraphEventListener() {
public void handleGraphEvent(GraphEvent evt) {
System.err.println("got "+evt);
}});
this.g = og;
//create a graphdraw
layout = new FRLayout(g);
layout.setSize(new Dimension(600,600));
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
Layout staticLayout =
new StaticLayout(g, layout);
vv = new VisualizationViewer(staticLayout, new Dimension(600,600));
JRootPane rp = this.getRootPane();
rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
getContentPane().setLayout(new BorderLayout());
getContentPane().setBackground(java.awt.Color.lightGray);
getContentPane().setFont(new Font("Serif", Font.PLAIN, 12));
vv.setGraphMouse(new DefaultModalGraphMouse());
vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.setForeground(Color.white);
vv.addComponentListener(new ComponentAdapter() {
/**
* @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent)
*/
@Override
public void componentResized(ComponentEvent arg0) {
super.componentResized(arg0);
System.err.println("resized");
layout.setSize(arg0.getComponent().getSize());
}});
getContentPane().add(vv);
switchLayout = new JButton("Switch to SpringLayout");
switchLayout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Dimension d = vv.getSize();//new Dimension(600,600);
if (switchLayout.getText().indexOf("Spring") > 0) {
switchLayout.setText("Switch to FRLayout");
layout =
new SpringLayout(g, Functions.constant(EDGE_LENGTH));
layout.setSize(d);
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
StaticLayout staticLayout =
new StaticLayout(g, layout);
LayoutTransition lt =
new LayoutTransition(vv, vv.getGraphLayout(),
staticLayout);
Animator animator = new Animator(lt);
animator.start();
vv.repaint();
} else {
switchLayout.setText("Switch to SpringLayout");
layout = new FRLayout(g, d);
layout.setSize(d);
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
StaticLayout staticLayout =
new StaticLayout(g, layout);
LayoutTransition lt =
new LayoutTransition(vv, vv.getGraphLayout(),
staticLayout);
Animator animator = new Animator(lt);
animator.start();
vv.repaint();
}
}
});
getContentPane().add(switchLayout, BorderLayout.SOUTH);
timer = new Timer();
}
@Override
public void start() {
validate();
//set timer so applet will change
timer.schedule(new RemindTask(), 1000, 1000); //subsequent rate
vv.repaint();
}
Integer v_prev = null;
public void process() {
vv.getRenderContext().getPickedVertexState().clear();
vv.getRenderContext().getPickedEdgeState().clear();
try {
if (g.getVertexCount() < 100) {
//add a vertex
Integer v1 = new Integer(g.getVertexCount());
g.addVertex(v1);
vv.getRenderContext().getPickedVertexState().pick(v1, true);
// wire it to some edges
if (v_prev != null) {
Integer edge = g.getEdgeCount();
vv.getRenderContext().getPickedEdgeState().pick(edge, true);
g.addEdge(edge, v_prev, v1);
// let's connect to a random vertex, too!
int rand = (int) (Math.random() * g.getVertexCount());
edge = g.getEdgeCount();
vv.getRenderContext().getPickedEdgeState().pick(edge, true);
g.addEdge(edge, v1, rand);
}
v_prev = v1;
layout.initialize();
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
StaticLayout staticLayout =
new StaticLayout(g, layout);
LayoutTransition lt =
new LayoutTransition(vv, vv.getGraphLayout(),
staticLayout);
Animator animator = new Animator(lt);
animator.start();
// vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.repaint();
} else {
done = true;
}
} catch (Exception e) {
System.out.println(e);
}
}
class RemindTask extends TimerTask {
@Override
public void run() {
process();
if(done) cancel();
}
}
public static void main(String[] args) {
AnimatingAddNodeDemo and = new AnimatingAddNodeDemo();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(and);
and.init();
and.start();
frame.pack();
frame.setVisible(true);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy