1

I write this program but it has exception in line

group.addChild(tg);

but when i add

TransformGroup tg = new TransformGroup();

into the for loop block it runs with any problem please tell me it's reason.

Thanks.

this is my code

public BranchGroup Creat()
{
    BranchGroup group = new BranchGroup();
    TransformGroup tg = new TransformGroup();
    for(float x = 0.0f; x < 1.0f; x += 0.1f)
    {
         Transform3D td = new Transform3D();
         Vector3f vector3f = new Vector3f(x, x, x);             
         td.setTranslation(vector3f);
         tg.setTransform(td);
         tg.addChild(new Cone(0.05f, 0.1f));
         group.addChild(tg);             
   }



    return group;
}

this is it's exception

Exception in thread "main" javax.media.j3d.MultipleParentException: Group.addChild: child already has a parent
    at javax.media.j3d.GroupRetained.checkValidChild(GroupRetained.java:478)
    at javax.media.j3d.GroupRetained.addChild(GroupRetained.java:487)
    at javax.media.j3d.Group.addChild(Group.java:290)
    at t39.Draw.Creat(Draw.java:68)
    at t39.Draw.<init>(Draw.java:50)
    at t39.Main.main(Main.java:22)
2
  • 2
    11 questions and no accepted answers... good luck Commented Jan 28, 2011 at 17:31
  • What's the exception? Based on your "fix", I would assume (without knowing anything about the library) that each TransformGroup can only be added to a parent BranchGroup once. As it stands, it looks like having group.addChild(tg) inside the for loop is pointless since it's only interacting with objects outside the loop. Commented Jan 28, 2011 at 17:32

1 Answer 1

1

The same element can't exist more than once in the scene graph. When you create a new TransformGroup within the loop it doesn't break the rule, but if you don't create a new one for every addChild() you break this rule.

(There are exceptions to the "only once in the graph", via weaker references instead of parent/child, e.g. for attributes)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.