I am new to Jung. I need to visualize a tree containing severel nodes. It all works fine using a graph of type DelegateForest and a TreeLayout. However, when adding edges, the code runs into an exception, if edges are being added to the graph that are already in the tree. Exception is: "Exception in thread "main" java.lang.IllegalArgumentException: Tree must not already contain child x". In other words, if an edge is added that contains vertices, which are already in the graph, the above exception is thrown. The following example code demonstrates this:
Forest<Integer, String> g2 = new DelegateForest<Integer, String>();
g2.addVertex((Integer) 1);
g2.addVertex((Integer) 2);
g2.addVertex((Integer) 3);
g2.addVertex((Integer) 4);
g2.addVertex((Integer) 5);
g2.addEdge("Edge-1-3", 1, 3);
g2.addEdge("Edge-2-3-P", 2, 3);
g2.addEdge("Edge-4-3-P", 4, 3);
g2.addEdge("Edge-4-5-P", 4, 5);
g2.addEdge("Edge-3-5-P", 3, 5);
Layout<Integer, String> layout = new TreeLayout<Integer, String>(g2);
VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout);
vv.setPreferredSize(new Dimension(800, 600));
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
JFrame frame = new JFrame("Simple Graph View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
The exception is thrown on the line "g2.addEdge("Edge-3-5-P", 3, 5);"
So I understand that I have to use the graph type SparseMultigraph. Unfortunately, there is no layout that looks like the TreeLayout that one can use with the SparseMultigraph. Can you give me a hint on how to solve my problem?
Many thanks in advance!