Jung API-如何在两个现有节点之间添加新Edge

问题描述 投票:1回答:1

我试图像这样用荣格制作格子:

my target

直到现在,我在两个阶段之间建立了链接,但是我不知道如何在两个现有顶点之间建立链接。

这里是第一阶段和第二阶段之间的联系:

my target

这里是第2阶段和第3阶段之间的链接:

my target

这里是第3阶段和第4阶段之间的链接:

my target

问题是,由于无法添加具有现有顶点的Edge,因此无法将所有阶段组合在一起。它将导致此错误:

Exception in thread "main" java.lang.IllegalArgumentException: Tree must not already contain child µ1234
    at edu.uci.ics.jung.graph.DelegateTree.addChild(DelegateTree.java:182)
    at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:102)
    at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:346)
    at edu.uci.ics.jung.graph.util.TreeUtils.growSubTree(TreeUtils.java:76)
    at edu.uci.ics.jung.graph.util.TreeUtils.growSubTree(TreeUtils.java:80)
    at edu.uci.ics.jung.graph.DelegateForest.getTrees(DelegateForest.java:295)
    at edu.uci.ics.jung.graph.util.TreeUtils.getRoots(TreeUtils.java:34)
    at edu.uci.ics.jung.algorithms.layout.TreeLayout.buildTree(TreeLayout.java:102)
    at edu.uci.ics.jung.algorithms.layout.TreeLayout.<init>(TreeLayout.java:97)
    at edu.uci.ics.jung.algorithms.layout.TreeLayout.<init>(TreeLayout.java:75)
    at code.Gui_Arbre.<init>(Gui_Arbre.java:48)
    at code.Gui_Arbre.main(Gui_Arbre.java:171)

这是我的代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JFrame;
import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.functors.ConstantTransformer;
import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.Forest;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;

@SuppressWarnings({ "serial", "deprecation" })
public class Gui_Arbre extends JApplet {
    private Fuzzy_Mesure fm;
    /**
     * le graph
     */
    Forest<U, Integer> graph;
    // mod?le de lien entre noeud
    Factory<Integer> edgeFactory = new Factory<Integer>() {
        int i = 0;

        public Integer create() {
            return i++;
        }
    };
    /**
     * l'?l?ment visuel
     */
    VisualizationViewer<U, Integer> vv;
    TreeLayout<U, Integer> layout;

    @SuppressWarnings("unchecked")
    public Gui_Arbre(Fuzzy_Mesure fm) {
        // create a simple graph for the demo
        graph = new DelegateForest<U, Integer>();
        this.fm = fm;
        createTree();
        layout = new TreeLayout<U, Integer>(graph);
        vv = new VisualizationViewer<U, Integer>(layout, new Dimension(600, 600));
        vv.setBackground(Color.white);
        // personnalisation des fleches
        vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<U, Integer>());
        vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
        // affiche les labels
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<U>());

        Container content = getContentPane();
        final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
        content.add(panel);
        final DefaultModalGraphMouse<U, Integer> graphMouse = new DefaultModalGraphMouse<>();
        vv.setGraphMouse(graphMouse);
    }

    /**
     * cr?ation de l'arbre
     */

    private void createTree() {
        for (int i = 0; i < this.fm.getLevel(); i++) {// parcourir etage
            for (int y = 0; y < this.fm.getMap().get(i).size(); y++) {// parcourir list de l'etage

                // create a link between stage 1 and 2
                if (i == this.fm.getLevel() - 2) {

                    for (int o = 0; o < this.fm.getMap().get(i).get(y).getEnfant().size(); o++) {
                        graph.addEdge(edgeFactory.create(), this.fm.getMap().get(i).get(y),
                                this.fm.getMap().get(i).get(y).getEnfant().get(o));

                    }

                }
                if (i == this.fm.getLevel() - 3) {// create a link between stage 2 and 3, but as stage 2 already exists
                                                    // with existing Vertex, there is an error

                    for (int o = 0; o < this.fm.getMap().get(i).get(y).getEnfant().size(); o++) {
                        graph.addEdge(edgeFactory.create(), this.fm.getMap().get(i).get(y),
                                this.fm.getMap().get(i).get(y).getEnfant().get(o));

                    }

                }

            }

        }

    }

    /**
     * Tests
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        Container content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        List<U> set = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            set.add(new U(1));
        }
        Fuzzy_Mesure fm = new Fuzzy_Mesure(set);
        content.add(new Gui_Arbre(fm));

        frame.pack();
        frame.setVisible(true);
    }
}
java jung
1个回答
0
投票

JUNG defines a tree作为图,从(指定的)根到任何顶点都只有一条路径。

格子不是树,也不是森林,因此您不能将想要的图形构造为树/森林;您需要使用常规的Graph类型。

这也意味着您不能(直接)在图形上使用TreeLayout。您可以构建图形的生成树并在其上使用TreeLayout,但它看起来可能与上面的图不一样。

© www.soinside.com 2019 - 2024. All rights reserved.