How to represent edge weight through JGraphT visualization?

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

http://i.stack.imgur.com/KXRrc.png

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

+4
source share
2 answers

These border labels are derived from the toString method of the edge object. Therefore, you can create your own edge class and override the toString method so that it gives the weight of the edge.

You can create your custom rib as follows:

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

And then create your graph so that the graph creates edges as instances of MyDefaultEdge and therefore only displays the edge size in the visualization:

 SimpleWeightedGraph<String,MyWeightedEdge> graph = new SimpleWeightedGraph<String,MyWeightedEdge>(MyWeightedEdge.class); 
+4
source

JGraphT is implemented by Generic, so when you pass the Edge class, just apply the toString () method in your class. For instance. Suppose that the "City" is my "Top" class, and the "Path" is my edge, so we implement the toString () method in that

 class Pathe{ @Override public String toString() { return "Distant Between" +this.source +"this.dest" "is" + this.distance; } } 
0
source

Source: https://habr.com/ru/post/1445989/


All Articles