gpt4 book ai didi

java - 使用 JGraphX 删除图中的重复顶点

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:31 24 4
gpt4 key购买 nike

我有一个顶点(顶点 A)连接到两个不同的顶点(B 和 C)。但它被复制并显示相同的顶点 (A) 连接到两个不同的顶点 (B & C)。我怎样才能制作出带有 2 条边并连接到 B 和 C 的单个顶点 (A)。

    for (int i = 0; i < cardList.getSize(); i++) {
try {

Object v1 = graph.insertVertex(graph.getDefaultParent(), null, Card, x, y, cardWidth, cardHeight);
Object v2 = graph.insertVertex(graph.getDefaultParent(), null, card.getConnectedCard(i), x + cardWidth + 50, y, cardWidth, cardPanelHeight);
Object e1 = graph.insertEdge(graph.getDefaultParent(), null, "", v1, v2);

} finally {
graph.getModel().endUpdate();
}
}

最佳答案

问题是您多次调用 insertVertex。每次都会创建一个新的顶点。虽然我对 JGraphX 不是很熟悉,而且目前提供的代码远不能编译,但问题可能可以通过分别插入顶点和边来解决:

// First, insert all vertices into the graph, and store
// the mapping between "Card" objects and the corresponding
// vertices
Map<Card, Object> cardToVertex = new LinkedHashMap<Card, Vertex>();
for (int i = 0; i < cardList.getSize(); i++)
{
Card card = cardList.get(i);
Object vertex = graph.insertVertex(
graph.getDefaultParent(), null, card, x, y, cardWidth, cardHeight);
cardToVertex.put(card, vertex);
}

// Now, for each pair of connected cards, obtain the corresponding
// vertices from the map, and create an edge for these vertices
for (int i = 0; i < cardList.getSize(); i++)
{
Card card0 = cardList.get(i);
Card card1 = card0.getConnectedCard(i);

Object vertex0 = cardToVertex.get(card0);
Object vertex1 = cardToVertex.get(card1);
Object e1 = graph.insertEdge(
graph.getDefaultParent(), null, "", vertex0, vertex1);
}

关于java - 使用 JGraphX 删除图中的重复顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003091/

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