2

We have developed a little graph editor with jung where you can draw graph/networks with your mouse. We use the VisualizationViewer as the panel we draw on. The VisualizationViewer holds the graph it has to display via its containing GraphLayout. We also have a save function which saves the graph into a text file:

public void saveGraph(){
    Graph<V, E> g = visualizationviewer.getGraphLayout.getGraph();
    // save g into text file
}

I have now written a class that generates me a new graph object using some algorithms:

public Graph<V, E> generateGraph(){
    Graph<V, E> g = new DirectedSparseGraph<V, E>();
    // do some algorithms on g
    return g
}

If I now want to display the generated graph via:

...
visualisationviewer.getGraphLayout.setGraph(generateGraph());
...

The new Graph is perfectly displayed and one can draw on it even further.

But the saving functions (and all other functions that want to use the underlying Graph object of the VisualizationViewer) are now not working properly anymore. Either only one node is recognized or no nodes (which results in a NullPointerException). Everytime we want to retrieve the underlying Graph data structure we use:

visualizationviewer.getGraphLayout.getGraph();

Am I something missing here? Or is there a known bug within the framework that hasn't been fixed? It seems weird that the optical part is working perfectly while the programmatically retrieving of the underlying objects is not.

user283494
  • 1,897
  • 4
  • 19
  • 26
  • I don't fully explore JUNG2 only use what i need most but JUNG2 does provide a persistent mechanism to save/load a graph/layout which I don't use in my project...try to check the example code in `edu.uci.ics.jung.samples.PersistentLayoutDemo.java` whether it fits your requirement or not. – ecle Nov 28 '11 at 11:19
  • The retrieving is also used in many other parts of our editor, I just used the saving feature as an example. The problem is always the same. – user283494 Nov 28 '11 at 15:10

1 Answers1

1

The problem is that you added the vertices in two steps by adding them to an arrayList first and adding to the graph from that list. Since your program works dynamically in order to avoid null-pointer exceptions you have to add nodes like this:

Node node;           
g.addVertex(node = nodefactory.create());
nodes.add(node);

This way you're still able to use your arrayList(nodes) but avoid errors!

The second error is that the visualization viewer does not recognize the nodes from the new graph therefore you have to compare the names of the nodes and take the position from the old node in order to get it right

I don't think that's helpful at all..
Just keep in mind to add PDEEdges and Nodes directly and not via ArrayList ;-)

Anne
  • 26,765
  • 9
  • 65
  • 71
OttoWalkes
  • 11
  • 1