【问题标题】:Removing duplicate vertices in a graph using JGraphX使用 JGraphX 删除图中的重复顶点
【发布时间】:2016-03-15 19:24:57
【问题描述】:

我有一个顶点(顶点 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();
        }
    }

【问题讨论】:

  • 添加代码sn-p有助于其他人回答。
  • 添加了用于绘制图形的代码sn-p
  • 直截了当地说:您多次调用insertVertex,想知道为什么要多次插入顶点?

标签: java jgraphx


【解决方案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);
}        

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 2015-10-09
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多