Search code examples
javauser-interfacejungjung2

Java: Place the arrow at the center of the edges in Jung2 Network


I have a network with directed edges. The arrows indicate the direction of the edge in the GUI. But as shown in figure, when the arrows are at the end of the edges it becomes pretty confusing. Is it possible to change the location of the arrow to the center or any other position on the edge? enter image description here

EDIT:

AffineTransform getArrowTransform(RenderContext<V,E> rc, Shape edgeShape, Shape vertexShape)

I have found that this method can be used to set the arrows at the defined position but I still could not find any example on how to use this method. Link


Solution

  • You can assign the CenterEdgeArrowRenderingSupport to the Renderer.Edge that you obtained from the Renderer of the VisualizationViewer. It will not necessarily hit the exact center of the edge (and from a short glance at the code, it might fail completely if the edge is straight, but this would have to be verified).

    In any case, here is a MCVE:

    import edu.uci.ics.jung.algorithms.layout.FRLayout;
    import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
    import edu.uci.ics.jung.graph.Graph;
    import edu.uci.ics.jung.visualization.VisualizationViewer;
    import edu.uci.ics.jung.visualization.renderers.CenterEdgeArrowRenderingSupport;
    import edu.uci.ics.jung.visualization.renderers.Renderer;
    
    public class JUNGEdgeCenterArrows 
    {
        public static void main(String[] args) 
        {
            JFrame f = new JFrame();
            final Graph<String, String> g = getGraph();
            VisualizationViewer<String, String> vv = 
                new VisualizationViewer<String, String>(
                    new FRLayout<String, String>(g));
            Renderer.Edge<String, String> edgeRenderer = 
                vv.getRenderer().getEdgeRenderer();
            edgeRenderer.setEdgeArrowRenderingSupport(
                new CenterEdgeArrowRenderingSupport<String, String>());
            f.getContentPane().add(vv);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    
    
        public static Graph<String, String> getGraph() 
        {
            Graph<String, String> g = 
                new DirectedSparseMultigraph<String, String>();
            g.addVertex("v0");
            g.addVertex("v1");
            g.addEdge("e0", "v0", "v1");
            g.addEdge("e1", "v0", "v1");
            g.addEdge("e2", "v0", "v1");
            return g;
        }
    }