How represent edge weight via JGraphT visualization?
Asked Answered
C

2

6

I tried create a Java JGraphT visualization with Weight, for example:

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/KXRrc.png

Now I want to change the edge label (ex: (a:b) to its edge weight (ex: 2). I tried to search in Javadoc but didn't see anything. Please help me thank you!

Counterpoint answered 14/11, 2012 at 12:6 Comment(0)
E
4

Those edge labels came from the toString method of the edge object. Therefore you may create your own edge class and override the toString method in such a way that it yields the edge weight.

You may create your custom edge like this:

class MyWeightedEdge extends DefaultWeightedEdge {
  @Override
  public String toString() {
    return Double.toString(getWeight());
  }
}

And then create your graph like this which will make the graph create edges as instances of MyDefaultEdge and therefore display just edge weight in the visualization:

SimpleWeightedGraph<String,MyWeightedEdge> graph
  = new SimpleWeightedGraph<String,MyWeightedEdge>(MyWeightedEdge.class);
Ecklund answered 16/1, 2013 at 22:0 Comment(0)
V
0

JGraphT is implemented Generic so when you pass Edge class just implement toString() method in your respective class. E.g. suppose "City" is my Vertex class and "Path" is my Edge so implement toString() method in that

class Pathe{
  @Override
  public String toString() {
    return "Distant Between" +this.source +"this.dest" "is" + this.distance;
  }
}
Voracious answered 1/5, 2013 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.