home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / Thing.java < prev   
Text File  |  1997-02-21  |  3KB  |  77 lines

  1. // A "thing" -- a wrapper around a VRML object
  2.  
  3. // Written by Bernie Roehl, January 1996
  4.  
  5. import vrml.external.Browser;
  6. import vrml.external.Node;
  7. import vrml.external.field.*;
  8. import vrml.external.exception.*;
  9. import multi.*;
  10.  
  11. public class Thing {
  12.     protected Node transformNode;
  13.     protected Node switchNode;
  14.     protected EventInSFVec3f set_translation;
  15.     protected EventInSFRotation set_rotation;
  16.     protected EventInSFInt32 set_switch;
  17.     protected EventInMFNode remove;
  18.  
  19.     public void setLocation(Vec3 loc) {
  20.         float[] value = new float[3];
  21.         value[0] = loc.getX();
  22.         value[1] = loc.getY();
  23.         value[2] = loc.getZ();
  24.         set_translation.setValue(value);
  25.     }
  26.  
  27.     public void setOrientation(Rotation ori) {
  28.         float[] value = new float[4];
  29.         value[0] = ori.getX();
  30.         value[1] = ori.getY();
  31.         value[2] = ori.getZ();
  32.         value[3] = ori.getAngle();
  33.         set_rotation.setValue(value);
  34.     }
  35.  
  36.     public void setVisibility(boolean visible) {
  37.         set_switch.setValue(visible ? 0 : -1);
  38.     }
  39.  
  40.     public Thing(Browser browser, EventInMFNode add,
  41.                  EventInMFNode remove, String url)
  42.         throws InvalidVrmlException, InvalidEventInException {
  43.         Node[] nodes;
  44.         EventInMFNode nodeIn;
  45.         // create Transform node
  46.         nodes = browser.createVrmlFromString("Transform { }");
  47.         transformNode = nodes[0];
  48.         add.setValue(nodes);
  49.         // create Switch node
  50.         nodes = browser.createVrmlFromString("Switch { whichChoice 0 }");
  51.         switchNode = nodes[0];
  52.         // put Switch into Transform
  53.         nodeIn = (EventInMFNode) transformNode.getEventIn("addChildren");
  54.         nodeIn.setValue(nodes);
  55.         // create Inline node
  56. // here's the bug: if I use an Inline, the ProximitySensor that I'm using
  57. // to find the user's location stops working  :-(
  58. //        nodes = browser.createVrmlFromString("Inline { url \"" + url + "\" }");
  59.         nodes = browser.createVrmlFromString("Shape { geometry Cylinder { } }");
  60.         // put Inline into Switch
  61.         nodeIn = (EventInMFNode) switchNode.getEventIn("choice");
  62.         nodeIn.setValue(nodes);
  63.         // get EventIns
  64.         set_translation = (EventInSFVec3f) transformNode.getEventIn("translation");
  65.         set_rotation = (EventInSFRotation) transformNode.getEventIn("rotation");
  66.         set_switch = (EventInSFInt32) switchNode.getEventIn("whichChoice");
  67. System.out.println("Added new entity from url " + url);
  68.     }
  69.  
  70.     public void finalize() {
  71.         Node[] nodes = new Node[1];
  72.         nodes[0] = transformNode;
  73.         remove.setValue(nodes);
  74.     }
  75. }
  76.  
  77.