GraphStream Users

Archives de la liste Aide

Re: Dissociate the automatic positioning and the display of the graph.

Chronologique Discussions 
  • From: guilhelm savin <guilhelm.savin AT>
  • To: graphstream-users AT
  • Subject: Re: Dissociate the automatic positioning and the display of the graph.
  • Date: Fri, 8 Jul 2011 10:46:59 +0200

Do you want to get an image of the graph ? Or a view without 
a window ?

For the view, Viewer uses View objects that are JPanel and
that you can use in your own program :

| Graph g = ... ;
| Viewer v = new Viewer(g, ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
| Viewer view = v.addDefaultView(false); // use false to not create a window
| JPanel viewAsPanel = (JPanel) view;

To create images, you can see FileSinkImages.

Hope this helps ...

2011/7/8 Nicolas Quattropani <nicolas.quattropani AT>
Thanks a lot for this answer, it helps me to understand some concepts and I made some tries by myself but actually It would have been more a question about the option 1 : "obtaining a picture without opening a window" so we can use/export our generated graph the way we want after that..

Sorry for the misunderstanding, I hope you could help me again.


2011/7/7 Antoine Dutot <antoine.dutot AT>
I am not sure to understand. Do you want to obtain a picture without opening a window or only get the nodes position from the layout ?

I assume it's the second option. The Layouts in GraphStream are made to run continuously (for dynamic graphs), and we lack layout algorithms. However it is possible to use the only layout available in GraphStream (SpringBox) in this way. Under is an example of this.

Basically, you create the layout object, make it sink of the graph, and make the graph sink of the layout. This way the layout receives graph events (new nodes, new edges) and the graph receives position attributes ("xyz" attributes stored on nodes). The result is a graph with the correct positioning attributes (you can display this graph with a viewer where the automatic layout is disabled).

To compute the layout with the SpringBox, you only have to call repeatedly layout.compute(), until the layout.getStabilization() method returns a number close to 1. Naturally, force-directed layouts may never reach 1, therefore you must fix a threshold, for example 0.9. The example under, displays the graph, but the viewer, here, is not related to the layout, and no automatic layout takes place. You can remove the code related to the viewer to obtain only a layout of the graph.

The graph being directly modified by the layout, you can write it directly, and the "xyz" attributes will be stored with it.

Hope this answers the question, else do not hesitate to repost,



import org.graphstream.algorithm.generator.DorogovtsevMendesGenerator;
import org.graphstream.algorithm.generator.Generator;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.MultiGraph;
import org.graphstream.ui.layout.Layout;
import org.graphstream.ui.layout.springbox.SpringBox;
import org.graphstream.ui.swingViewer.Viewer;

public class TestLayoutAndViewer {
public static void main(String args[]) {
new TestLayoutAndViewer();

public TestLayoutAndViewer() {
boolean loop = true;
Graph graph = new MultiGraph("g1");
Viewer viewer = new Viewer(new ThreadProxyPipe(graph));
ProxyPipe fromViewer = viewer.newThreadProxyOnGraphicGraph();
Layout layout = new SpringBox(false);

graph.addAttribute("ui.stylesheet", styleSheet);
// We make a loop between the layout and the graph.
// Both listen at the other, however the graph
// can listen only at attributes of the graph since
// only the "xyz" positions are needed. GraphStream
// handles such loops gracefully (this is called
// graph synchronization).

// Generate a graph.
Generator gen = new DorogovtsevMendesGenerator();

for (int i = 0; i < 500; i++)

while (loop) {
// Get the events from the viewer (is the view
// closed, is the mouse pressed). This comes
// under the form of attributes.

if (graph.hasAttribute("ui.viewClosed")) {
loop = false;
} else {
try { Thread.sleep(20); } catch (Exception e) {}
// We compute one step of the layout.
// The more the layout is iterated, better
// is the layout. Note that at the contrary
// of automatic layout, the algorithm will
// never end. You must stop it by yourself,
// by observing the layout.getStabilization()
// value (0 not stable, 1 fully stable).


protected static String styleSheet =
 "node { size: 3px; fill-color: rgb(150,150,150); }"
+ "edge { fill-color: rgb(100,100,100); }";

2011/7/7 Nicolas Quattropani <nicolas.quattropani AT>

I would like to know if it's possible to dissociate the automatic calculation of the elements' placement and the display of the graph (ie get the viewer without displaying the swing windows).

Indeed, we would like to set up our graph without displaying it it in a swing window in order to export it in an another format afterwards.

We didn't found how to do that, is it possible ?

Thanks for your time,



Guilhelm Savin
PhD Student of Computer Science

Archives gérées par MHonArc 2.6.16.

Top of page