/* 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 com.sun.j3d.loaders.Scene;
import java.awt.event.KeyListener;import java.awt.event.KeyEvent;
import java.util.*;
import com.sun.j3d.loaders.objectfile.ObjectFile;import java.io.*;
import java.awt.event.*;
public class Mykeynavbeh extends Frame implements KeyListener {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null;
private TransformGroup tg_f = null; private Transform3D t3d_f = null;
private Transform3D t3dstep = new Transform3D(); private Matrix4d matrix = new Matrix4d();
private Switch selector_2 = new Switch(Switch.CHILD_MASK); private BitSet flag_i_2 = new BitSet(6);
public Mykeynavbeh() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config); add("Center", canvas); universe = new SimpleUniverse(canvas);
canvas.addKeyListener(this);
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);
objRoot.addChild(createPomeranians());
return objRoot; }
private BranchGroup createPomeranians() {
BranchGroup objRoot = new BranchGroup();
tg_f = new TransformGroup(); t3d_f = new Transform3D();
tg_f.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_f.setTranslation(new Vector3d(0.0, -2.0, -10.0)); t3d_f.setScale(1.0); tg_f.setTransform(t3d_f);
tg_f.addChild(createPonta_Pom()); tg_f.addChild(createLeo());
objRoot.addChild(tg_f); objRoot.compile();
return objRoot;
}
private BranchGroup createPonta_Pom() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
TransformGroup tg_pt = new TransformGroup(); Transform3D t3d_pt = new Transform3D();
TransformGroup tg_p = new TransformGroup(); Transform3D t3d_p = new Transform3D();
t3d_pt.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_pt.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 2.9f)); tg_pt.setTransform(t3d_pt);
t3d_p.setTranslation(new Vector3d(1.1, 0.0, 0.0)); t3d_p.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 3.14f)); tg_p.setTransform(t3d_p);
tg_pt.addChild(createObjLoad("model/Sitting_Pomeranian.obj")); tg_p.addChild(createObjLoad("model/Sitting_Pomeranian.obj")); tg.addChild(tg_pt); tg.addChild(tg_p); objRoot.addChild(tg); objRoot.compile();
return objRoot;
}
private BranchGroup createLeo() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(-1.7, 0.5, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 3.14f)); t3d.setScale(1.5); tg.setTransform(t3d);
selector_2.setCapability(Switch.ALLOW_SWITCH_WRITE); selector_2.addChild(createObjLoad("model/Sitting_Leo.obj")); selector_2.addChild(createObjLoad("model/Leo_right_paw_left.obj")); selector_2.addChild(createObjLoad("model/Leo_right_paw_right.obj")); selector_2.addChild(createObjLoad("model/Leo_right_paw_up.obj")); selector_2.addChild(createObjLoad("model/Leo_right_paw_down.obj")); selector_2.addChild(createObjLoad("model/Leo_right_paw_more_right.obj"));
flag_i_2.clear(); flag_i_2.set(0); selector_2.setChildMask(flag_i_2);
tg.addChild(selector_2); objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
private BranchGroup createObjLoad(String filename) {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(1.0); tg.setTransform(t3d);
ObjectFile loader = new ObjectFile(ObjectFile.RESIZE); Scene s = null;
File file = new java.io.File(filename);
try { s = loader.load(file.toURI().toURL()); } catch (Exception e) { System.err.println(e); System.exit(1); }
tg.addChild(s.getSceneGroup());
objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
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); }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar();
if (key == 's') {
flag_i_2.clear(); flag_i_2.set(1); selector_2.setChildMask(flag_i_2);
}
if (key == 'f') {
flag_i_2.clear(); flag_i_2.set(2); selector_2.setChildMask(flag_i_2);
}
if (key == 'e') {
flag_i_2.clear(); flag_i_2.set(3); selector_2.setChildMask(flag_i_2);
}
if (key == 'c') {
flag_i_2.clear(); flag_i_2.set(4); selector_2.setChildMask(flag_i_2);
}
if (key == '@') {
flag_i_2.clear(); flag_i_2.set(5); selector_2.setChildMask(flag_i_2);
}
if (key == '^') {
t3dstep.rotY(-Math.PI / 32); tg_f.getTransform(t3d_f); t3d_f.get(matrix); t3d_f.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_f.mul(t3dstep); t3d_f.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg_f.setTransform(t3d_f);
}
if (key == '~') {
t3dstep.rotY(Math.PI / 32); tg_f.getTransform(t3d_f); t3d_f.get(matrix); t3d_f.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_f.mul(t3dstep); t3d_f.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg_f.setTransform(t3d_f);
} }
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
}