Xj3D Browser Applet Example

Changing Viewpoints Externally

HTML Modifications

Modifying the viewpoint that is currently bound externally, or from an HTML based construct requires interaction with the applet via ECMA Script, aka JavaScript. There is an additional hoop to jump through because of the use of the Java JNLP Applet Launcher, which requires the script to access the subapplet, and not the actual applet that is contained on the page. However, there is a convienient method within the JNLP applet's code that allows for this.

The JavaScript

A very simple script is required to change the bound viewpoint in the applet, just simply call the method in the applet's source that does the task, and pass it the DEF name of the viewpoint to be bound. The DEF names are hard-coded into the HTML <select> tag for the sake of simplicity. The following script is used on this page:

function viewpointChanged() {
	var x = document.getElementById("viewpointselect");
	document.applets[0].getSubApplet().changeViewpoint(x.options[x.selectedIndex].value);
}

The getSubApplet() method is within the JNLP Applet, and returns the instance of the subapplet within the launcher. This allows calls to be made to the nested applet from the HTML. Defining x is only for convienience.

The Input Construct

This example uses a <select> tag to allow picking different viewpoints. Within the tag, each <option> element has the DEF name of a viewpoint in its value parameter, and the corresponding description field as the selectable text. These values are hard-coded to ensure compataiblity with the X3D file that is being used.

<select id="viewpointselect" onchange="viewpointChanged()">
<option value="V1">Viewpoint1</option>
<option value="V2">Viewpoint2</option>
</select>

Java Code Modifications

Only one extra method needs to be added to the applet code. This is the method that is to be called by the script to actually do the change within the browser. This method takes in a String parameter representing the DEF'd name of the viewpoint that is to be the new bound viewpoint.

public void changeViewpoint(String defName) {
	X3DScene scene = (X3DScene)browser.getExecutionContext();
	X3DNode newVp = scene/getNamedNode(defName);
	SFBool vboundField = (SFBool)newVp.getField("set_bind");
	boundField.setValue(Boolean.TRUE);
}

X3D File notes

This example will work without the viewpoints having a description field, however they must have a DEF name. The DEF names must match between the X3D file that is being loaded and the values passed from the HTML page.

Source Files


[ Xj3D Homepage | Xj3D @ Web3d | Screenshots | Dev docs | Dev Releases | Contributors | Getting Started ]
Last updated: $Date: 2008-08-29 00:03:26 $