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

Java Jung API-如何在两个现有节点之间添加新边,java,jung,Java,Jung,我试着用Jung做一个格子,像这样: 直到现在,我在两个阶段之间建立了链接,但我不知道如何在两个现有顶点之间建立链接 这里是阶段1和阶段2之间的链接: 这里是第2阶段和第3阶段之间的联系: 这里是第3阶段和第4阶段之间的联系: 问题是我不能将所有的阶段放在一起,因为我不能添加一条带有现有顶点的边。 它将导致以下错误: Exception in thread "main" java.lang.IllegalArgumentException: Tree must not already c

我试着用Jung做一个格子,像这样:

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

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

这里是第2阶段和第3阶段之间的联系:

这里是第3阶段和第4阶段之间的联系:

问题是我不能将所有的阶段放在一起,因为我不能添加一条带有现有顶点的边。 它将导致以下错误:

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)
线程“main”java.lang.IllegalArgumentException中的异常:树不能已经包含子级µ1234 位于edu.uci.ics.jung.graph.DelegateTree.addChild(DelegateTree.java:182) 位于edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:102) 位于edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:346) 位于edu.uci.ics.jung.graph.util.TreeUtils.growtubtree(TreeUtils.java:76) 位于edu.uci.ics.jung.graph.util.TreeUtils.growtubtree(TreeUtils.java:80) 位于edu.uci.ics.jung.graph.DelegateForest.getTrees(DelegateForest.java:295) 位于edu.uci.ics.jung.graph.util.TreeUtils.getRoots(TreeUtils.java:34) 位于edu.uci.ics.jung.algorithms.layout.TreeLayout.buildTree(TreeLayout.java:102) 位于edu.uci.ics.jung.algorithms.layout.TreeLayout.(TreeLayout.java:97) 位于edu.uci.ics.jung.algorithms.layout.TreeLayout.(TreeLayout.java:75) 在code.Gui\u Arbre.(Gui\u Arbre.java:48) 位于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.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JApplet;
导入javax.swing.JFrame;
导入org.apache.commons.collections15.Factory;
导入org.apache.commons.collections15.functors.constantttransformer;
导入edu.uci.ics.jung.algorithms.layout;
导入edu.uci.ics.jung.graph.DelegateForest;
导入edu.uci.ics.jung.graph.Forest;
导入edu.uci.ics.jung.visualization.GraphZoomScrollPane;
导入edu.uci.ics.jung.visualization.VisualizationViewer;
导入edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
导入edu.uci.ics.jung.visualization.decorators.EdgeShape;
导入edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
@SuppressWarnings({“串行”、“弃用”})
公共类Gui_Arbre扩展JApplet{
私有模糊测度;
/**
*勒图
*/
森林图;
//因诺厄德连酒店
Factory edgeFactory=新工厂(){
int i=0;
公共整数创建(){
返回i++;
}
};
/**
*l’?l?元素视觉
*/
可视化查看器vv;
树型布局;
@抑制警告(“未选中”)
公共图形用户界面(模糊测量fm){
//为演示创建一个简单的图形
graph=新的DelegateForest();
this.fm=fm;
createTree();
布局=新树形图(图形);
vv=新的VisualizationViewer(布局,新尺寸(600600));
vv.立根地面(颜色:白色);
//肉芽肿人格化
vv.getRenderContext().setEdgeShapeTransformer(新EdgeShape.Line());
vv.getRenderContext().setArrowFillPaintTransformer(新ConstantTransformer(Color.lightGray));
//粘贴标签
vv.getRenderContext().setVertexLabelTransformer(新ToStringLabeler());
容器内容=getContentPane();
最终图形ZoomScrollPane面板=新图形ZoomScrollPane(vv);
内容。添加(面板);
final DefaultModalGraphMouse graphMouse=新的DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
}
/**
*阿尔布雷酒店
*/
私有void createTree(){
对于(int i=0;i
JUNG是一个从(指定的)根到任何顶点只有一条路径的图形

晶格不是树,也不是林,因此无法将图形构造为树/林;您需要使用常规
图形
类型


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

您主要感兴趣的是构造图表还是操纵图形