home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / Tester.java < prev    next >
Text File  |  1997-02-21  |  4KB  |  135 lines

  1. import java.applet.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.io.*;
  5. import netscape.javascript.JSObject;
  6. import vrml.external.Browser;
  7. import vrml.external.Node;
  8. import vrml.external.field.*;
  9. import vrml.external.exception.*;
  10.  
  11. public class Tester extends Applet implements Runnable, EventOutObserver {
  12.  
  13.     Thread myThread = null;
  14.  
  15.     boolean running = true;
  16.  
  17.     Browser browser;          // the VRML browser
  18.     Node root;                // root node of the loaded world
  19.     EventInMFNode addChildren = null;
  20.     EventInMFNode removeChildren = null;
  21.     EventOutSFVec3f position_changed;
  22.     EventOutSFRotation orientation_changed;
  23.     Node obj1;                // debug only!
  24.     Node sensor1;
  25.  
  26.     public void init() {
  27.     }
  28.  
  29.     public void start() {
  30.         if (myThread == null)
  31.             myThread = new Thread(this);
  32.         myThread.start();
  33.     }
  34.  
  35.     public void stop() {
  36.        if (myThread != null)
  37.            myThread.stop();
  38.     }
  39.  
  40.     public void run() {
  41.         browserSetup();  // connect to the VRML browser
  42.         while (running) {
  43.         }
  44.     }
  45.  
  46.     private Browser getBrowser() {
  47.         Browser browser = Browser.getBrowser();
  48.         if (browser == null) {
  49.             JSObject win = JSObject.getWindow(this);
  50.             if (win != null) {
  51.                 JSObject doc = (JSObject) win.getMember("document");
  52.                 JSObject embeds = (JSObject) doc.getMember("embeds");
  53.                 JSObject applets = (JSObject) doc.getMember("applets");
  54.                 JSObject frames = (JSObject) doc.getMember("frames");
  55.                 for (int i = 0; i < 10; ++i) {
  56.                     browser = (Browser) embeds.getSlot(0);
  57.                     if (browser != null)
  58.                         break;
  59.                     try { Thread.sleep(500); }
  60.                     catch (InterruptedException e) { }
  61.                 }
  62.             }
  63.         }
  64.         return browser;
  65.     }
  66.  
  67.     private void browserSetup() {
  68.  
  69.         browser = getBrowser();
  70.         if (browser == null)
  71.             System.out.println("Could not find VRML browser!");
  72.  
  73.         // find the root node of the loaded world
  74.  
  75.         root = null;
  76.         try {
  77.             while (root == null)
  78.                 root = browser.getNode("Root");
  79.         }
  80.         catch (InvalidNodeException e) {
  81.             System.out.println("Could not find node named \"Root\"");
  82.         }
  83.  
  84.         // find that node's addChildren and removeChildren eventIns
  85.  
  86.         try {
  87.             addChildren = (EventInMFNode) root.getEventIn("addChildren");
  88.             removeChildren = (EventInMFNode) root.getEventIn("removeChildren");
  89.         }
  90.         catch (InvalidEventInException e) {
  91.             System.out.println("Could not access eventIn on root node: " + e);
  92.         }
  93.  
  94.         if (addChildren != null) {
  95.             Node[] nodes = browser.createVrmlFromString(
  96.  
  97. // Now, here's the weird part.  If I uncomment the Transform, the
  98. // ProximitySensor only sends one event and then nothing more, whereas
  99. // if I uncomment the Shape, everything works fine!
  100.  
  101. //              "Transform { children Inline { url \"sph.wrl\" } }");
  102.               "Shape { geometry Sphere { } }");
  103.  
  104.             obj1 = nodes[0];
  105.             addChildren.setValue(nodes);
  106.             nodes = browser.createVrmlFromString(
  107.                     "ProximitySensor { size 1e30 1e30 1e30 }");
  108.             sensor1 = nodes[0];
  109.             addChildren.setValue(nodes);
  110.             position_changed =
  111.                 (EventOutSFVec3f) sensor1.getEventOut("position_changed");
  112.             position_changed.advise(this, null);
  113.             orientation_changed =
  114.                 (EventOutSFRotation) sensor1.getEventOut("orientation_changed");
  115.             orientation_changed.advise(this, null);
  116.         }
  117.  
  118.     }
  119.  
  120.     private int n = 0;  // used to count callbacks
  121.  
  122.     public void callback(EventOut event, double time, Object userData) {
  123.         if (event.getType() == FieldTypes.SFVEC3F) {
  124.             EventOutSFVec3f pos = (EventOutSFVec3f) event;
  125.             float[] values = pos.getValue();
  126. System.out.println("location = " + values[0] + " " + values[1] + " " + values[2]);
  127.         }
  128.         else if (event.getType() == FieldTypes.SFROTATION) {
  129.             EventOutSFRotation rot = (EventOutSFRotation) event;
  130.             float[] values = rot.getValue();
  131.         }
  132.     }
  133.  
  134. }
  135.