如何用Jung库绘制边缘?

时间:2017-05-24 15:25:27

标签: java graph jung

我有一个Graph的实现,我想用JUNG可视化它。我的问题是,当我添加边缘时,我可视化2个边缘,例如1到2和2到1具有相同的权重,但我只想看到1个边缘。

我的代码:

public void createGraph(HashMap<Integer, Vertice<Integer, Integer>> vertices, long[][] matriz) {

    if (vertices != null) {

        Graph<Vertice<Integer, Integer>, Arista<Integer, Integer>> ig = new SparseMultigraph<Vertice<Integer, Integer>, Arista<Integer, Integer>>();
        for (int i = 0; i < vertices.size(); i++) {
            ig.addVertex(vertices.get(i));
        }


        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < i; j++) {
                if(matriz[i][j] > 0){
                    Arista<Integer, Integer> a= new Arista(vertices.get(i), vertices.get(j), (int)matriz[i][j]);
                    ig.addEdge(a, vertices.get(i), vertices.get(j));
                }                   
            }
        }


        VisualizationImageServer<Vertice<Integer, Integer>, Arista<Integer, Integer>> vs = new VisualizationImageServer<Vertice<Integer, Integer>, Arista<Integer, Integer>>(
                new KKLayout<Vertice<Integer, Integer>, Arista<Integer, Integer>>(ig), new Dimension(680, 340));
        vs.setBackground(Color.GRAY);


        Transformer<Vertice<Integer, Integer>, Paint> vertexColor = new Transformer<Vertice<Integer, Integer>, Paint>() {
            @Override
            public Paint transform(Vertice<Integer, Integer> i) {
                return Color.GREEN;
            }
        };

        vs.getRenderContext().setVertexFillPaintTransformer(vertexColor);


        vs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Vertice<Integer, Integer>>());
        vs.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);


        vs.getRenderContext().setEdgeLabelTransformer(new Transformer<Arista<Integer, Integer>, String>() {
            public String transform(Arista<Integer, Integer> e) {
                return (e.toString());
            }
        });

        this.add(vs, BorderLayout.CENTER);
    }
}

1 个答案:

答案 0 :(得分:1)

您有一个无向图,因此您应该使用UndirectedGraph的实现。 SparseMultigraph允许有向和无向边缘,默认边缘方向性被定向&#39;。

您想要的课程可能是UndirectedSparseGraph