/* 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.geometry.*;import javax.vecmath.Vector3d;import com.sun.j3d.utils.behaviors.keyboard.*;import java.util.*;
public class Mykeynavbeh extends Applet {
private SimpleUniverse universe = null; private Canvas3D canvas = null;
private Applet applet = null; private Transform3D transform = new Transform3D(); private TransformGroup viewtrans = null;
public static final double ANGLE = Math.PI / 16; private Transform3D t3d_ang = new Transform3D(); private TransformGroup fairy_w = null;
public Mykeynavbeh() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); add("Center", canvas); universe = new SimpleUniverse(canvas);
BranchGroup scene = createSceneGraph();
universe.getViewingPlatform().setNominalViewingTransform(); universe.getViewer().getView().setBackClipDistance(100.0); universe.addBranchGraph(scene); }
private BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); TransformGroup tg2 = new TransformGroup(); TransformGroup tg3 = new TransformGroup(); TransformGroup tg4 = new TransformGroup();
fairy_w = new TransformGroup(); Transform3D t3d_w = new Transform3D();
Transform3D t3da = new Transform3D(); Transform3D t3da3 = new Transform3D(); Transform3D t3da4 = new Transform3D();
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);
Background background = new Background(); background.setColor(30, 50, 30); background.setApplicationBounds(bounds); objRoot.addChild(background);
RotateColorCube cube = new RotateColorCube(); tg.addChild(cube.tg);
RotateColorCube cube2 = new RotateColorCube(); tg2.addChild(cube2.tg); t3da.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 3.14f)); tg2.setTransform(t3da);
RotateColorCube cube3 = new RotateColorCube(); tg3.addChild(cube3.tg); t3da3.setTranslation(new Vector3d(-0.011, 0.21, -0.1)); t3da3.setScale(new Vector3d(0.3d, 0.3d, 1.0d)); tg3.setTransform(t3da3);
RotateColorCube cube4 = new RotateColorCube(); tg4.addChild(cube4.tg); t3da4.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 3.14f)); t3da4.setTranslation(new Vector3d(0.011, 0.21, -0.1)); t3da4.setScale(new Vector3d(0.3d, 0.3d, 1.0d)); tg4.setTransform(t3da4);
fairy_w.addChild(tg); fairy_w.addChild(cube); fairy_w.addChild(tg2); fairy_w.addChild(cube2); fairy_w.addChild(tg3); fairy_w.addChild(cube3); fairy_w.addChild(tg4); fairy_w.addChild(cube4);
t3d_ang.rotX(-ANGLE * 12); fairy_w.getTransform(t3d_w); t3d_w.setTranslation(new Vector3d(0.0, 0.10, 0.81)); t3d_w.mul(t3d_ang); t3d_w.setScale(new Vector3d(1.5d, 1.4d, 0.5d)); fairy_w.setTransform(t3d_w);
objRoot.addChild(fairy_w);
objRoot.addChild(createLight()); objRoot.addChild(createLight2());
return objRoot; }
private Light createLight() { DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(-1.0f, -1.0f, -1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));
return light; }
private Light createLight2() { DirectionalLight light = new DirectionalLight(true, new Color3f(0.0f, 0.0f, 1.0f), new Vector3f(1.0f, 1.0f, 1.0f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));
return light; }
public static void main(String[] args) { Mykeynavbeh applet = new Mykeynavbeh(); Frame frame = new MainFrame(applet, 800, 600); }}
class RotateColorCube extends Behavior {
public TransformGroup tg = null; public Transform3D t3d = null;
private WakeupOnElapsedFrames wakeFrame = null; private float angle = 0.0f; private float height = 0.0f; private int count = 0;
public RotateColorCube() { t3d = new Transform3D(); t3d.setScale(1.8);
int flagcyl = Cylinder.GENERATE_NORMALS | Cylinder.GENERATE_TEXTURE_COORDS | Cylinder.ENABLE_GEOMETRY_PICKING;
Cylinder cylinder = new Cylinder(0.06f, 0.00015f * 20, flagcyl, 30, 30, null);
tg = new TransformGroup(t3d); tg.addChild(cylinder);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 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(5); wakeupOn(wakeFrame); }
public void processStimulus(Enumeration criteria) {
count++; if (count % 100 >= 25) { angle -= 0.01f; height += 0.001f;
} if (count % 100 < 25) { angle += 0.03f; height -= 0.003f; }
t3d.setTranslation(new Vector3d(-0.1, height + 0.2f, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 1.0f, angle)); tg.setTransform(t3d);
wakeupOn(wakeFrame); }}