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

3

我有一个单独的顶点(顶点A)与两个不同的顶点(B和C)相连。但它被复制了,显示相同的顶点(A)与两个不同的顶点(B和C)相连。如何使一个单独的顶点(A)有两个出边并连接到B和C。

    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();
        }
    }

添加代码片段可以帮助其他人更好地回答问题。 - Arun Xavier
添加了用于绘制图形的代码片段。 - span
搞清楚一件事:您多次调用了“insertVertex”,想知道为什么顶点会被多次插入? - Marco13
1个回答

1
问题在于您多次调用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);
}        

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接