Skip to content

Instantly share code, notes, and snippets.

@myro
Created June 30, 2012 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myro/d2136f4e492092d6c40a to your computer and use it in GitHub Desktop.
Save myro/d2136f4e492092d6c40a to your computer and use it in GitHub Desktop.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.MultiGraph;
import org.graphstream.ui.swingViewer.Viewer;
public class AllInSwing
{
public static void main(String args[])
{
new AllInSwing();
}
public AllInSwing()
{
Graph graph = new MultiGraph("mg");
Viewer viewer = new Viewer(graph,
Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
SwingUtilities.invokeLater(new InitializeApplication(viewer, graph));
}
}
class InitializeApplication extends JFrame implements Runnable
{
private static final long serialVersionUID = -804177406404724792L;
protected Graph graph;
protected Viewer viewer;
public InitializeApplication(Viewer viewer, Graph graph)
{
this.viewer = viewer;
this.graph = graph;
}
public void run()
{
int amount = 10;
for (Integer i = 0; i < amount; ++i)
{
Node n = graph.addNode(i.toString());
n.setAttribute("ui.label", i.toString());
}
// add one edge between each two nodes
for(Integer i = 0; i < amount; ++i)
{
for(Integer j = i; j < amount; ++j)
{
if(i != j)
{
graph.addEdge(i.toString()+ j.toString(), i.toString(), j.toString());
}
}
}
viewer.enableAutoLayout();
add(viewer.addDefaultView(false), BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment