Can JGraphT java library be used for making 3D graphs -
basically have plot 3d undirected graph list of vertices.at present can plot undirected graph in 2d , have used following code this.
import java.awt.color; import java.awt.dimension; import java.awt.rectangle; import java.awt.geom.rectangle2d; import java.util.hashmap; import java.util.map; import javax.swing.japplet; import javax.swing.jframe; import org.jgraph.jgraph; import org.jgraph.graph.defaultgraphcell; import org.jgraph.graph.graphconstants; import org.jgrapht.listenablegraph; import org.jgrapht.ext.jgraphmodeladapter; import org.jgrapht.graph.listenabledirectedgraph; import org.jgrapht.graph.defaultedge; public class jgraphadapterdemo extends japplet { private static final color default_bg_color = color.decode( "#fafbff" ); private static final dimension default_size = new dimension( 530, 320 ); // private jgraphmodeladapter m_jgadapter; /** * @see java.applet.applet#init(). */ public void init( ) { // create jgrapht graph listenablegraph g = new listenabledirectedgraph( defaultedge.class ); // create visualization using jgraph, via adapter m_jgadapter = new jgraphmodeladapter( g ); jgraph jgraph = new jgraph( m_jgadapter ); adjustdisplaysettings( jgraph ); getcontentpane( ).add( jgraph ); resize( default_size ); // add sample data (graph manipulated via jgrapht) g.addvertex( "v1" ); g.addvertex( "v2" ); g.addvertex( "v3" ); g.addvertex( "v4" ); g.addedge( "v1", "v2" ); g.addedge( "v2", "v3" ); g.addedge( "v3", "v1" ); g.addedge( "v4", "v3" ); // position vertices nicely within jgraph component positionvertexat( "v1", 50, 50 ); positionvertexat( "v2", 120, 120 ); positionvertexat( "v3", 200,200 ); positionvertexat( "v4", 20, 20 ); // that's there it!... } private void adjustdisplaysettings( jgraph jg ) { jg.setpreferredsize( default_size ); color c = default_bg_color; string colorstr = null; try { colorstr = getparameter( "bgcolor" ); } catch( exception e ) {} if( colorstr != null ) { c = color.decode( colorstr ); } jg.setbackground( c ); } private void positionvertexat( object vertex, int x, int y ) { defaultgraphcell cell = m_jgadapter.getvertexcell( vertex ); map attr = cell.getattributes( ); rectangle2d b = graphconstants.getbounds(attr); graphconstants.setbounds( attr, new rectangle( x, y, b.out_bottom, b.out_top ) ); map cellattr = new hashmap( ); cellattr.put( cell, attr ); m_jgadapter.edit( cellattr, null, null, null); } }
now want know can extend code , plot undirected graph in 3d using jgrapht library.is there else more useful me in fulfulling task other this.
i have implemented above task.i have not done using jgrapht library instead did using java 3d only.below code shows 3d undirected graph of tetrahedron made using java 3d.
import javax.media.j3d.appearance; import javax.media.j3d.boundingsphere; import javax.media.j3d.branchgroup; import javax.media.j3d.geometryarray; import javax.media.j3d.linestriparray; import javax.media.j3d.shape3d; import javax.media.j3d.transformgroup; import javax.vecmath.point3d; import com.sun.j3d.utils.behaviors.mouse.mouserotate; import com.sun.j3d.utils.universe.simpleuniverse; public class test { public test(){ simpleuniverse u=new simpleuniverse(); branchgroup group=new branchgroup(); point3d coords[] = new point3d[9]; appearance app=new appearance(); coords[0] = new point3d(0.5d, 0.5d, 0.5d); coords[1] = new point3d(1.0d, 0.5d, 0.5d); coords[2] = new point3d(0.5d, 0.5d, 0.5d); coords[3] = new point3d(0.5d, 1.0d, 0.5d); coords[4] = new point3d(0.5d, 0.5d, 0.5d); coords[5] = new point3d(0.5d, 0.5d, 1.0d); coords[6] = new point3d(1.0d, 0.5d, 0.5d); coords[7] = new point3d(0.5d, 1.0d, 0.5d); coords[8] = new point3d(0.5d, 0.5d, 1.0d); int vertexcounts[] = {9}; linestriparray lines = new linestriparray(9, geometryarray.coordinates, vertexcounts); lines.setcoordinates(0, coords); shape3d shape=new shape3d(lines , app); //group.addchild(shape); transformgroup objrotate = new transformgroup(); objrotate.setcapability(transformgroup.allow_transform_write); objrotate.addchild(shape); mouserotate f1=new mouserotate(); f1.setschedulingbounds(new boundingsphere()); f1.settransformgroup(objrotate); group.addchild(f1); group.addchild(objrotate); u.addbranchgraph(group); u.getviewingplatform().setnominalviewingtransform(); } }
the image of tetrahedron implemented using given below:
Comments
Post a Comment