Integrating JAVA and VRML

 

2.1 Introduction

VRML and Java solve different tasks in web design. Java allows a programmer to control the flow of a web presentation; VRML allows to create 3D objects in a 3D space and to add simple behaviors to those objects.

Java can be used to add complex behavior to VRML objects. This is useful to organize complex data sets (VRML visualization) and to give the possibility of dynamically altering those data sets in real time (Java). Two achievements are reached:

This interaction can be very useful for scientific visualization.

This diagram shows the link between Java and VRML:

 

 

External Authoring Interface (EAI) is the bridge between Java and VRML. It allows a Java applet to communicate with a VRML world embedded in the same page.

EAI classes are included in the vrml.external.* package:

vrml.external.Browser

Browser class. It is the representation of VRML world in the Java applet

vrml.external.Node

Node class. It's the representation of a VRML node.

vrml.external.field.*

Type classes. They represent all the types of VRML language

vrml.external.exception.*

Exception classes.

 

2.2 EAI Desciption

 Here is the class diagram. Only some of field classes are drawn.

 

Browser class

It's the abstraction of the VRML browser from Java view. A Java applet communicate with VRML by first obtaining a reference of the Browser class. It's possible using the static method getBrowser which returns a reference to Browser. Then calling getNode it's possible to retrieve a reference to a Node class which is the representation of node in the scene graph. Only named nodes are accessible. A node can be named using DEF. Here is the list of most used methods:

static public Browser getBrowser(Applet applet)

Get a reference of VRML browser. It's a static method. A typical Java applet which retrieves the Browser reference is:

public class MyClass extends Applet

{

    Browser browser = null;



    Public void start ()

    {

        ...

        browser = Browser.getBrowser(this);

        ...

    }

} 

public Node getNode(String name)

Get a reference of the named node in the VRML scene.

VRML file:

...

DEF Clicker TouchSensor {}

...

Java code:

...

Node clicker = browser.getNode("Clicker");

...

 

Node class

It's the abstraction of a VRML node. Each node object has a named counterpart in the VRML file. A reference to a node is obtained by getNode method of Browser class. Only eventIns and eventOuts of VRML node can be accessed by Java node. ExposedFields can be accessed as well, by giving the name of the corresponding eventIn or eventOut (set_event or event_changed or simply event ). Here is the list of the methods:

public String getType()

Return the type of node.

public EventIn getEventIn(String name)

It returns an EventIn object when passed the name of the desired eventIn. Then an event can be sent to the VMRL node by invoking setValue() method of EventIn class. A cast must be used to obtain the correct type before using it.

Here is an example. The Java code moves the position of the sphere to (3,4,5).

VRML file:

DEF sphere Transform {

    Translation 1 2 3

    Children [ Shape { geometry Sphere {} } ]

}

Java code:

...

Node sphere = browser.getNode("sphere");



EventInSFVec3f position = (EventInSFVec3f) sphere.getEventIn("translation");



float[] new_position = { 3, 4, 5};



position.setValue(new_position);

...

public EventOut getEventOut(String name)

It returns an EventOut object when passed the name of the desired eventOut. Two operations can be performed on a EventOut object:

  • Get the current value of eventOut of the node
  • Set up a callback function to be called when the node send this eventOut. See EventOutObserver.

A cast to the appropriate type must be performed before using the getValue method.

Here is an example. It gets the position of the previous sphere.

...

EventInSFVec3f position = (EventInSFVec3f) sphere.getEventOut("translation");



float[] new_position = position.getValue();

...

 

 EventIn class

It's an abstract class. It represents the base class for all the eventIn types: EventInSFVec3f, EventInSFColor, ... . Each derived class implements a setValue(...)  method for the corresponding type. The eventIn objects are used to set state information about the VRML node or, in other words, to send an incoming event to the node.

 

EventOut class

It's an abstract class. It represents the base class for all the eventOut types: EventOutSFVec3f, EventOutSFColor, ... An eventOut is an event sent by a VRML node. Two operations are available. One for retrieving state information and the other one to set a callback function to be called each time that an outgoing event is sent by the node. Each derived class implements a getValue() method. EventOut class implements only the advise function. It sets the object that must be informed when the eventOut occurs. A description of callback function is in EventOutObserver class description.

 

EventOutObserver class

It's the base class for catching VRML eventOut. It's an interface. So a class must implement it for receiving events. Then this class must be passed to the advise method of the EventOut object. Every time that an event occurs the callback method is called and the applet can handle the event. An object of type Object is passed to the callback method. It's the same object that it has been passed to the advise method. It can be used to recognize an event.

 

2.3 References

External Authoring Interface

http://vrml.sgi.com/moving-worlds/spec/ExternalInterface.html