Bitcoin

Bitcoin
Bitcoin

Java code to display 3D spherical ball

//NOTE: You must download java 3D if you IDE does not support it
// From http://download.java.net/media/java3d/builds/release/1.5.1/README-download.html

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

/**
 *
 * @author Sambyte
 */
public class Ball {
    public Ball(){
        SimpleUniverse v=new SimpleUniverse();//To create an object of the virtual environment
        BranchGroup g = new BranchGroup();//The child of the simpleUniverse to hold all siblings
        Sphere s = new Sphere(0.5f);// Object of the sphere with a float as the radius
        g.addChild(s);// adding the sphere object to the branchgroup
        Color3f l = new Color3f(1.8f,0.1f,0.1f);//color parameters of the sphere
        BoundingSphere sh = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
        Vector3f ve= new Vector3f(4.0f,-7.0f,-12.0f);
        DirectionalLight light = new DirectionalLight(l,ve);//direction viewing light in the virtual world
        light.setInfluencingBounds(sh);
        g.addChild(light);
        v.getViewingPlatform().setNominalViewingTransform();
        v.addBranchGraph(g);
    }
    public static void main(String[] args){
        new Ball();
    }
}

No comments:

Post a Comment

Facebook