2011-01-28 3 views
1

-je écrire ce programme, mais il a exception dans la lignejeu d'écriture avec java3d

group.addChild(tg); 

mais quand j'ajoute

TransformGroup tg = new TransformGroup(); 

dans le pour le bloc en boucle, il fonctionne avec tout problème s'il vous plaît me dire que ce raisonnons .

Merci.

ceci est mon 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; 
} 

est ce qu'il est 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

11 questions et aucune réponse acceptée ... bonne chance – mkoryak

+0

Quelle est l'exception? Basé sur votre "fix", je supposerais (sans rien savoir sur la bibliothèque) que chaque 'TransformGroup' ne peut être ajouté à un' BranchGroup' parent qu'une seule fois. Dans l'état actuel, il semble que '' group.addChild (tg) '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''. –

Répondre

1

Le même élément ne peut pas exister plus d'une fois dans le graphique de la scène. Lorsque vous créez un nouveau TransformGroup dans la boucle, il ne casse pas la règle, mais si vous n'en créez pas un nouveau pour chaque addChild(), vous enfreignez cette règle.

(Il y a des exceptions à la « une seule fois dans le graphique », via des références plus faibles au lieu de parent/enfant, par exemple pour les attributs)