我使用 JUNG java 库来管理图形,我想知道如何用相同的颜色为连接的节点着色以区分连接的组件。
例如,我希望节点2
和具有相同的颜色,并且节点3
具有另一种颜色,知道邻接矩阵是:4
1
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
创建图形的代码(节点具有相同的颜色):
Graph<Integer, String> g;
/** Creates a new instance of SimpleGraphView */
public ReadFile(int nbsommet,int [] [] nodeMat) {
// Graph<V, E> where V is the type of the vertices and E is the type of the edges
// Note showing the use of a SparseGraph rather than a SparseMultigraph
g = new SparseGraph<Integer, String>();
// Add some vertices. From above we defined these to be type Integer.
/*for (int i = 1; i <=nbsommet; i++) {
g.addVertex((Integer)i);
}*/
for (int i = 1; i <=nbsommet; i++)
{ g.addVertex((Integer)i);
for (int j = 1; j<=nbsommet; j++)
{
if((nodeMat[i][j]==1)&& (j>i))
{ if(!(g.getVertices().contains(j)))
{ g.addVertex((Integer)j);}
g.addEdge(i+" "+j, i, j);
}
}}}
//...
ReadFile sgv = new ReadFile(nbsommet,nodeMatfinal); // This builds the graph
//Design sgv1 = new Design();
Layout<Integer, String> layout = new KKLayout (sgv.g);
//TreeLayout layout = new TreeLayout(sgv.g,100,100);
layout.setSize(new Dimension(800,800));
BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<Integer, String>(layout);
Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {
public Paint transform(Integer i) {
return (Paint) Color.GREEN;
}
};
vv.setPreferredSize(new Dimension(850,850));
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.green));
vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.white));
vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));
vv.getRenderContext().setVertexFillPaintTransformer((Transformer<Integer, java.awt.Paint>) vertexPaint);
vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.green, Color.yellow));
vv.setBackground(Color.gray);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
JFrame frame = new JFrame("Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
那么如何更改此代码以使连接的组件具有不同的颜色