簡體   English   中英

如何用JUNG2畫直線?

[英]How to draw Straight line by JUNG2?

經過JUNG2的測試后,我發現所有邊緣線都是彎曲的,但不是直線...如何通過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);
    }

}

跟隨輸出結果,黑線是默認值,紅線是我想要的: http : //www.zhaocs.info/wp-content/uploads/2015/04/test.png

tl; dr

呼叫

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

在您的VisualizationViewer


JUNG中的邊緣形狀有多種選擇。 這些選項被實現為edu.uci.ics.jung.visualization.decorators.EdgeShape類的嵌套類“邊緣形狀變換器”。 默認值為EdgeShape.QuadCurve ,它會導致彎曲的邊緣。 其他選項包括(摘自文檔):

 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. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM