java - 如何用JUNG2画直线?

标签 java swing visualizer 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;博士:

通话

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.

关于java - 如何用JUNG2画直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29587011/

相关文章:

java - java中的pl/sql varchar2类型是什么?

编译克隆方法时出现 Java StackOverflowError

java - 如何打印位图 TM-T88V

java - 使用 JDK 1.6 从 .mp3 读取数据的最佳插件

java - 在 Eclipse 中调试大插件

java - 如何将图像添加到动态创建的 JTable

Java Swing - 如何在 Canvas 中绘制新线并将其添加到现有线中?

java - 为自定义驱动程序创建 GraphicsDevice

ios - 是否有任何 iOS 粒子系统库

android - 获取歌曲的所有声音字节以用于Android应用