Changes required in SimJava code
Imports:-
import vrml.external.field.*;
import vrml.external.Node;
import vrml.external.Browser;
import vrml.external.exception.*;
Basic connection to the VRML world (The pauses are a cludge and
are not always necessary):-
Browser browser=null;
boolean error=true;
/** Connect to browser */
public void initBrowser() {
pause(1000);
browser = Browser.getBrowser(this);
if (browser == null) {
error = true;
} else {
error = false;
}
}
/** Pause thread for t millisecs */
void pause(int t) {
try {Thread.currentThread().sleep(t);}
catch (InterruptedException e) {
System.out.println("Rudely interrupted");
}
}
initBrowser() should be called from anim_layout() so that
the connection is reestablished each time the simulation is
initialised.
The basic function to get a DEFed node by name is:-
public Node getNode(String name) {
Node n = null;
if (!error) {
try {n = browser.getNode(name); }
catch (InvalidNodeException ne) {
System.out.println("Failed to get node:" + ne);
error = true; }
}
return n;
}
And to get an eventIn from a node:-
public EventIn getEventIn(Node n, String ename) {
EventIn e = null;
if (!error) {
if (n!=null) {
try {
e = n.getEventIn(ename);
} catch (Exception ex) {
System.out.println("Couldn't get event "+ename+":"+ex);
}
}}
return e;
}
Example usage (VRML fragment):-
EXTERNPROTO CPU [
exposedField SFInt32 state
exposedField SFVec3f translation] "cpu.wrl"
DEF Node0 CPU { state 0 translation -3 4 0 }
To get hold of Node0 and set its state to 1 from java using the above
functions:-
Node node0 = getNode("Node0");
EventIn EventInSFInt32 set_state =
(EventInSFInt32) getEventIn(node0,"set_state");
set_state.setValue(1);