簡體   English   中英

Jung API - 如何在兩個現有節點之間添加新 Edge

[英]Jung API - How to add new Edge between two existing nodes

我試着像這樣用 Jung 做一個格子:

我的目標

到目前為止,我在 2 個階段之間建立了鏈接,但我不知道如何在 2 個現有的 Vertex 之間建立鏈接。

這里是階段 1 和 2 之間的鏈接:

我的目標

這里是階段 2 和 3 之間的鏈接:

我的目標

這里是階段 3 和 4 之間的鏈接:

我的目標

問題是我無法將所有階段放在一起,因為我無法添加具有現有頂點的 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);
    }
}

JUNG 將樹定義為從(指定的)根到任何頂點只有一條路徑的圖。

格子不是樹,也不是森林,所以你不能把你想要的圖構造成樹/森林; 您需要使用一般的Graph類型。

這也意味着您不能(直接)在圖表上使用TreeLayout 您可以構建圖形的生成樹並在其上使用TreeLayout ,但它可能看起來不像上面的圖表。

暫無
暫無

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

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