java - Using the library JUNG graphs -
i use jung java library manage graphs , want know how color connected nodes same color distinguish connected components.
for example, want nodes 2
, 3
, 4
have same color , node 1
to have color, knowing adjacency matrix is:
0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0
code create graph (nodes have same color):
graph<integer, string> g; /** creates new instance of simplegraphview */ public readfile(int nbsommet,int [] [] nodemat) { // graph<v, e> v type of vertices , e type of edges // note showing use of sparsegraph rather sparsemultigraph g = new sparsegraph<integer, string>(); // add vertices. above defined these type integer. /*for (int = 1; <=nbsommet; i++) { g.addvertex((integer)i); }*/ (int = 1; <=nbsommet; i++) { g.addvertex((integer)i); (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); // builds 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);
so how change code have connected components different colors
the jung library not let set different renderers per vertex, instead use 1 renderer , put of logic there. (i got idea this post).
you have determine vertices should colors, , put either in method call or map<integer, color>
. assume latter:
final map<integer,color> colormapping = new hashmap<integer, color>(); // ..... transformer<integer,paint> vertexpaint = new transformer<integer,paint>() { public paint transform(integer i) { return colormapping.get(i.intvalue()); } };
obviously leaves question how fill colormapping
.
need determine connected components adjacency matrix; separate problem has been handled in this stackoverflow post.
once have separate subgraphs, left assign colors them, , fill colormapping
accordingly.
a few notes on code sample:
i had comment call out:
vv.getrendercontext().setvertexfillpainttransformer(new pickablevertexpainttransformer<integer>(vv.getpickedvertexstate(), color.green, color.yellow));
it sets new painttransformer replaces 1 calls
vertexpaint
.your graph initialization 1-based, array 0-based. should change
if((nodemat[i][j]==1)&& (j>i))
into
if((nodemat[i-1][j-1]==1)&& (j>i))
or arrayindexoutofboundsexception.
Comments
Post a Comment