Skip to main content

Exception in thread "AWT-EventQueue-1" javax.media.j3d.MultipleParentException: Group.addChild: child already has a parent

No replies
mitrajit
Offline
Joined: 2011-12-12
Points: 0

Hi guys, I'm new in java 3d.

I wrote a simple program where, user can draw some 3d shapes(cylinder,sphere,cube...) just by click on buttons. All shapes are by default draw on the canvas at center.
Here is the program code---

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.LineArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.Sphere;
import java.awt.event.ItemListener;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.swing.event.ChangeListener;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;



public class FHD3DOutput extends JPanel implements MouseMotionListener, ActionListener, ChangeListener,ItemListener{
   
    //Attributes
    private Canvas3D canvas3D;
    private GraphicsConfiguration config;
    private SimpleUniverse universe;
    FHD3DInput obj;
    BranchGroup scene  = new BranchGroup();
    BranchGroup scene0 = new BranchGroup();
    BranchGroup sceneTmp = new BranchGroup();
   
    boolean drawCircle = false;
    boolean drawCube = false;
    boolean drawLine = false;
    boolean drawPoint = false, drawPointByValues = false;
    boolean drawCylinder = false;
    boolean drawAxis = false;
    ArrayList<TransformGroup> arrayTG = new ArrayList<TransformGroup>();
    float coX1 = -1.0f, coY1 = 0.0f, coZ1 = 0.0f;
    float coX2 = 1.0f, coY2 = 0.0f, coZ2 = 0.0f;
    Transform3D tf = new Transform3D();
    TransformGroup tgArray[] = new TransformGroup[100];
 
   
   

   
    public FHD3DOutput()
    {
        obj = new FHD3DInput(this);
        //add(obj, BorderLayout.WEST);

       
        setLayout(new BorderLayout());
        config = SimpleUniverse.getPreferredConfiguration();
        canvas3D = new Canvas3D(config);
        add("Center", canvas3D);
       
        scene = createSceneGraph();
       
        scene0.setCapability(BranchGroup.ALLOW_DETACH);
        scene0.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        scene0.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
        scene0.setCapability(BranchGroup.ALLOW_LOCALE_READ);
        scene0.setCapability(BranchGroup.ALLOW_PARENT_READ);
        scene0.addChild(scene);

        universe = new SimpleUniverse(canvas3D);
        universe.getViewingPlatform().setNominalViewingTransform();
      
        universe.addBranchGraph(scene0);
    }
   
   
    @Override
    public void mouseDragged(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        //showStatus(""+e.getX()+"  "+e.getY());
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //JOptionPane.showMessageDialog(canvas3D, "mlllm");
        String command = e.getActionCommand();
        if("JustDrawPoint".equals(command))
        {
            drawPoint = true;
            drawPoint();
        }
       
        if("JustDrawLine".equals(command))
        {
            drawLine = true;
            drawLine();
        }
    }
   


   
   
   
    public void drawLine()
    {
        coX1 = -0.5f; coX2 = 0.5f;
        scene0.removeChild(scene);
        scene = createSceneGraph();
        scene0.addChild(scene);
        universe.getViewingPlatform().setNominalViewingTransform();
    }
   
   
   
   
    public void drawPoint()
    {
        scene0.removeChild(scene);
        scene = createSceneGraph();
        scene0.addChild(scene);
        universe.getViewingPlatform().setNominalViewingTransform();
    }
      
   
   
   
   
