/* 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.awt.*;
import javax.media.j3d.*;import javax.vecmath.*;
import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.universe.PlatformGeometry;import com.sun.j3d.utils.behaviors.keyboard.*;
import java.util.*;import java.awt.event.*;import com.sun.j3d.utils.geometry.*;
public class Mykeynavbeh extends Frame {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null; private Transform3D t3dstep = new Transform3D();
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); 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); Background background = new Background(); background.setColor(1.0f, 1.0f, 1.0f); background.setApplicationBounds(bounds); objRoot.addChild(background); objRoot.addChild(createBalls()); objRoot.addChild(createLight());
return objRoot; } private BranchGroup createBalls() {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, -0.4, -3.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(6.0);
tg.setTransform(t3d);
FloatingBalls balls = new FloatingBalls(); tg.addChild(balls.tg);
tg.addChild(balls);
objRoot.addChild(tg); objRoot.compile();
return objRoot;
}
private Appearance createAppearance(Color3f col) { Appearance ap = new Appearance(); Material ma = new Material(); ma.setDiffuseColor(col); ap.setMaterial(ma); return ap; } 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 window = new Mykeynavbeh();
window.setSize(800, 600);
window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
window.setVisible(true); } class FloatingBalls extends Behavior {
public TransformGroup tg = null; public Transform3D t3d = null; private WakeupOnElapsedFrames wakeFrame = null; public TransformGroup tg_2_r = null; public Transform3D t3d_2_r = null; public TransformGroup tg_2_b = null; public Transform3D t3d_2_b = null; public TransformGroup tg_3_rs = null; public Transform3D t3d_3_rs = null; public TransformGroup tg_3_bs = null; public Transform3D t3d_3_bs = null; private double x_r = 0.0; private double y_r = 0.0; private double z_r = 0.0; private double x_b = 0.0; private double y_b = 0.0; private double z_b = 0.0; private double rball_height = 0.1516; private double bball_height = 0.1516; private int count_3 = 0; public FloatingBalls() { tg = new TransformGroup(); t3d = new Transform3D();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0); tg.setTransform(t3d); tg_2_r = new TransformGroup(); t3d_2_r = new Transform3D();
tg_2_r.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_2_r.setTranslation(new Vector3d(0.2, 0.0, -0.5)); tg_2_r.setTransform(t3d_2_r); tg_2_b = new TransformGroup(); t3d_2_b = new Transform3D();
tg_2_b.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_2_b.setTranslation(new Vector3d(-0.2, 0.0, -1.0)); tg_2_b.setTransform(t3d_2_b); Appearance ap_red = createAppearance(new Color3f(1.0f, 0.0f, 0.0f)); Sphere rball = new Sphere(0.05f, ap_red); tg_2_r.addChild(rball); Appearance ap_blue = createAppearance(new Color3f(0.0f, 0.0f, 1.0f)); Sphere bball = new Sphere(0.05f, ap_blue); tg_2_b.addChild(bball); tg_3_rs = new TransformGroup(); t3d_3_rs = new Transform3D(); tg_3_rs.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); t3d_3_rs.setTranslation(new Vector3d(0.1516, -0.1516, -0.0001)); tg_3_rs.setTransform(t3d_3_rs); tg_3_bs = new TransformGroup(); t3d_3_bs = new Transform3D();
tg_3_bs.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_3_bs.setTranslation(new Vector3d(0.1516, -0.1516, -0.0001)); tg_3_bs.setTransform(t3d_3_bs); Appearance ap_black_r = createAppearance(new Color3f(0.0f, 0.0f, 0.0f)); Appearance ap_black_b = createAppearance(new Color3f(0.0f, 0.0f, 0.0f)); Cylinder rball_shdw = new Cylinder(0.05f, 0.0001f, ap_black_r); Cylinder bball_shdw = new Cylinder(0.05f, 0.0001f, ap_black_b); tg_3_rs.addChild(rball_shdw); tg_3_bs.addChild(bball_shdw);
tg_2_r.addChild(tg_3_rs); tg_2_b.addChild(tg_3_bs);
tg.addChild(tg_2_r); tg.addChild(tg_2_b); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0); this.setSchedulingBounds(bounds); }
public void initialize() { wakeFrame = new WakeupOnElapsedFrames(0); wakeupOn(wakeFrame); }
public void processStimulus(Enumeration criteria) { if(count_3 % 2000 == 0){ x_r = Math.random()-0.5; y_r = Math.random()-0.5; z_r = Math.random()-0.5; } if(count_3 % 2000 -1000 == 0){ x_b = Math.random()-0.5; y_b = Math.random()-0.5; z_b = Math.random()-0.5; }
if(rball_height >= 0.055){ t3dstep.set(new Vector3d(x_r * 0.00005, y_r * 0.00005, z_r * 0.00005)); tg_2_r.getTransform(t3d_2_r); t3d_2_r.mul(t3dstep); tg_2_r.setTransform(t3d_2_r); t3dstep.set(new Vector3d(y_r * 0.00005, -y_r * 0.00005, 0.0)); tg_3_rs.getTransform(t3d_3_rs); t3d_3_rs.mul(t3dstep); tg_3_rs.setTransform(t3d_3_rs); rball_height += y_r * 0.00005; }else{ t3dstep.set(new Vector3d(x_r * 0.00005, -y_r * 0.00005*200, z_r * 0.00005)); tg_2_r.getTransform(t3d_2_r); t3d_2_r.mul(t3dstep); tg_2_r.setTransform(t3d_2_r); t3dstep.set(new Vector3d(-y_r * 0.00005*200, y_r * 0.00005*200, 0.0)); tg_3_rs.getTransform(t3d_3_rs); t3d_3_rs.mul(t3dstep); tg_3_rs.setTransform(t3d_3_rs);
rball_height += -y_r * 0.00005*200; System.out.println("rball_height " + rball_height); y_r = -y_r; } if(bball_height >= 0.055){ t3dstep.set(new Vector3d(x_b * 0.00005, y_b * 0.00005, z_b * 0.00005)); tg_2_b.getTransform(t3d_2_b); t3d_2_b.mul(t3dstep); tg_2_b.setTransform(t3d_2_b); t3dstep.set(new Vector3d(y_b * 0.00005, -y_b * 0.00005, 0.0)); tg_3_bs.getTransform(t3d_3_bs); t3d_3_bs.mul(t3dstep); tg_3_bs.setTransform(t3d_3_bs); bball_height += y_b * 0.00005; }else{ t3dstep.set(new Vector3d(x_b * 0.00005, -y_b * 0.00005*200, z_b * 0.00005)); tg_2_b.getTransform(t3d_2_b); t3d_2_b.mul(t3dstep); tg_2_b.setTransform(t3d_2_b); t3dstep.set(new Vector3d(-y_b * 0.00005*200, y_b * 0.00005*200, 0.0)); tg_3_bs.getTransform(t3d_3_bs); t3d_3_bs.mul(t3dstep); tg_3_bs.setTransform(t3d_3_bs); bball_height += -y_b * 0.00005*200; System.out.println("bball_height " + bball_height); y_b = -y_b; } count_3++;
wakeupOn(wakeFrame); } }
}