3D transform view group - Java Media

Java examples for Media:3D

Description

3D transform view group

Demo Code



import java.awt.Frame;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Main extends Applet 
    SimpleUniverse simpleU; //from  w w  w .  j ava  2s .co  m

    public void init() {
        setLayout(new BorderLayout()); // standard Java code for BorderLayout
        Canvas3D c = new Canvas3D(
                SimpleUniverse.getPreferredConfiguration());
        add("Center", c);
        simpleU = new SimpleUniverse(c); // setup the SimpleUniverse, attach the Canvas3D

        BranchGroup scene = createSceneGraph();
        scene.compile();

        simpleU.addBranchGraph(scene); //add your SceneGraph to the SimpleUniverse

    }

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

        TransformGroup cctg = new TransformGroup(); 

        ColorCube c = new ColorCube(0.5f);
        cctg.addChild(c); // add ColorCube to cctg

        Transform3D cc3d = new Transform3D();
        cc3d.setTranslation(new Vector3f(0.8f, 1.0f, -2.0f));

        cctg.setTransform(cc3d); 

        objRoot.addChild(cctg); // add cctg to Root

        cc3d.setTranslation(new Vector3f(0, 0, 0));
        cctg.setTransform(cc3d);

        ViewingPlatform vp = simpleU.getViewingPlatform(); // get the ViewingPlatform of the SimpleUniverse

        TransformGroup View_TransformGroup = vp.getMultiTransformGroup()
                .getTransformGroup(0); // get the TransformGroup associated

        Transform3D View_Transform3D = new Transform3D(); // create a Transform3D for the ViewingPlatform
        View_TransformGroup.getTransform(View_Transform3D); // get the current 3d from the ViewingPlatform

        View_Transform3D.setTranslation(new Vector3f(0.0f, -1.0f, 5.0f)); // set 3d to  x=0, y=-1, z= 5
        View_TransformGroup.setTransform(View_Transform3D); // assign Transform3D to ViewPlatform

        return objRoot;
    }

    public void destroy() {

        simpleU.removeAllLocales();
    }

    public static void main(String[] args) {
        Frame frame = new MainFrame(new Main(), 500, 500);
    }

}

Related Tutorials