java - Is it possible add multiple edges using TreeLayout? -
i've display graph tree level in hierarchy use treelayout included in jung, don't know if it's possible use layout add multiple edges between nodes. , if it's not possible, how recommend me that?
thanks!
public class visualizacion extends japplet { /** * graph */ graph<string,string> graph; forest<string,string> tree; funciones f=new funciones(); /** * visual component , renderer graph */ visualizationviewer<string,string> vv; string root; layout<string,string> layout; layout<string,string> layout2; ; public visualizacion(org.graphstream.graph.graph grafito) { graph= new directedsparsemultigraph<string, string>(); createtree(grafito); minimumspanningforest2<string,string> prim = new minimumspanningforest2<string,string>(graph, new delegateforest<string,string>(), delegatetree.<string,string>getfactory(), new constanttransformer(1.0)); tree = prim.getforest(); layout = new treelayout<string,string>(tree,100,100); layout2 = new staticlayout<string,string>(graph, layout); transformer<string,paint>vertexpaint= new transformer<string,paint>(){ public paint transform(string i){ node node=grafito.getnode(i); if(node.hasattribute("excedecaudal")){ return color.red;} else{ return color.lightgray;} } }; vv = new visualizationviewer<string,string>(layout2, new dimension(800,600)); vv.addgraphmouselistener(new testgraphmouselistener<string>()); vv.setbackground(color.white); vv.getrendercontext().setedgeshapetransformer(new edgeshape.line()); vv.getrendercontext().setvertexlabeltransformer(new tostringlabeller()); vv.getrendercontext().setedgelabeltransformer(new tostringlabeller()); vv.getrendercontext().setvertexshapetransformer(new clustervertexshapefunction()); vv.getrendercontext().setvertexfillpainttransformer(vertexpaint); // add listener tooltips vv.setvertextooltiptransformer(new tostringlabeller()); vv.getrendercontext().setarrowfillpainttransformer(new constanttransformer(color.lightgray)); vv.getrendercontext().setedgelabeltransformer(new edgelabeltransformer<string>()); container content = getcontentpane(); final graphzoomscrollpane panel = new graphzoomscrollpane(vv); content.add(panel); final defaultmodalgraphmouse graphmouse = new defaultmodalgraphmouse(); vv.setgraphmouse(graphmouse); //ver cual nodo se selecciona final pickedstate<string> pickedstate=vv.getpickedvertexstate(); pickedstate.additemlistener(new itemlistener(){ @override public void itemstatechanged(itemevent e){ object subject=e.getitem(); if(subject instanceof string){ string vertice=(string)subject; if(pickedstate.ispicked(vertice)){ system.out.println("vertice "+vertice+" está seleccionado"); } else{ system.out.println("vertice "+vertice+"no está seleccionado"); } } } }); }private void createtree(org.graphstream.graph.graph grafito) { for(node node:grafito){ graph.addvertex(node.getid()); } int count=0; (edge edge: grafito.geteachedge()){ string padre=edge.getnode0().getid(); string hijo=edge.getnode1().getid(); string caudal=(edge.getattribute("caudal")); graph.addedge(caudal+"-"+count, padre,hijo,edgetype.directed); system.out.println("intento agregar "+edge.getattribute("caudal")+" cuyo padre es "+padre+" e hijo "+hijo); count++; }}private class edgelabeltransformer<v>implements transformer<v,string>{ public string transform(v v){ return v.tostring().split("-")[0]; } } class clustervertexshapefunction<v> extends ellipsevertexshapetransformer<v> { clustervertexshapefunction() { setsizetransformer(new clustervertexsizefunction<v>(20)); } @suppresswarnings("unchecked") @override public shape transform(v v) { if(v instanceof graph) { int size = ((graph)v).getvertexcount(); if (size < 8) { int sides = math.max(size, 3); return factory.getregularpolygon(v, sides); } else { return factory.getregularstar(v, size); } } return super.transform(v); } } class clustervertexsizefunction<v> implements transformer<v,integer> { int size; public clustervertexsizefunction(integer size) { this.size = size; } public integer transform(v v) { if(v instanceof graph) { return 30; } return size; } } static class testgraphmouselistener<v> implements graphmouselistener<v> { public void graphclicked(v v, mouseevent me) { if(me.getclickcount()==2){ system.err.println("vertex "+v+" fui doble click"); } system.err.println("vertex "+v+" clicked @ ("+me.getx()+","+me.gety()+")"); } public void graphpressed(v v, mouseevent me) { system.err.println("vertex "+v+" pressed @ ("+me.getx()+","+me.gety()+")"); } public void graphreleased(v v, mouseevent me) { system.err.println("vertex "+v+" released @ ("+me.getx()+","+me.gety()+")"); } } public void execute(org.graphstream.graph.graph grafito){ jframe frame = new jframe(); container content = frame.getcontentpane(); frame.setdefaultcloseoperation(jframe.dispose_on_close); content.add(new visualizacion(grafito)); frame.pack(); frame.setvisible(true);}}
jung layout
instances determine vertex positions, not edge positions; edge rendering determined automatically based on edge shape use , number of connecting edges.
jung tree
objects, however, can have 1 edge connecting pair of vertices (otherwise it's not tree).
so if want graph
vertices laid out using treelayout
, not directly representable tree
, can do:
(1) construct tree
encodes structural relationships want (from original graph
), doesn't have parallel edges want. can either directly (by constructing copy removing parallel edges exist, or not adding them if that's feasible), or using minimumweightspanningtree
algorithm extract tree
original graph
.
(2) generate treelayout
tree
.
(3) create staticlayout
copies positions used treelayout
.
(4) use staticlayout
layout algorithm original graph
.
you can see process demonstrated in jung minimumspanningtreedemo.java
, in provided sample code that's part of jung source distribution. if want see code, it's here: http://jung.sourceforge.net/site/jung-samples/xref/edu/uci/ics/jung/samples/minimumspanningtreedemo.html
Comments
Post a Comment