java - Jung API - 如何在两个现有节点之间添加新边缘
问题描述
我试着像这样和 Jung 做一个格子:
到目前为止,我在 2 个阶段之间建立了联系,但我不知道如何在 2 个现有 Vertex 之间建立联系。
这里是第 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)
这是我的代码:
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
在其上使用,但它可能看起来不像上面的图表。
推荐阅读
- python - 在 django 模型中更改 primary_key
- docker - 为什么docker上的ElasticSearch拒绝来自另一个docker容器的连接但没有拒绝curl get
- javascript - 在 Chrome 扩展中引用 JS 库
- jquery - 如何使用左边缘或右边缘而不是中心分配 jQuery 滑块句柄值?
- r - R Tensorflow - 如何升级到 tf v2
- c# - 在 MVC 中有条件地显示动作链接
- javascript - 如何使用 vue-router 正确设置 main.js 和 layout.vue
- django - Django - 多张照片上传
- python - 如何通过余弦相似度传递用户输入?
- oracle - Oracle:显示不同的保持顺序