    public BranchGroup createSceneGraph()
    {
        BranchGroup objRoot = new BranchGroup();
        BoundingSphere bounds = new BoundingSphere(new Point3d(),10.0);
       
        objRoot.setCapability(BranchGroup.ALLOW_DETACH);
        objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
        objRoot.setCapability(BranchGroup.ALLOW_LOCALE_READ);
        objRoot.setCapability(BranchGroup.ALLOW_PARENT_READ);
       
       
        Transform3D tForm = new Transform3D();
        TransformGroup tGroup = new TransformGroup(tForm);
        objRoot.addChild(tGroup);
        tGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

       

        if(drawPoint)
        {
            //Transform3D t1 = new Transform3D();
            //TransformGroup g1 = new TransformGroup(tf);
           
            tgArray[0] = new TransformGroup(tf);
            tgArray[0].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            tgArray[0].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            tgArray[0].setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
            tgArray[0].setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
            tgArray[0].setCapability(TransformGroup.ALLOW_LOCALE_READ);
            tgArray[0].setCapability(TransformGroup.ALLOW_PARENT_READ);
          
            tgArray[0].setTransform(tf);
            Appearance appear1 = new Appearance();
            Material material1 = new Material();
            material1.setShininess(50.0f);
            appear1.setMaterial(material1);

            tgArray[0].addChild(new Sphere(0.005f, Sphere.GENERATE_NORMALS, appear1));
            AmbientLight lightA1 = new AmbientLight();
            lightA1.setInfluencingBounds(new BoundingSphere());
            tgArray[0].addChild(lightA1);

            DirectionalLight lightD1 = new DirectionalLight();
            lightD1.setInfluencingBounds(new BoundingSphere());
            Vector3f direction1 = new Vector3f(-1.0f, -1.0f, -0.5f);
            direction1.normalize();
            lightD1.setDirection(direction1);
            lightD1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
            tgArray[0].addChild(lightD1);

            DirectionalLight lightD2 = new DirectionalLight();
            lightD2.setInfluencingBounds(new BoundingSphere());
            Vector3f direction2 = new Vector3f(1.0f, -1.0f, -0.5f);
            direction2.normalize();
            lightD2.setDirection(direction2);
            lightD2.setColor(new Color3f(1.0f, 1.0f, 1.0f));
            tgArray[0].addChild(lightD2);
            arrayTG.add(tgArray[0]);
            drawPoint = false;
        }
       
           
       
       
        if(drawLine)
        {
        tgArray[1] = new TransformGroup(tf);
            tgArray[1].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            tgArray[1].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            tgArray[1].setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
            tgArray[1].setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
            tgArray[1].setCapability(TransformGroup.ALLOW_LOCALE_READ);
            tgArray[1].setCapability(TransformGroup.ALLOW_PARENT_READ);
          
           
        /*********** Create X axis ********************/
        LineArray la = new LineArray(10, LineArray.COORDINATES);
        tgArray[1].addChild(new Shape3D(la));
       
        Point3f x111 = new Point3f(coX1, coY1, coZ1);
        Point3f x222 = new Point3f(coX2, coY2, coZ2);
       
        la.setCoordinate(0,x111);
        la.setCoordinate(1,x222);
        //tGroup.addChild(g2);
        arrayTG.add(tgArray[1]);
       
        drawLine = false;
           
        }
       
        // Here I add all the arraylist elements to the tGroup
        for(int i=0;i<arrayTG.size(); i++)
        {
            try
            {
                tGroup.addChild(arrayTG.get(i));
            }catch(Exception e){ System.out.println(e);}
        }  
        return objRoot;
    }  
}

FHD3DInput is the another class where my GUI components are set, such as buttons. When I click on a button to draw a point it set true to variable "drawPoint" and call the createSceneGraph() method, where I check whether "drawpoint" variable is true or not, if true then it execute the following corresponding code block.
Similarly, other buttons as usual. But if I try to draw one after another by clicked on buttons and display all the shapes together in the scene, it occurred following error ---

Exception in thread "AWT-EventQueue-1" 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 FHD3DOutput.createSceneGraph(FHD3DOutput.java:508)
at FHD3DOutput.drawLine(FHD3DOutput.java:187)
at FHD3DOutput.actionPerformed(FHD3DOutput.java:136)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
.........................................................................
.........................................................................

I understand the problem but don't understand how to solve it?
I have attached the java files for better understand. If you run the attached program, then in the input frame, there is a option "JUST DRAW", which is only operational.
please help me.....
Sorry for my bad English .
Thank you very much in advance...

AttachmentSize
FreeHandDraw3D.zip11.67 KB