2

After do a test by JUNG2, I found all edge line is bent, but not straight line...How to do a straight line for edge by Jung2?

package pkg;

import javax.swing.JFrame;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;

public class test {
    public static void main(String[] args) {
        // Graph<V, E> where V is the type of the vertices
        // and E is the type of the edges
        Graph<Integer, String> g = new SparseMultigraph<Integer, String>();
        // Add some vertices. From above we defined these to be type Integer.
        g.addVertex((Integer)1);
        g.addVertex((Integer)2);
        g.addVertex((Integer)3);
        // Add some edges. From above we defined these to be of type String
        // Note that the default is for undirected edges.
        g.addEdge("Edge-A", 1, 2);
        g.addEdge("Edge-B", 2, 3);
        // Let's see what we have. Note the nice output from the
        // SparseMultigraph<V,E> toString() method
        // Note that we can use the same nodes and edges in two different graphs.
        System.out.println("The graph g = " + g.toString());

        Layout<Integer, String> layout = new CircleLayout(g);

        VisualizationViewer<Integer,String> vv = new VisualizationViewer<Integer,String>(layout);
        DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
        gm.setMode(ModalGraphMouse.Mode.PICKING);
        vv.setGraphMouse(gm);

        JFrame frame = new JFrame("Simple Graph View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        frame.pack();
        frame.setVisible(true);
    }

}

Follow output result, black line is default, red line that I want to get that: http://www.zhaocs.info/wp-content/uploads/2015/04/test.png

frank
  • 53
  • 1
  • 8

1 Answers1

3

tl;dr:

Call

vv.getRenderContext().setEdgeShapeTransformer(
    new EdgeShape.Line<Integer,String>());

on your VisualizationViewer


There are various options for the edge shape in JUNG. These options are implemented as "edge shape transformers", nested classes of the edu.uci.ics.jung.visualization.decorators.EdgeShape class. The default is EdgeShape.QuadCurve which causes the curved edges. Other options include (extracted from the documentation) :

BentLine<V,E>    bent-line between the vertex endpoints.
Box<V,E>         loop with its nadir at the center of the vertex.
CubicCurve<V,E>  CubicCurve between vertex endpoints.
Line<V,E>        straight line between the vertex endpoints.
Loop<V,E>        loop with its nadir at the center of the vertex.
Orthogonal<V,E>  bent-line between the vertex endpoints.
QuadCurve<V,E>   QuadCurve between vertex endpoints.
SimpleLoop<V,E>  loop with its nadir at the center of the vertex.
Wedge<V,E>       isosceles triangle whose apex is at the destination 
                 vertex for directed edges, and as a "bowtie" shape 
                 for undirected edges.
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • Maybe the interface changed, but using jung-visualization 2.1.1 `new EdgeShape.Line()` doesn't work because `EdgeShape.Line` is not a generic class of type ``. Instead, use the static function `EdgeShape.line(g)`, which will return a properly initialized Line instance: `vv.getRenderContext().setEdgeShapeTransformer(EdgeShape.line(g));` – Zakum Mar 27 '17 at 15:30
  • 1
    @Zakum Looks like you're right: [EdgeShape.Line in 2.0.1](http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/visualization/decorators/EdgeShape.Line.html) and [EdgeShape.Line in 2.1](http://jrtom.github.io/jung/javadoc/edu/uci/ics/jung/visualization/decorators/EdgeShape.Line.html) (or a later version). The factory methods make things a tad simpler, indeed. (The development of JUNG had stalled for a while - good to see that jtrom picked it up - it's a very useful library!) – Marco13 Mar 27 '17 at 18:17