Xj3D Browser Applet Example

HTML Modifications

The Anchor node is not supported in the Interchange profile, and so a new jar has to be substituted in order for this example to work. This jar is entitled xj3d-interactive-applet-av3d_2.0.0.jar and will bring the level of support up to the Interactive profile. Also this example does not use any scripting in the HTML page. The only addition to the base HTML applet code is the use of an iframe to show that the link is actually working.

<iframe src="http://www.foo.org/blueFrame.html" id="showframe" name="showframe" frameborder="0" scrolling="no" width="100%"></iframe>

Java Code Modifications

Using an Anchor node requires implementing the Xj3DAnchorListener which defines one method, processLinkActivaition(String[] url, String[] param). The target frame is set in the X3D file as the first element of the parameter field, and the target url is set as the first element of the url field. This example makes use of the AppletContext interface to interact with the page containing our Applet. The broswer must be notified that the applet is an Xj3DAnchorListener implementing class. A new boolean, linkClicked, has been added to the class, and is initialized to false in the init() method. This particular method changed the color of the box to reflect what color the frame will be when you click on it and it changes back and forth for each click. It also sets the browser description bar to reflect that it is loading the next page.

public class Xj3DAppletTutorial4 extends Applet implements Xj3DAnchorListener {
				Xj3DBrowser browser;
				boolean linkClicked;

public voidinit() {
	setLayout(new BorderLayout());
	browser = (Xj3DBrowser) getBrowser();
	browser.setAnchorListener(this);
	linkClicked = false;
	loadScene();
}

public boolean processLinkActivation(String[] url, String[] param) {
	AppletContext appletContext = getAppletContext();
	String description = "Loading... " + url[0];
	appletContext.showStatus(description);
	try {
		appletContext.showDocument(new URL(url[0]), param[0]);
	} catch (MalformedURLException mue) {
		System.out.println("Invalid URL: " + url[0] + ".\n" + mue.getStackTrace());
		return false;
	}
	X3DScene scene = (X3DScene) browser.getExecutionContext();
	SFColor color = (SFColor) scene.getNamedNode("material").getField("diffuseColor");
	MFString link = (MFString) scene.getNamedNode("anchor").getField("url");
	if (linkClicked) {
		color.setValue(new float[] {0f, 1f, 0f});
		link.setValue(1, new String[] {"http://www.foo.org/blueFrame.html"});
	} else {
		color.setValue(new float[] {0f, 0f, 1f});
		link.setValue(1, new String[] {"http://www.foo.org/greenFrame.html"});
	}
	linkClicked = !linkClicked;
	return true;
}

X3D Modifications

The Shape containing the box must be wrapped in an Anchor node in order for the box to be clickable, and for the browser to know that it needs to trigger the processLinkActivation method from the Xj3DAnchorListener interface. This is very simple and looks like the following:

<Scene>
  <Anchor DEF='anchor' parameter='showframe' url='http://www.foo.org/greenFrame.html'>
    <Shape>
      <Appearance>
        <Material DEF='material' diffuseColor='0.0 1.0 0.0'></Material>
      </Appearance>
      <Box></Box>
    </Shape>
  </Anchor>
</Scene>

Source Files


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