/* 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.*;
import javax.sound.sampled.DataLine;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Clip;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.UnsupportedAudioFileException;
import java.net.*;
public class Mykeynavbeh extends Frame implements Runnable, 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 String typedKey = "0"; private String keyOrder = "0";
private Switch selector = new Switch(Switch.CHILD_MASK); private BitSet flag_i = new BitSet(2);
private int count = 0;
private Thread thread = new Thread(this);
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);
Background background = new Background(); background.setColor(1.0f, 1.0f, 1.0f); background.setApplicationBounds(bounds); objRoot.addChild(background);
objRoot.addChild(createSquirrel()); objRoot.addChild(createPomeranian());
return objRoot; }
private BranchGroup createSquirrel() {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.3, -0.87, -7.5)); t3d.setScale(0.7);
tg.setTransform(t3d);
MovingSquirrel squirrel = new MovingSquirrel("model/Squirrel_Acorn_dl.obj", "model/Squirrel_Acorn_dl2.obj");
tg.addChild(squirrel.tg); tg.addChild(squirrel);
objRoot.addChild(tg); objRoot.addChild(createLight());
objRoot.compile();
return objRoot;
}
private BranchGroup createPomeranian() {
BranchGroup objRoot = new BranchGroup();
tg = new TransformGroup(); t3d = new Transform3D();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(-0.5, -0.25, -12.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.4); tg.setTransform(t3d);
selector.setCapability(Switch.ALLOW_SWITCH_WRITE);
flag_i.clear(); flag_i.set(1); selector.setChildMask(flag_i);
selector.addChild(createObjLoad("model/Pomeranian_tilt_2_legs_bend_smooth2.obj")); selector.addChild(createObjLoad("model/Pomeranian_tilt_-2_legs_bend_smooth2.obj"));
tg.addChild(selector); 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;
}
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 void getSound(String fname) { Clip clip = null;
try { URL url = this.getClass().getClassLoader().getResource(fname); AudioInputStream audioIn = AudioSystem.getAudioInputStream(url); clip = AudioSystem.getClip(); clip.open(audioIn); clip.start(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } }
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 run() { }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar(); typedKey = String.valueOf(key);
if (key == '3') { flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
t3dstep.set(new Vector3d(0.0, 0.0, 0.1)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
count++; }
if (key == '9') { flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
t3dstep.set(new Vector3d(0.0, 0.0, -0.1)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
count++; }
if (key == '0') { flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
count++; }
if (key == '2') {
flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
t3dstep.rotY(-Math.PI / 32); tg.getTransform(t3d); t3d.get(matrix); t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.mul(t3dstep); t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg.setTransform(t3d);
count++;
}
if (key == '4') {
flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
t3dstep.rotY(Math.PI / 32); tg.getTransform(t3d); t3d.get(matrix); t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.mul(t3dstep); t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg.setTransform(t3d);
count++;
}
if (key == 'h') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'p') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder); }
if (key == 'w') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'u') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 't') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'd') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'm') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'n') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'i') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'o') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'y') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 's') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'g') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'f') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
} if (key == 'l') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'a') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'c') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'v') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'b') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == 'k') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
}
if (key == '.') {
keyOrder = keyOrder + typedKey; System.out.println("keyOrder: " + keyOrder);
switch (keyOrder) { case "0ho.": getSound("Hello_E_s_10.wav"); try { thread.sleep(1000); } catch (InterruptedException ae) { } getSound("Hello_S_F_G_I_C_s_40.wav"); try { thread.sleep(4000); } catch (InterruptedException ae) { } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break;
case "0aiiy.": getSound("Am I interrupting you_s_10.wav"); try { thread.sleep(1500); } catch (InterruptedException ae) { } getSound("No, not at all_s_40.wav"); try { thread.sleep(3000); } catch (InterruptedException ae) { } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break;
case "0ciays.": getSound("Can I ask you something_s_10.wav"); try { thread.sleep(1500); } catch (InterruptedException ae) { } getSound("Sure, anything you want_s_40.wav"); try { thread.sleep(3000); } catch (InterruptedException ae) { } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break;
case "0hdyshig.": getSound("How do you say hello in G_s_10.wav"); try { thread.sleep(1800); } catch (InterruptedException ae) { } for (int i = 0; i < 3; i++) { getSound("Hello_in_G_s_40.wav"); try { thread.sleep(1500); } catch (InterruptedException ae) { } } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break;
case "0hdyshif.": getSound("How do you say hello in F_s_10.wav"); try { thread.sleep(1800); } catch (InterruptedException ae) { } for (int i = 0; i < 3; i++) { getSound("Hello_in_F_s_40.wav"); try { thread.sleep(1500); } catch (InterruptedException ae) { } } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break;
case "0ybvhyawd.": getSound("You've been very helpful_s_10.wav"); try { thread.sleep(3000); } catch (InterruptedException ae) { } getSound("Wow, I'm flattered_s_40.wav"); try { thread.sleep(3000); } catch (InterruptedException ae) { } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break;
case "0tyfykh.": getSound("Thank you for your kind help_s_10.wav"); try { thread.sleep(1500); } catch (InterruptedException ae) { } getSound("Sure! No problem!_s_40.wav"); try { thread.sleep(2000); } catch (InterruptedException ae) { } getSound("I love to eat_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } getSound("Delicious No time_s_40.wav"); try { thread.sleep(3500); } catch (InterruptedException ae) { } break; }
keyOrder = "0"; System.out.println("keyOrder: " + keyOrder);
}
}
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
class MovingSquirrel extends Behavior {
public TransformGroup tg = null; public Transform3D t3d = null; private Transform3D t3dstep = new Transform3D(); private Matrix4d matrix = new Matrix4d();
private WakeupOnElapsedFrames wakeFrame = null;
private Switch selector = new Switch(Switch.CHILD_MASK); private BitSet flag_i = new BitSet(2);
private int count = 0;
private double x = 0.0f; private double z = 0.0f; private double r = 0.0f;
public MovingSquirrel(String filename, String filename2) {
tg = new TransformGroup(); t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, -1.25f)); t3d.setScale(1.0); tg.setTransform(t3d);
selector.setCapability(Switch.ALLOW_SWITCH_WRITE);
flag_i.clear(); flag_i.set(1); selector.setChildMask(flag_i);
selector.addChild(createObjLoad(filename)); selector.addChild(createObjLoad(filename2));
tg.addChild(selector); 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(30); wakeupOn(wakeFrame); }
public void processStimulus(Enumeration criteria) {
x = (Math.random() - 0.5) * 0.1; z = (Math.random()) * 0.05; r = (Math.random() - 0.5) * 1.2;
t3dstep.set(new Vector3d(x, 0.0, z)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
t3dstep.rotY(r); tg.getTransform(t3d); t3d.get(matrix); t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.mul(t3dstep); t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg.setTransform(t3d);
flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
count++;
wakeupOn(wakeFrame); } }
}