Hi,
i can't add an object in runtime to a Java 3D environment.
I'm trying to construct a kind of hollow cube, and when you click
somewhere along the axis of the cube, a square appears inside the cube.
See screenshot : http://www.seo-web-hosting.be/hypercube/screenshot.jpg
Now i'm trying to make a square appear by adding a behavior to the
branchgroup and telling it to add a square at runtime, when any key is
pressed.
The results so far have been discouraging :-(.
Sometimes i get an error telling me that only a branchgroup can be
added, and when i try placing the square in a branchgroup, nothing
happens.
I'll add my code below.
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
//BranchGroup addBehavior = new BranchGroup();
Transform3D rotate = new Transform3D();
Transform3D tempRotate = new Transform3D();
rotate.rotX(Math.PI/7.0d);
tempRotate.rotY(Math.PI/4.0d);
rotate.mul(tempRotate);
TransformGroup objRotate = new TransformGroup(rotate);
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
SimpleBehavior myRotationBehavior = new
SimpleBehavior(objRoot);
myRotationBehavior.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myRotationBehavior);
objRoot.addChild(objRotate);
// this is the important code from createSceneGraph(), the rest is to
construct the hollow cube.
//And this is the code from the SimpleBehavior class
import java.awt.event.KeyEvent;
import javax.media.j3d.Behavior;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnAWTEvent;
import java.util.Enumeration;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.BranchGroup;
public class SimpleBehavior extends Behavior {
private BranchGroup objRoot;
SimpleBehavior(BranchGroup objRoot){
this.objRoot = objRoot;
}
//initialize the behavior
// set initial wakeup condition
// called when behavior becomes live
public void initialize(){
//set initial wakeup condition
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
//called by Java3D when appropriate stimulus occurs
public void processStimulus(Enumeration criteria){
//create square environment
QuadArray qaEnvironment = new QuadArray(4,
QuadArray.COORDINATES);
float coordsSquareEnvironment [] = {
-0.2f, -0.4f, 0.4f, //de eerste
coordinaat van elk punt bepaalt waar
-0.2f, 0.4f, 0.4f, //de "plaat"
zich bevindt op de environment-as
-0.2f, 0.4f, -0.4f,
-0.2f, -0.4f, -0.4f,
};
qaEnvironment.setCoordinates(0, coordsSquareEnvironment);
Appearance appEnvironment = new Appearance();
ColoringAttributes caEnvironment = new ColoringAttributes(); //
opmaak environment vierkant
caEnvironment.setColor(1.0f, 0.0f, 0.0f); // opmaak environment
vierkant
appEnvironment.setColoringAttributes(caEnvironment);
Shape3D squareEnvironment = new Shape3D(qaEnvironment,
appEnvironment);
Transform3D rotate = new Transform3D();
Transform3D tempRotate = new Transform3D();
rotate.rotX(Math.PI / 7.0d);
tempRotate.rotY(Math.PI / 4.0d);
rotate.mul(tempRotate);
TransformGroup objRotate = new TransformGroup(rotate);
objRotate.addChild(squareEnvironment);
objRoot.addChild(objRotate);
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
What am i doing wrong?
Ced - 31 Mar 2006 14:40 GMT
> Sometimes i get an error telling me that only a branchgroup can
> be added, and when i try placing the square in a branchgroup,
> nothing happens.
It's a common problem. You can't add something to a branchgroup when it
is live (attached in you scene graph). So you have to detach it
(detach() method), add your object et add the BranchGroup again. I
don't know if It's clear because it's not easy to explain in English.
Don't forget the
objRoot.setCapability(BranchGroup.ALLOW_DETACH);
(objRoot is the BranchGroup you have to detach)
and probably a
mouseTransform.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
for his parent.
matthiasvangorp - 31 Mar 2006 15:46 GMT
Thanks, i'll give it a try.