gpt4 book ai didi

java - 在图内插入顶点

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:10 25 4
gpt4 key购买 nike

我想在现有图形中添加一个新顶点。因此,我创建了一个新单元格,并尝试重新连接我的边缘,但我的图表没有更新(对于边缘)

这是我的代码:

    mxGraph graph = editor.getGraph();

mxCell selectedElt = (mxCell) graph.getSelectionCell();
Object cells[] = { selectedElt };

if (selectedElt.isEdge()) {
// cell is an edge, so we have source and target
System.out.println("Source : " + selectedElt.getSource().getId());
System.out.println("Target : " + selectedElt.getTarget().getId());
} else {
// edge before
mxCell beforeEdge = (mxCell) selectedElt.getEdgeAt(0);
// edge after
mxCell afterEdge = (mxCell) selectedElt.getEdgeAt(1);
// moving down the selected cell
graph.moveCells(cells, 0, 50);

// create a new vertex
GraphStyle graphStyle = new GraphStyle(graph);
mxCell cell = new mxCell("AAM",
new mxGeometry(selectedElt.getGeometry().getX(), selectedElt.getGeometry().getY(), 80, 50),
graphStyle.getCalculatorStyleName());
cell.setVertex(true);

beforeEdge.setTarget(cell);
graph.insertEdge(graph.getDefaultParent(), "e33", "", cell, selectedElt);

graph.addCell(cell);
graph.repaint();
}

最佳答案

不要调用 beforeEdge.setTarget(cell),而是尝试 cell.insertEdge(beforeEdge, false)。这将从前一个顶点中删除边并将其添加到新顶点。

顺便说一句。我建议将代码包装到 try-finally block 中,如下所示:

graph.getModel().beginUpdate();
try {
// do all the graph related stuff
}
finally {
graph.getModel().endUpdate();
}

关于java - 在图内插入顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com