Skip to main content

Add new object after certain time

3 replies [Last post]
mefisto_o
Offline
Joined: 2007-08-01
Points: 0

I am new user of java3d and i have a problem. I would like to add a new shape to the scene after some time.

All objects are visble immediatly. I would like to add each sphere after 5 seconds. Could you help me please. How to do this. My code looks like this:

for (int k = -3; k < 3; k++) {
objTrans.addChild(createSphere(k, 1, 1, 0.5));
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
knirps
Offline
Joined: 2007-02-02
Points: 0

You could probably use a timed behaviour to control sphere creation.
Or just use a timer + timertask.

...
SphereGen gen = new SphereGen();
gen.setSchedulingBounds(yourApplicationBounds);
yourSceneBranchGroup.addChild(gen);
...
public class SphereGen extends Behavior {

private WakeupCondition timeDelay;
private static final int WAKEUP_TIME = 5000;

public SphereGen(){
timeDelay = new WakeupOnElapsedTime(WAKEUP_TIME);
}

@Override
public void initialize(){
wakeupOn(timeDelay);
}

@Override
public final void processStimulus(Enumeration criteria){
if(lastSphere){
createSphere();
//dont reset wakupCondition because it's the last sphere you want to add
}else{
createSphere();
wakeupOn(timeDelay); //reset wakeUp..
}
...
}

Message was edited by: knirps

mefisto_o
Offline
Joined: 2007-08-01
Points: 0

Thank You very much, it was very helpful but I don't know why added objects are not visible :( Could You help me with this. Below is my code

public class Ramka_Test_3D_1 extends JFrame {

// ** add sphere method
protected Group createSphere(float x, float y, float z, float radius) {
TransformGroup tg = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(x, y, z));
tg.setTransform(t3d);

// create an Appearance and Material
Appearance app = new Wyglad_Obiektu(1).zwroc_Wyglad_Obiektu();

tg.addChild(new Sphere(radius, Primitive.GENERATE_NORMALS, app));

return tg;
}

// **method creating a scenegraph branchgroup

public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();

// Swiatlo
DirectionalLight swiatlo_kier = new DirectionalLight();
swiatlo_kier.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));
// swiatlo_kier.setColor(new Color3f(1.0f, 1.0f, 1.0f));
swiatlo_kier.setDirection(1.0f, -1.25f, -1.0f);
objRoot.addChild(swiatlo_kier);

objRoot.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
objRoot.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
objRoot.setCapability(TransformGroup.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
objRoot.setCapability(TransformGroup.ALLOW_AUTO_COMPUTE_BOUNDS_WRITE);
objRoot.setCapability(TransformGroup.ALLOW_BOUNDS_READ);
objRoot.setCapability(TransformGroup.ALLOW_BOUNDS_WRITE);
objRoot.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
objRoot.setCapability(TransformGroup.ALLOW_CHILDREN_READ);

// create a large sphere in the center of the
// scene and the floor as staionary objects
objRoot.addChild(createSphere(0, 0, 0, 2));
objRoot.addChild(createFloor());

// create a Spahare generating object

SphereGen gen = new SphereGen();
gen.setSchedulingBounds(new BoundingSphere(new Point3d(), 100.0));
objRoot.addChild(gen);

return objRoot;

}

// ****Creating new objects */
class SphereGen extends Behavior {
private WakeupCondition timeDelay;
private static final int WAKEUP_TIME = 5000;
private int number = 0;

public SphereGen() {
timeDelay = new WakeupOnElapsedTime(WAKEUP_TIME);

}

public void initialize() {
wakeupOn(timeDelay);
}

public void processStimulus(Enumeration arg0) {
if (!(number < 10)) {createSphere(-number, -number, number, 1);
number++

System.out.println("Last Object");
} else {
createSphere(-number, -number, number, 1);
number++;
createSphere(6, 7, -1, 1);
System.out.println("Now should add object");
wakeupOn(timeDelay); reset wakeUp..
}

}
}

public Ramka_Test_3D_1(int ident_obiektu, int nr_testu, int param_1, int param_2,
int param_3) {
super("TEST_3D");

setSize(640, 480);
setLayout(new BorderLayout());
GraphicsConfiguration
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D with GraphicsConfiguration
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();

create the first Viewer, this is a static top-down view
create a ViewingPlatform with 2 TransformGroups above the
ViewPlatform
ViewingPlatform vp = new ViewingPlatform(2);

create the Viewer and attach to the first canvas
Viewer viewer = new Viewer(canvas3D);

rotate and position the first Viewer above the environment
Transform3D t3d = new Transform3D();
t3d.rotX(Math.PI 5.5);
t3d.setTranslation(new Vector3d(0, 0, -40));
t3d.invert();

MultiTransformGroup mtg = vp.getMultiTransformGroup();
mtg.getTransformGroup(0).setTransform(t3d);

create a SimpleUniverse from the ViewingPlatform and Viewer
SimpleUniverse u = new SimpleUniverse(vp, viewer);

add the geometry to the scenegraph
u.addBranchGraph(scene);

u.getViewingPlatform().setNominalViewingTransform();

setVisible(true);

}

}

Message was edited by: mefisto_o

knirps
Offline
Joined: 2007-02-02
Points: 0

You need to attach the new created transformgroups of the spheres to the Scenegraph to make them visible i think.
like getParent().addChild(createSphere...))