The KeyNavigator is fairly easy to use and has some good features.
However, it is 'documentation challenged'. I think the documentation,
possibly some enhancements to the code, will be included in the next
version of Java 3D.
Also, look for a complete explanation with examples in Chapter 4 of the
Java 3D Tutorial. Chapter 4 is scheduled for May/June 1999 publication.
In the meantime here is the basic idea.
Keyboard navigation is very easy through the use of the
KeyNavigatorBehavior Class. This class uses the KeyNavigator class
internally. All you have to do is include the KeyNavigatorBehavior
in the scene graph with an appropriate scheduling bounds and provide
the view platform transform to the behavior.
The following lines of code is all that is required.
// vpTrans is the view platform transform
KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans);
keyNavBeh.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(keyNavBeh);
Attached is a simple example. It is not great, but it works.
There are a couple of draw backs to the KeyNavigator (I have filed my
thoughts with SUN).
The biggest 'gotcha' is providing a proper scheduling bounds.
I hope this helps.
-Dennis
PS Chapter 6 (Lights) of the Java 3D API Tutorial should be published
very shortly.
"John F. DeGeorge" wrote:
>
> The javadoc online documentation for the KeyNavigator object does not give
> me enough info to use the object. Can anyone point me toward some example
> code or provide a brief description on how to use the KeyNavigator.
>
> Thanks!
> John
>
--
-----
Dennis J Bouvier
[EMAIL PROTECTED]
Find the Java 3D API Tutorial "Getting Started with the Java 3D API" at
http://sun.com/desktop/java3d/collateral
/*
* @(#)KeuUniverseApp.java 1.0 99/03/25 11:42:40
*
* Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
// import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.awt.AWTEvent;
import java.util.Enumeration;
import com.sun.j3d.utils.behaviors.keyboard.*;
// KeyUniverseApp renders a single, rotated cube.
public class KeyUniverseApp extends Applet {
public BranchGroup createSceneGraph(TransformGroup vpTrans) {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
objRoot.addChild(new ColorCube(0.4));
KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans);
keyNavBeh.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(keyNavBeh);
// Let Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
} // end of CreateSceneGraph method of KeyUniverseApp
public class UniverseBuilder extends Object {
public Locale locale;
public TransformGroup vpTrans;
public UniverseBuilder(Canvas3D canvas) {
// this.canvas = canvas;
VirtualUniverse universe = new VirtualUniverse();
locale = new Locale(universe);
PhysicalBody body = new PhysicalBody();
PhysicalEnvironment environment = new PhysicalEnvironment();
View view = new View();
view.addCanvas3D(canvas);
view.setPhysicalBody(body);
view.setPhysicalEnvironment(environment);
BranchGroup vpRoot = new BranchGroup();
Transform3D viewT3D = new Transform3D();
viewT3D.set(new Vector3f(0.0f, 0.0f, 2.0f));
ViewPlatform vp = new ViewPlatform();
vpTrans = new TransformGroup(viewT3D);
vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
vpTrans.addChild(vp);
vpRoot.addChild(vpTrans);
view.attachViewPlatform(vp);
locale.addBranchGraph(vpRoot);
}
} // end of UniverseBuilder class
KeyUniverseApp() {
setLayout(new BorderLayout());
Canvas3D canvas = new Canvas3D(null);
add("Center", canvas);
UniverseBuilder u = new UniverseBuilder(canvas);
BranchGroup scene = createSceneGraph(u.vpTrans);
u.locale.addBranchGraph(scene);
} // end of KeyUniverseApp (constructor)
// The following allows this to be run as an application
// as well as an applet
public static void main(String[] args) {
Frame frame = new MainFrame(new KeyUniverseApp(), 256, 256);
} // end of main (method of KeyUniverseApp)
} // end of class KeyUniverseApp