ninithi to be released.......

Ninith, a software for nano technology........

A new visualizing and analyzing software for Nanotechnology....This is an opensource software. 1 st versionof ninithi will be released by the end of March 2010.

ninithi Home page

How to draw a cylinder in Java 3D between 2 points

If we go through the documentation of Java 3D, we can see that, there is no direct way to draw a cylinder between 2 Cartesian coordinates. A bond is simply a cylinder. Cylinder can be drawn using ‘Cylinder’ class in Java3D.  Cylinder is defined with a radius and a height. It is a capped cylinder centered at the origin with its central axis aligned the Y-axis. The cylinder constructor accepts radius, height, appearance etc. Here, the bond should be drawn between given two points. In Java3D, a cylinder cannot be drawn between given two points directly. Following steps can be identified to draw a bond between given two points.

1.      Translate the center of the cylinder to the middle point of given two points.
2.      Find the angle of rotation.
3.      Find the axis of rotation
Let’s assume, the end points of the bond are p1 (x1, y1, z1) and p2 (x2, y2, z2). Then the height of the cylinder can be calculated by finding the distance between two points, 

Implementing bonds in Java3D

‘Vector3f’ class can be used to represent vectors in Java3D. First of all, the unit vector in the direction of the line connecting two points should be represented by a vector3f object. Cross vector of Y- axis and unit vector in the direction of the line connecting two points should be calculated and assigned to a vector.  Set the angle of rotation about the axis of rotation to an object of ‘AxisAngle4f’. Add this axisangle4f object to an object of ‘Transform3D. Let’s assume this transform3D object as v1.

Translate the center of the cylinder to a new location using an object of ‘Transform3D’. Then multiply this object by transform3D object v1.  Then add the result to an object of ‘TransformGroup’.

Then construct the cylinder with predefined radius, calculated height, custom appearance etc.

Finally, by adding this cylinder to the above transformgroup object, a bond can be constructed.By adding a bond by bond to another new TransformGroup object, the bunch of bonds can be drawn.
Then create an object of ‘BranchGroup’ and add both atoms transformgroup and bond transformgroup to branchgroup object. Finally add the branchgroup object to the ‘SimpleUniverse’ object.

import javax.vecmath.AxisAngle4f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Cylinder;

public class Bond {
    private Vector3f tempVec = new Vector3f();
    private Vector3f crossVec = new Vector3f();
    private static final Vector3f YAXIS = new Vector3f(0, 1, 0);
    private Transform3D tempTrans = new Transform3D();
    private Transform3D tempTrans2 = new Transform3D();
    private AxisAngle4f tempAA = new AxisAngle4f();
    private TransformGroup bondGroup=new TransformGroup();
    private TransformGroup bondsGroup=new TransformGroup();
    public TransformGroup drawBonds(double[][] coordsBond,Appearance appBond,float radious){

                        // coordsBond = {x1,y1,z1,x2,y2,z2}
                        float x,y,z;
                        double dx,dy,dz,length;
            for(int i=0;i                    
                        tempVec.set((float)dx, (float)dy,(float) dz);
                         // Find axis of rotation
            crossVec.cross(YAXIS, tempVec);
         // Find amount of rotation and put into matrix
            tempAA.set(crossVec, (float)Math.acos(;
         // Transform to midpoint between two nodes
            tempTrans2.setTranslation(new Vector3f(x/2, y/2, z/2));
            bondGroup=new TransformGroup(tempTrans2);          
            Cylinder bond=new Cylinder(radious, (float)length,appBond);
            }//End of for loop
                        return bondsGroup;

Have you heard about

We (myself, Rupe, Chamitha and Gamage) launched a website with the sole objective to help to preserve the environment. we, 4 of us, are Electronics and Telecommunication engineers graduated from University of Moratuwa, Sri Lanka. As engineers we have responsible to achieve great successes using modern sophisticated technologies without causing any problems to the environment. We hope to educate various communities about environmental issues and solution to those issues.

About Me

My photo
I'm specialized in Electronic and Telecommunication Engineering from University of Moratuwa, Sri Lanka. Currently I'm working for Lanka Software Foundation. Our main objective is to develop an open source software for NANOTECHNOLOGY. These days I'm working with scientists in Sri Lanka Institute of Nanotechnolgy to identify the required domain.