2016-03-15 1 views
3

J'ai un seul sommet (sommet A) connecté à deux sommets différents (B & C). Mais il est dupliqué et montre le même sommet (A) connecté aux deux sommets différents (B & C). Comment puis-je faire un seul sommet (A) avec 2 bords sortant et étant relié à B & C.Suppression de sommets dupliqués dans un graphique à l'aide de JGraphX ​​

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

ajoutant extrait de code serait utile d'autres pour répondre. –

+0

Ajout de l'extrait de code utilisé pour tracer le graphique – span

+0

Pour obtenir ceci: Vous appelez plusieurs fois 'insertVertex' et vous vous demandez pourquoi le sommet est inséré plusieurs fois? – Marco13

Répondre

1

Le problème est que vous appelez insertVertex plusieurs fois. Chaque fois, un nouveau sommet sera créé. Bien que je ne suis pas vraiment familier avec JGraphX, et le code fourni à ce jour était loin d'être compilable, le problème peut probablement être résolu en insérant les sommets et les arêtes séparément:

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