-3
我有一個Graph的實現,我想用JUNG將它可視化。我的問題是,當我添加一條邊時,我想象出兩條邊,例如1到2和2到1的重量相同,但我只想看到1條邊。如何用榮格庫繪製邊緣?
我的代碼:
public void createGraph(HashMap<Integer, Vertice<Integer, Integer>> vertices, long[][] matriz) {
if (vertices != null) {
Graph<Vertice<Integer, Integer>, Arista<Integer, Integer>> ig = new SparseMultigraph<Vertice<Integer, Integer>, Arista<Integer, Integer>>();
for (int i = 0; i < vertices.size(); i++) {
ig.addVertex(vertices.get(i));
}
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < i; j++) {
if(matriz[i][j] > 0){
Arista<Integer, Integer> a= new Arista(vertices.get(i), vertices.get(j), (int)matriz[i][j]);
ig.addEdge(a, vertices.get(i), vertices.get(j));
}
}
}
VisualizationImageServer<Vertice<Integer, Integer>, Arista<Integer, Integer>> vs = new VisualizationImageServer<Vertice<Integer, Integer>, Arista<Integer, Integer>>(
new KKLayout<Vertice<Integer, Integer>, Arista<Integer, Integer>>(ig), new Dimension(680, 340));
vs.setBackground(Color.GRAY);
Transformer<Vertice<Integer, Integer>, Paint> vertexColor = new Transformer<Vertice<Integer, Integer>, Paint>() {
@Override
public Paint transform(Vertice<Integer, Integer> i) {
return Color.GREEN;
}
};
vs.getRenderContext().setVertexFillPaintTransformer(vertexColor);
vs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Vertice<Integer, Integer>>());
vs.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
vs.getRenderContext().setEdgeLabelTransformer(new Transformer<Arista<Integer, Integer>, String>() {
public String transform(Arista<Integer, Integer> e) {
return (e.toString());
}
});
this.add(vs, BorderLayout.CENTER);
}
}
您的問題可能在於創建邊緣的double循環。你的'matriz'結構是否有冗餘信息,例如是'matriz [i] [j] == matriz [j] [i]'? – Oneiros
是... matriz [i] [j] == matriz [j] [i] – Mishale
但這就是爲什麼我只是在下三角矩陣中迭代。 – Mishale