简体   繁体   中英

JUNG - Edges with icons

I am using JUNG for displaying network devices as graphs. Each edge is a link between two network elements. I want to add a picture exactly in the middle of the link. Currently my link looks like this: enter image description here

The code for it is:

vv.getRenderContext().setEdgeLabelTransformer(new Function<GraphLink,String>() {
            URL url = getClass().getResource("/icons/model/conn.radio.png");
            public String apply(GraphLink input) {
                return "<html><img src="+url+" height=14 width=14>";
            }});

As you can see the icon is adjacent with the link. Is there any option so the link could split the icon in two equals parts?

tl;dr You want to tweak the edge label offset.

The position of the edge label is specified by a couple of properties, both accessible via the RenderContext :

  • The EdgeLabelClosenessTransformer, which should return a value in the range [0, 1], specifies the position of the label along the edge, that is, whether it should be closer to the source node (0) or the target node (1).
  • The edge label offset, which should return the distance from the edge to its label.

You can see how these properties are used (by default) in BasicEdgeLabelRenderer .

By default, the edge label offset is set to 10, which should make the edge label fairly close to, but not on top of, the edge. If you set it to 0:

vv.getRenderContext().setLabelOffset(0);

Then that should do what you want.

You may find it useful to experiment with EdgeLabelDemo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM