Andrew Kimsey wrote:
>
> Thanks Eric, but ViewPlatform subclasses Leaf, which can't be detached. I tried
> detaching my ViewingPlatform, but although SimpleUniverse has a
> getViewingPlatform function, it has none to attach one. It does have an
> addBranchGroup function, but if I use that to attach a ViewingPlatform will it
> recognize it as a ViewingPlatform?
>
> Thanks,
> Andy
>

Hi Andy,
attached is a small "dirty" program, which opens a frame and initializes
2 views without using SimpleUniverse. Hope it's useful.

Joerg
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
//import javax.swing.JTree;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import java.io.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
//show the scengraph in an x-tra window
//import SceneGraphViewer.ViewerLocale;

public class UniverseSwing extends  JFrame implements ActionListener{

 BranchGroup contentroot;
 BranchGroup sgroot;
 BranchGroup viewgraphroot;

 public UniverseSwing(String name, int x, int y, int w, int h){
        super(name);
        setBounds(x,y,w,h);

 Container contentPane = getContentPane();

 addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent w){
                System.exit(0);
        }});

        Canvas3D canvas_1 = new 
Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
        Canvas3D canvas_2 = new 
Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
        JButton button1 = new JButton("B1");
        JButton button2 = new JButton("B2");
  button1.addActionListener(this);
  button2.addActionListener(this);

  JPanel  buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout());
  buttonPanel.add(button1);
  buttonPanel.add(button2);

  //JTree  sgtree = new JTree();

  contentPane.setLayout(new GridLayout(0,2));
  contentPane.add(canvas_1);
  contentPane.add(canvas_2);
  contentPane.add(buttonPanel);
  //contentPane.add(sgtree);
  show();
        createSceneGraph(canvas_1,canvas_2);
 }

 public void createSceneGraph(Canvas3D c1, Canvas3D c2){
        //Create a new Universe
        VirtualUniverse universe = new VirtualUniverse();

        //Create a new Locale & add it to the Universe
        Locale locale = new Locale(universe);

         //Debuging
         //ViewerLocale locale = new ViewerLocale(universe);

        //Create the root of the scenegraph & add it to locale
        // sgroot will be used to attach content_branch_graph and view_branch_graph
        sgroot = new BranchGroup();
        sgroot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        sgroot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  sgroot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);

  // Create the root of the content graph.
  // Used to attach the geometricmodel of the scene
  // Create the root of the viewgraph.
  // Used to attach the different views
  // Add the Stuff to the SceneGraphs Root
  contentroot = new BranchGroup();
  contentroot.setCapability(BranchGroup.ALLOW_DETACH);
  viewgraphroot = new BranchGroup();
  sgroot.addChild(contentroot);
  sgroot.addChild(viewgraphroot);
  createContent(contentroot);
  createViews(viewgraphroot, c1, c2);
  createLights(contentroot);
  //Make the scenegraph "live"
  locale.addBranchGraph(sgroot);
 }

 public void createMouseBehavior(TransformGroup tgr){
        tgr.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        tgr.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
        tgr.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

  BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

        // Create the rotate behavior node fuer ObjTrans
        MouseRotate behavior = new MouseRotate(tgr);
        tgr.addChild(behavior);
        behavior.setSchedulingBounds(bounds);

        // Create the zoom behavior node fuer ObjTrans
        MouseZoom behavior2 = new MouseZoom(tgr);
        tgr.addChild(behavior2);
        behavior2.setSchedulingBounds(bounds);
 }

 public void createViews(BranchGroup viewgraphroot, Canvas3D c1, Canvas3D c2){
        //Create the Views and attach their canvases
  View v1 = new View();
  v1.addCanvas3D(c1);
  View v2 = new View();
  v2.addCanvas3D(c2);

  //Create the viewplatforms,physical bodies & phys. Environments
  //and attach the stuff to the views
  ViewPlatform vp1 = new ViewPlatform();
  v1.attachViewPlatform(vp1);
  v1.setPhysicalBody(new PhysicalBody());
  v1.setPhysicalEnvironment(new PhysicalEnvironment());

  ViewPlatform vp2 = new ViewPlatform();
  v2.attachViewPlatform(vp2);
  v2.setPhysicalBody(new PhysicalBody());
  v2.setPhysicalEnvironment(new PhysicalEnvironment());

  //Create TransformGroups & Transformations for the diffrent views
  //Attach the views to their belonging TG's
        Transform3D t = new Transform3D();
        Transform3D r = new Transform3D();
        TransformGroup vp1tgr = new TransformGroup();
        vp1tgr.addChild(vp1);
        TransformGroup vp2tgr = new TransformGroup();
        vp2tgr.addChild(vp2);

        // Set view transformations
        t.set(new Vector3f(0f, 0f, 10f));
        vp1tgr.setTransform(t);
        t.set(new Vector3f(0f, 13f, 10f));
        r.rotX(-Math.PI/4d);
        t.mul(t, r);
        vp2tgr.setTransform(t);


  //Create keyboard behavior fuer viewplatform 1

   createVpNavBeh(vp1tgr, contentroot);


        //Finally attach the viewtgr's to the view graph root
        viewgraphroot.addChild(vp1tgr);
        viewgraphroot.addChild(vp2tgr);


 }

 public void createVpNavBeh(TransformGroup navtgr, BranchGroup bg){
  navtgr.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        navtgr.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
  KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(navtgr);
  keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(),1000.0));
  bg.addChild(keyNavBeh);
 }


  private void createLights(BranchGroup graphRoot) {

        // Create a bounds for the light source influence
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);

        // Set up the global, ambient light
        Color3f alColor = new Color3f(0.1f, 0.1f, 0.1f);
        AmbientLight aLgt = new AmbientLight(alColor);
        aLgt.setInfluencingBounds(bounds);
        graphRoot.addChild(aLgt);

        // Set up the directional (infinite) light source
        Color3f lColor1 = new Color3f(0.5f, 0.5f, 0.5f);
        Vector3f lDir1  = new Vector3f(10f, -10f, -10f);
        DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
        lgt1.setInfluencingBounds(bounds);
        graphRoot.addChild(lgt1);
    }

  public void createContent(BranchGroup contentroot){


        TransformGroup contenttgr = new TransformGroup();

        //Coordinate Axis
        Appearance xapp = new Appearance();
        Appearance yapp = new Appearance();
        Appearance zapp = new Appearance();
        ColoringAttributes xc = new ColoringAttributes();
        ColoringAttributes yc = new ColoringAttributes();
        ColoringAttributes zc = new ColoringAttributes();
        xc.setColor(1f,0f,0f);
        xapp.setColoringAttributes(xc);
        Box x_axe = new Box(15.0f, 0.1f, 0.1f, xapp);
        yc.setColor(0f,1f,0f);
        yapp.setColoringAttributes(yc);
        Box y_axe = new Box(0.1f, 15f, 0.1f, yapp);
        zc.setColor(0f,0f,1f);
        zapp.setColoringAttributes(zc);
        Box z_axe = new Box(0.1f, 0.1f, 15f, zapp);
        contenttgr.addChild(x_axe);
        contenttgr.addChild(y_axe);
        contenttgr.addChild(z_axe);
  contenttgr.addChild(new Box());
  contentroot.addChild(contenttgr);
  createMouseBehavior(contenttgr);


 }

 public void actionPerformed(ActionEvent e){
 String arg = e.getActionCommand();

 if ("B1".equals(arg)){
  contentroot.detach();
  }
  else if ("B2".equals(arg)){
  sgroot.addChild(contentroot);
  }
 }

 public static void main(String[] args){
         new UniverseSwing("SwingUniverse",0 , 0, 500, 500);
 }
}

Reply via email to