BS Contact J : Java-EAI : Browser Observer

Realizing a HUD

This example illustrates "listening for events" in Java. It shows the implementation of a Head Up Display (HUD). A HUD is a screen which follows the user's movements and always occupies the same position the user's field of view. A HUD behaves like a piece of paper that is stuck to one's glasses.

What is needed to implement the HUD? Of course, geometry which should represent the HUD, wrapped into a transform node. The transform node will be provided with the current viewpoint coordinates so that our
HUD also behaves like a HUD by following all user movements.

To get some user movements, we will implement a kind of navigation bar as a HUD. If the user clicks on a symbol of the HUD, the user will be transported and bound to a particular viewpoint. Whenever a TouchSensor
of the HUD is pressed, our extension applet should be informed. The applet then will bind the appropriate viewpoint and update the HUD's coordinates.

(Note: Although this can be realised with a ProximitySensor and some appropriate routes, we will do this with an extension applet for demonstration purposes)

Sorry, your browser doesn't support Java.

view complete source


So how to get notified when a TouchSensor is pressed? To achieve this, the extension applet must register as an observer for the TouchSensor touchTime events of the HUD's symbols by connecting to each TouchSensor's touchTime event first, and then adding the applet (this) as an observer to each event.


// get touch sensor active flags

//register observer


In this example strings are used as identifier ("red", "green", "blue") but one can think of every java object as an identifying object up to the field itself.

After the applet is registered successfully, all touchTime events of the registered TouchSensors will be sent to the BS Contact J event-handling method of the extension applet:

public boolean onEvent(int type, Object object, Object userData)



indicates the type of the event, e.g. URL_LOADED (flagging that a URL has been successfully loaded after a createX3DFromURL method call) or FIELD_CHANGED (flagging that a field has changed in the X3D browser).


can contain output associated with the event that occurred, e.g. URL_LOADED returns loaded and
parsed nodes in its object parameter.


contains the user data specified during event registering

In this case two event types must be observed:

occurs whenever a field (with an observer registered to it) has changed. In this case all HUD's TouchSensor press actions will be received in this method. It must only be determined which TouchSensor is pressed. This is done by evaluating the userData parameter. Depending on the pressed TouchSensor, the appropriate Viewpoint is bound by setting its set_bind event.

is notified when BS Contact J starts a new render cycle, meaning when something in the world has been changed so that a new scene rendering becomes necessary (for example a camera movement). This event is used to start the update of the HUD coordinates.

To update the HUD, the current camera position and orientation are needed. These co-ordinates can be received by the following call:

and reading out its coordinates via getValueFloatArray(). After that the value gets set as new coordinate values of the HUD.