/* Mykeynavbeh.java * Originally based on code from BackgroundApp.java * @(#)BackgroundApp.java 1.1 00/09/22 14:03 * * portions Copyright (c) 1996-2000 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.*;import java.awt.*;
import javax.media.j3d.*;import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.universe.PlatformGeometry;import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.loaders.Scene;import org.jdesktop.j3d.loaders.vrml97.VrmlLoader;
import java.awt.event.KeyListener;import java.awt.event.KeyEvent;
import java.util.*;import com.sun.j3d.utils.geometry.ColorCube;
public class Mykeynavbeh extends Applet implements KeyListener {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null;
private TransformGroup tg = null; private Transform3D t3d = null; private Transform3D t3dstep = new Transform3D(); private Matrix4d matrix = new Matrix4d();
private double speed = 0.05;
public Mykeynavbeh() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config); add("Center", canvas); universe = new SimpleUniverse(canvas);
BranchGroup scene = createSceneGraph(); universe.getViewingPlatform().setNominalViewingTransform();
universe.getViewer().getView().setBackClipDistance(100.0);
canvas.addKeyListener(this);
universe.addBranchGraph(scene); }
private BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds = new BoundingSphere(new Point3d(), 10000.0);
viewtrans = universe.getViewingPlatform().getViewPlatformTransform();
KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(viewtrans); keyNavBeh.setSchedulingBounds(bounds); PlatformGeometry platformGeom = new PlatformGeometry(); platformGeom.addChild(keyNavBeh); universe.getViewingPlatform().setPlatformGeometry(platformGeom);
objRoot.addChild(createCubes());
return objRoot; }
private BranchGroup createCubes() {
BranchGroup objRoot = new BranchGroup(); tg = new TransformGroup(); t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.setScale(1.0); tg.setTransform(t3d);
tg.addChild(createCube(0.1, -0.1, 0.1, Math.random())); tg.addChild(createCube(-0.1, -0.1, 0.1, Math.random())); tg.addChild(createCube(0.1, 0.1, 0.1, Math.random())); tg.addChild(createCube(-0.1, 0.1, 0.1, Math.random())); tg.addChild(createCube(0.1, -0.1, -0.1, Math.random())); tg.addChild(createCube(-0.1, -0.1, -0.1, Math.random())); tg.addChild(createCube(0.1, 0.1, -0.1, Math.random())); tg.addChild(createCube(-0.1, 0.1, -0.1, Math.random()));
tg.addChild(createCube(0.3, -0.1, 0.1, Math.random())); tg.addChild(createCube(-0.3, -0.1, 0.1, Math.random())); tg.addChild(createCube(0.3, 0.1, 0.1, Math.random())); tg.addChild(createCube(-0.3, 0.1, 0.1, Math.random())); tg.addChild(createCube(0.3, -0.1, -0.1, Math.random())); tg.addChild(createCube(-0.3, -0.1, -0.1, Math.random())); tg.addChild(createCube(0.3, 0.1, -0.1, Math.random())); tg.addChild(createCube(-0.3, 0.1, -0.1, Math.random()));
tg.addChild(createCube(0.1, -0.3, 0.1, Math.random())); tg.addChild(createCube(-0.1, -0.3, 0.1, Math.random())); tg.addChild(createCube(0.1, 0.3, 0.1, Math.random())); tg.addChild(createCube(-0.1, 0.3, 0.1, Math.random())); tg.addChild(createCube(0.1, -0.3, -0.1, Math.random())); tg.addChild(createCube(-0.1, -0.3, -0.1, Math.random())); tg.addChild(createCube(0.1, 0.3, -0.1, Math.random())); tg.addChild(createCube(-0.1, 0.3, -0.1, Math.random()));
tg.addChild(createCube(0.1, -0.1, 0.3, Math.random())); tg.addChild(createCube(-0.1, -0.1, 0.3, Math.random())); tg.addChild(createCube(0.1, 0.1, 0.3, Math.random())); tg.addChild(createCube(-0.1, 0.1, 0.3, Math.random())); tg.addChild(createCube(0.1, -0.1, -0.3, Math.random())); tg.addChild(createCube(-0.1, -0.1, -0.3, Math.random())); tg.addChild(createCube(0.1, 0.1, -0.3, Math.random())); tg.addChild(createCube(-0.1, 0.1, -0.3, Math.random()));
tg.addChild(createCube(0.3, -0.3, 0.1, Math.random())); tg.addChild(createCube(-0.3, -0.3, 0.1, Math.random())); tg.addChild(createCube(0.3, 0.3, 0.1, Math.random())); tg.addChild(createCube(-0.3, 0.3, 0.1, Math.random())); tg.addChild(createCube(0.3, -0.3, -0.1, Math.random())); tg.addChild(createCube(-0.3, -0.3, -0.1, Math.random())); tg.addChild(createCube(0.3, 0.3, -0.1, Math.random())); tg.addChild(createCube(-0.3, 0.3, -0.1, Math.random()));
tg.addChild(createCube(0.3, -0.1, 0.3, Math.random())); tg.addChild(createCube(-0.3, -0.1, 0.3, Math.random())); tg.addChild(createCube(0.3, 0.1, 0.3, Math.random())); tg.addChild(createCube(-0.3, 0.1, 0.3, Math.random())); tg.addChild(createCube(0.3, -0.1, -0.3, Math.random())); tg.addChild(createCube(-0.3, -0.1, -0.3, Math.random())); tg.addChild(createCube(0.3, 0.1, -0.3, Math.random())); tg.addChild(createCube(-0.3, 0.1, -0.3, Math.random()));
tg.addChild(createCube(0.1, -0.3, 0.3, Math.random())); tg.addChild(createCube(-0.1, -0.3, 0.3, Math.random())); tg.addChild(createCube(0.1, 0.3, 0.3, Math.random())); tg.addChild(createCube(-0.1, 0.3, 0.3, Math.random())); tg.addChild(createCube(0.1, -0.3, -0.3, Math.random())); tg.addChild(createCube(-0.1, -0.3, -0.3, Math.random())); tg.addChild(createCube(0.1, 0.3, -0.3, Math.random())); tg.addChild(createCube(-0.1, 0.3, -0.3, Math.random()));
tg.addChild(createCube(0.3, -0.3, 0.3, Math.random())); tg.addChild(createCube(-0.3, -0.3, 0.3, Math.random())); tg.addChild(createCube(0.3, 0.3, 0.3, Math.random())); tg.addChild(createCube(-0.3, 0.3, 0.3, Math.random())); tg.addChild(createCube(0.3, -0.3, -0.3, Math.random())); tg.addChild(createCube(-0.3, -0.3, -0.3, Math.random())); tg.addChild(createCube(0.3, 0.3, -0.3, Math.random())); tg.addChild(createCube(-0.3, 0.3, -0.3, Math.random()));
objRoot.addChild(tg); objRoot.addChild(createLight());
objRoot.compile();
return objRoot;
}
private BranchGroup createCube(double x, double y, double z, double random) {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
// tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(x, y, z)); t3d.setScale(0.1);
tg.setTransform(t3d);
MovingCube cube = new MovingCube(x, y, z, random); tg.addChild(cube.tg); // cube.tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg.addChild(cube);
objRoot.addChild(tg); objRoot.addChild(createLight());
objRoot.compile();
return objRoot;
}
private Light createLight() { DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(-0.3f, 0.2f, -1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 10000.0));
return light; }
public static void main(String[] args) { Mykeynavbeh applet = new Mykeynavbeh(); Frame frame = new MainFrame(applet, 800, 600); }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar();
}
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
class MovingCube extends Behavior {
public TransformGroup tg = null; public Transform3D t3d = null; private Transform3D t3dstep = new Transform3D(); private WakeupOnElapsedFrames wakeFrame = null;
private double x; private double y; private double z;
private double distance; private double random;
public MovingCube(double xcube, double ycube, double zcube, double randomcube) {
x = xcube; y = ycube; z = zcube;
distance = Math.sqrt(x * x + y * y + z * z); random = randomcube;
t3d = new Transform3D();
tg = new TransformGroup(t3d); t3d.setScale(1.0);
t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
tg.addChild(new ColorCube());
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0); this.setSchedulingBounds(bounds); }
public void initialize() { wakeFrame = new WakeupOnElapsedFrames(0); wakeupOn(wakeFrame); }
public void processStimulus(Enumeration criteria) { t3dstep.set(new Vector3d(random * speed * x / distance, random * speed * y / distance, random * speed * z / distance)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
wakeupOn(wakeFrame); } }
}