java3d 101 question

Dear all,

I start to learn java 3d. I am trying to put multiple thing on one graph. I have no clue why the sphere is black in color, even I have set the appearance to be blue. Please help me to read the following code and tell me what problem on the sphere part. Thanks . I hope the picture can display

[url #" style="display: block; background-image: url('http://i211.photobucket.com/albums/bb162/zmarcoz/Untitled.jpg'); [/url]

CODE

/*

* lesson01.java

*

* Created on June 14, 2007, 5:30 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/*

* Main.java

*

* Created on June 14, 2007, 5:28 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package java3d01;

// First we import packages that I use for Java3D

import java.awt.Frame;

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

import com.sun.j3d.utils.applet.MainFrame;

import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;

import javax.media.j3d.*;

import javax.vecmath.*;

publicclass Toolsextends Applet{// notice'lesson01', which is also the name of the file : lesson01.java

SimpleUniverse simpleU;// this is the SimpleUniverse Class that is used for Java3D

public Tools (){// this constructor is sometimes needed, even when empty as in here

}

publicvoid init(){

// this function will be called by both applications and applets

//this is usually the first function to write

setLayout(new BorderLayout());// standard Java code for BorderLayout

// Canvas3D is where all the action will be taking place, don't worry, after adding it

// to your layout, you don't have to touch it.

Canvas3D c =new Canvas3D(SimpleUniverse.getPreferredConfiguration());

// add Canvas3D to center of BorderLayout

add("Center", c);

simpleU=new SimpleUniverse(c);// setup the SimpleUniverse, attach the Canvas3D

//This is very important, the SceneGraph (where all the action takes place) is created

//by calling a function which here is called 'createSceneGraph'.

//The function is not necessary, you can put all your code here, but it is a

//standard in Java3D to create your SceneGraph contents in the function 'createSceneGraph'

BranchGroup scene = createSceneGraph();

BranchGroup scene1 = createSceneGraph1();

BranchGroup scene2 = createSceneGraph2();

//set the ViewingPlatform (where the User is) to nominal, more on this in the next lesson

simpleU.getViewingPlatform().setNominalViewingTransform();

// this will optimize your SceneGraph, not necessary, but it will allow your program to run faster.

scene.compile();

scene1.compile();

scene2.compile();

simpleU.addBranchGraph(scene);//add your SceneGraph to the SimpleUniverse

simpleU.addBranchGraph(scene1);

simpleU.addBranchGraph(scene2);

}

public BranchGroup createSceneGraph(){

//Here we will create a basic SceneGraph with a ColorCube object

BranchGroup objRoot =new BranchGroup();

TransformGroup cctg =new TransformGroup();// a TransformGroup for the ColorCube called cctg

//TransformGroup cctg1 = new TransformGroup();

ColorCube c =new ColorCube(0.1f);// create a ColorCube object

cctg.addChild(c);// add ColorCube to cctg

//Sphere sphere = new Sphere(0.2f);

//Appearance sphereAppearance = new Appearance();

//sphereAppearance.setColoringAttributes (new ColoringAttributes (new Color3f (0.0f, 0.0f, 1.0f),1));

//cctg1.addChild(sphere);

Transform3D cc3d =new Transform3D();// a Transform3D allows a TransformGroup to move

cc3d.setTranslation(new Vector3f (0.5f ,0.2f ,0.0f ));// set translation to x=0.8, y=1.0, z= -2.0

//Transform3D cc3d1 = new Transform3D(); // a Transform3D allows a TransformGroup to move

//cc3d1.setTranslation(new Vector3f (0.15f ,-0.2f ,0.5f )); // set translation to x=0.8, y=1.0, z= -2.0

cctg.setTransform(cc3d);//

//cctg1.setTransform(cc3d1);

objRoot.addChild(cctg);

//objRoot.addChild(cctg1);

return objRoot;

}

public BranchGroup createSceneGraph1(){

//Here we will create a basic SceneGraph with a ColorCube object

BranchGroup objRoot1 =new BranchGroup();

//TransformGroup cctg = new TransformGroup(); // a TransformGroup for the ColorCube called cctg

TransformGroup cctg1 =new TransformGroup();

//ColorCube c = new ColorCube(0.1f);// create a ColorCube object

//cctg.addChild(c); // add ColorCube to cctg

Sphere sphere =new Sphere(0.2f);

Appearance sphereAppearance =new Appearance();

sphereAppearance.setColoringAttributes (new ColoringAttributes (new Color3f (0.0f, 0.0f, 1.0f),1));

cctg1.addChild(sphere);

//Transform3D cc3d = new Transform3D(); // a Transform3D allows a TransformGroup to move

//cc3d.setTranslation(new Vector3f (0.5f ,0.2f ,0.0f )); // set translation to x=0.8, y=1.0, z= -2.0

Transform3D cc3d1 =new Transform3D();// a Transform3D allows a TransformGroup to move

cc3d1.setTranslation(new Vector3f (0.15f ,-0.2f ,0.5f ));// set translation to x=0.8, y=1.0, z= -2.0

//cctg.setTransform(cc3d); //

cctg1.setTransform(cc3d1);

//objRoot.addChild(cctg);

objRoot1.addChild(cctg1);

return objRoot1;

}

public BranchGroup createSceneGraph2(){

// Create the root of the branch graph

BranchGroup objRoot =new BranchGroup();

// Create the transform group node and initialize it to the

// identity. Add it to the root of the subgraph.

TransformGroup objSpin =new TransformGroup();

objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objRoot.addChild(objSpin);

// Create a simple shape leaf node, add it to the scene graph.

// ColorCube is a Convenience Utility class

objSpin.addChild(new ColorCube(0.4));

// create time varying function to drive the animation

Alpha rotationAlpha =new Alpha(-1, 4000);

// Create a new Behavior object that performs the desired

// operation on the specified transform object and add it into

// the scene graph.

RotationInterpolator rotator =

new RotationInterpolator(rotationAlpha, objSpin);

// a bounding sphere specifies a region a behavior is active

BoundingSphere bounds =new BoundingSphere();

rotator.setSchedulingBounds(bounds);

objSpin.addChild(rotator);

return objRoot;

}// end of createSceneGraph method

publicvoid destroy(){// this function will allow Java3D to clean up upon quiting

simpleU.removeAllLocales();

}

publicstaticvoid main(String[] args){

// if called as an application, a 500x500 window will be opened

Frame frame =new MainFrame(new Tools(), 700, 700);

}

}

[11189 byte] By [marco_wua] at [2007-11-27 8:01:24]
# 1

you never set sphere's appearance to sphereAppearance

to fix replace

Sphere sphere = new Sphere(0.2f);

Appearance sphereAppearance = new Appearance();

sphereAppearance.setColoringAttributes (new ColoringAttributes (new Color3f (0.0f, 0.0f, 1.0f),1));

cctg1.addChild(sphere);

with

Appearance sphereAppearance = new Appearance();

sphereAppearance.setColoringAttributes (new ColoringAttributes (new Color3f (0.0f, 0.0f, 1.0f),1));

Sphere sphere = new Sphere(0.2f,sphereAppearance);

cctg1.addChild(sphere);

seegreena at 2007-7-12 19:43:29 > top of Java-index,Security,Cryptography...
# 2
Thanks
marco_wua at 2007-7-12 19:43:29 > top of Java-index,Security,Cryptography...