home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch18
/
Tester.java
< prev
next >
Wrap
Text File
|
1997-02-21
|
4KB
|
135 lines
import java.applet.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import netscape.javascript.JSObject;
import vrml.external.Browser;
import vrml.external.Node;
import vrml.external.field.*;
import vrml.external.exception.*;
public class Tester extends Applet implements Runnable, EventOutObserver {
Thread myThread = null;
boolean running = true;
Browser browser; // the VRML browser
Node root; // root node of the loaded world
EventInMFNode addChildren = null;
EventInMFNode removeChildren = null;
EventOutSFVec3f position_changed;
EventOutSFRotation orientation_changed;
Node obj1; // debug only!
Node sensor1;
public void init() {
}
public void start() {
if (myThread == null)
myThread = new Thread(this);
myThread.start();
}
public void stop() {
if (myThread != null)
myThread.stop();
}
public void run() {
browserSetup(); // connect to the VRML browser
while (running) {
}
}
private Browser getBrowser() {
Browser browser = Browser.getBrowser();
if (browser == null) {
JSObject win = JSObject.getWindow(this);
if (win != null) {
JSObject doc = (JSObject) win.getMember("document");
JSObject embeds = (JSObject) doc.getMember("embeds");
JSObject applets = (JSObject) doc.getMember("applets");
JSObject frames = (JSObject) doc.getMember("frames");
for (int i = 0; i < 10; ++i) {
browser = (Browser) embeds.getSlot(0);
if (browser != null)
break;
try { Thread.sleep(500); }
catch (InterruptedException e) { }
}
}
}
return browser;
}
private void browserSetup() {
browser = getBrowser();
if (browser == null)
System.out.println("Could not find VRML browser!");
// find the root node of the loaded world
root = null;
try {
while (root == null)
root = browser.getNode("Root");
}
catch (InvalidNodeException e) {
System.out.println("Could not find node named \"Root\"");
}
// find that node's addChildren and removeChildren eventIns
try {
addChildren = (EventInMFNode) root.getEventIn("addChildren");
removeChildren = (EventInMFNode) root.getEventIn("removeChildren");
}
catch (InvalidEventInException e) {
System.out.println("Could not access eventIn on root node: " + e);
}
if (addChildren != null) {
Node[] nodes = browser.createVrmlFromString(
// Now, here's the weird part. If I uncomment the Transform, the
// ProximitySensor only sends one event and then nothing more, whereas
// if I uncomment the Shape, everything works fine!
// "Transform { children Inline { url \"sph.wrl\" } }");
"Shape { geometry Sphere { } }");
obj1 = nodes[0];
addChildren.setValue(nodes);
nodes = browser.createVrmlFromString(
"ProximitySensor { size 1e30 1e30 1e30 }");
sensor1 = nodes[0];
addChildren.setValue(nodes);
position_changed =
(EventOutSFVec3f) sensor1.getEventOut("position_changed");
position_changed.advise(this, null);
orientation_changed =
(EventOutSFRotation) sensor1.getEventOut("orientation_changed");
orientation_changed.advise(this, null);
}
}
private int n = 0; // used to count callbacks
public void callback(EventOut event, double time, Object userData) {
if (event.getType() == FieldTypes.SFVEC3F) {
EventOutSFVec3f pos = (EventOutSFVec3f) event;
float[] values = pos.getValue();
System.out.println("location = " + values[0] + " " + values[1] + " " + values[2]);
}
else if (event.getType() == FieldTypes.SFROTATION) {
EventOutSFRotation rot = (EventOutSFRotation) event;
float[] values = rot.getValue();
}
}
}