我有一个顶点(顶点A)连接到两个不同的顶点(B& C)。但它被复制并显示相同的顶点(A)连接到两个不同的顶点(B& C)。如何制作一个有两条边出现并连接到B& B的放大器的顶点(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();
}
}
答案 0 :(得分: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);
}