1

I am trying to model a weighted and directed graph in java. I am using jgraph and jgraphT libraries to do it. By reading the tutorials I managed to model a directed graph but I am confused how I can add weight to the edges. In fact, using "setEdgeWeight", I can do it but I also need to visualize this on the graph. Anyone knows how to it?

Following is my code:

 ListenableDirectedWeightedGraph<String, DefaultWeightedEdge> g =
        new ListenableDirectedWeightedGraph<>(
            DefaultWeightedEdge.class);

    // create a visualization using JGraph, via the adapter
    jgAdapter = new JGraphModelAdapter<>(g);

    jgraph = new JGraph(jgAdapter);

    adjustDisplaySettings(jgraph);
    getContentPane().add(jgraph);
    resize(DEFAULT_SIZE);

    // add some hard-coded sample data (graph manipulated via JGraphT)
    //First the vertices
    g.addVertex("input page");
    g.addVertex("vertex1");
    g.addVertex("vertex2");
    g.addEdge("vertex2", "input page");
    g.addEdge("vertex1", "input page");

  g.setEdgeWeight(g.getEdge("vertex2", "input page"), 1000);

Although the code assigns weight, while I try to visualize it, I see no weight label on the edges.

 positionVertexAt("input page", 130, 40);
    positionVertexAt("vertex2", 150, 10);
    positionVertexAt("vertex1", 110, 70);

    adjustDisplaySettings(jgraph);
    getContentPane().add(jgraph);
    resize(DEFAULT_SIZE);

    JFrame frame = new JFrame();
    frame.getContentPane().add(jgraph);
    frame.setTitle("JGraphT Adapter to JGraph Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

Using the following class, I can generate a static value of weight. But I need to generate the weights dynamically while expanding the graph.

public class MyWeightedEdge extends DefaultWeightedEdge {

public MyWeightedEdge() {
    super();
}

@Override
public String toString() {
    return Double.toString(2.0);
}
}
n4z4nin
  • 11
  • 1
  • 6
  • Here is an [example](http://stackoverflow.com/a/24519791/1048330). – tenorsax Aug 19 '14 at 15:51
  • @Aqua thank you. But do you know a way using jgrapht library? – n4z4nin Aug 20 '14 at 07:45
  • @Aqua I got the point now. Just can you please explain me what is getWeight() function? if I need to implement it by myself or if it already exists, which library I need to import? – n4z4nin Aug 20 '14 at 08:29
  • 1
    @Aqua I got the solution. The version of my jgrapht was a bit old. So getWeight() was not a known function. I downloaded the last version and it works fine now. Many thanks for your help. – n4z4nin Aug 20 '14 at 09:46
  • @Aqua By the way, do you know how can we force the graph, generated in this way, to be dense and fits in the screen? My generated graph is very large and does't show any abstract view of it. In fact, I need to scroll it and this is not understandable for me. I tried the following but it does not work. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(screenSize.width - 4, screenSize.height - 4); – n4z4nin Aug 22 '14 at 14:11
  • I am not sure I understand the problem. `mxGraphComponent` provides a scrollable view, it actually extends `JScrollPane`. Is there a problem with scrolling or you want to make the graph fit into a screen? To fit you can scale the view, here is an [example](http://forum.jgraph.com/questions/2844/how-do-i-fit-the-graph-into-the-visible-area). It is better if you create a new question, because comments are pretty limited. – tenorsax Aug 22 '14 at 18:04
  • @Aqua Thank you Aqua. I posted a new question. – n4z4nin Aug 25 '14 at 09:37

0 Answers0