Wolf Marian Wolf Marian - 1 month ago 14
Java Question

Add node name in java jung

I implemented some graph algoritms and I want to show a draw of them. I used Jung 2.0

I want to add a name to a node. I managed how to write around the edges, but I want to show the name of a node also.

Here is my code:

Graph<Integer, String> graph3 = new DelegateForest<Integer, String>();

graph3.addVertex(1);
graph3.addVertex(2);
graph3.addVertex(3);

graph3.addEdge("RAD-A", 1, 2);
graph3.addEdge("RAD-B", 1, 3);

Layout<Integer, String> layout3 = new TreeLayout<Integer, String>((Forest<Integer, String>) graph3);
VisualizationViewer<Integer, String> vv3 = new VisualizationViewer<Integer,String>(layout3);

Transformer<String, String> transformer3 = new Transformer<String, String>() {

@Override
public String transform(String arg0){
return arg0;
}
};

vv3.getRenderContext().setEdgeLabelTransformer(transformer3);

transformer3 = new Transformer<String, String>() {
@Override
public String transform(String arg0){
return arg0;
}
};

final DefaultModalGraphMouse<String,Number> graphMouse3 = new DefaultModalGraphMouse<String,Number>();
vv3.setGraphMouse(graphMouse3);
graphMouse3.setMode(ModalGraphMouse.Mode.PICKING);


JFrame frame3 = new JFrame("Pas 3");
frame3.getContentPane().add(vv3);
frame3.pack();
frame3.setSize(350, 300);
frame3.setVisible(true);

Answer

You are almost there... you need to do the same as you did with the edges...

for the edges you did something like:

 vv3.getRenderContext().setEdgeLabelTransformer(transformer3);

for the Nodes is the same... you need to setVertexLabelTransformer

add this segment to your code..

  Transformer<Integer, String> transformer = new Transformer<Integer, String>() {

    @Override
    public String transform(Integer arg0) {
    return arg0.toString();
    }

};
vv3.getRenderContext().setVertexLabelTransformer(transformer);

Edit:

you need to adapt the generic map in order to hold the Integer, String...

at the esd you will get somthing like

enter image description here