Search code examples
javajava-3d

SimpleUniverse i dont understand here the addChild line code please explain me


hello guys i wonder why here in the code there is a line where i the procedure addChild(trans) is not attached to any place in the SimpleUniverse tree if someone cant explain me why that works and how it would be very helpful to me . thanks the (line is inseide the second for)

    package examples;

import javax.media.j3d.Appearance;
import javax.media.j3d.Group;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;

public class SphereGroupExperiment extends Group{
    Shape3D[] shapes;
    int shapesNum=0;

    public SphereGroupExperiment( )
    {
        //    radius   x,y spacing   x,y count  appearance
        this(0.25f,   0.75f, 0.75f,   5, 5,      null, false );
    }

    public SphereGroupExperiment( Appearance app )
    {
        //    radius   x,y spacing   x,y count  appearance
        this( 0.25f,   0.75f, 0.75f,   5, 5, app, false );
    }

    public SphereGroupExperiment( float radius, float xSpacing, float ySpacing,
        int xCount, int yCount, boolean overrideflag )
    {
        this( radius,  xSpacing, ySpacing, xCount, yCount, null, overrideflag );
    }

    SphereGroupExperiment(float radius, float xSpace, float ySpace, int xCount, int yCount,Appearance app, boolean overrideFlag){
        if(app == null){
            Material material = new Material();
            material.setDiffuseColor(new Color3f(0.0f, 1.0f, 0.0f));
            material.setSpecularColor(new Color3f(0.0f, 0.0f, 0.0f));
            material.setShininess(0.0f);
            app.setMaterial(material);
        }
        double xStart = -xSpace * (double)(xCount-1)/2.0;
        double yStart = -ySpace * (double)(yCount-1)/2.0;

        Sphere sphare= null;
        TransformGroup trans = null; 
        Transform3D t = new Transform3D();
        Vector3d vec = new Vector3d();
        double x, y = yStart, z = 0.0;
        shapes = new Shape3D[xCount*yCount];
        for (int i = 0 ; i<yCount ; i++){
            x = xStart;
            for(int j = 0; j<xCount; j++){
                vec.set(x,y,z);
                t.setTranslation(vec);
                trans = new TransformGroup(t);
                addChild(trans);

                sphare = new Sphere(
                        radius,     // sphere radius
                        Primitive.GENERATE_NORMALS,  // generate normals
                        25,         // 16 divisions radially
                        app );      // it's appearance
                trans.addChild( sphare );

                x += xSpace;
                shapes[shapesNum] = sphare.getShape();
                if (overrideFlag) 
                shapes[shapesNum].setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_WRITE);
                shapesNum++;

            }
            y += ySpace;
        }

    }
    public Shape3D[] getSphares(){
        return shapes;
    }

}

Solution

  • Your class SphereGroupExperiment extends javax.media.j3d.Group. You will find addChild() in that class.