/* 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 javax.media.j3d.WakeupCriterion;import javax.media.j3d.WakeupOnCollisionEntry;import javax.media.j3d.WakeupOnCollisionExit;import javax.media.j3d.WakeupOnCollisionMovement;import javax.media.j3d.WakeupOr;
import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Clip;import javax.sound.sampled.DataLine;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.UnsupportedAudioFileException;
import java.util.*;import java.net.*;
import com.sun.j3d.loaders.objectfile.ObjectFile;import java.io.*;
import java.awt.event.*;import com.sun.j3d.utils.image.TextureLoader;import com.sun.j3d.utils.geometry.*;import java.text.SimpleDateFormat;
public class Mykeynavbeh extends Frame 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 Switch selector = new Switch(Switch.CHILD_MASK); private BitSet flag_i = new BitSet(2);
private Switch selector_2 = new Switch(Switch.CHILD_MASK); private BitSet flag_i_2 = new BitSet(16);
private int count = 0; private int count_2 = 0;
private Text2D text2d; private Text2D text2d_2;
private boolean collision_w = false; private boolean collision_b = false;
private Alpha alpha = null;
private TransformGroup tg_2 = null; private Transform3D t3d_2 = null;
private KeyNavigatorBehavior keyNavBeh;
private CollisionDetectorGroup cdGroup = null;
private int mode = TransparencyAttributes.NICEST; private TransparencyAttributes transAttr_w = new TransparencyAttributes(mode, 0.0f); private TransparencyAttributes transAttr_b = new TransparencyAttributes(mode, 0.0f);
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();
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(createPomeranian()); objRoot.addChild(createText2D()); 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);
FloatingBall ball_w = new FloatingBall(0.25, -0.0, -0.4, 1.0f, 1.0f, 1.0f, 1, 0, transAttr_w); FloatingBall ball_b = new FloatingBall(-0.2, -0.0, -1.0, 0.0f, 0.0f, 1.0f, 2, 1000, transAttr_b); tg.addChild(ball_w.tg); tg.addChild(ball_w); tg.addChild(ball_b.tg); tg.addChild(ball_b);
objRoot.addChild(tg); 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.0, -0.409, -8.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(0.9);
tg.setTransform(t3d);
TransformGroup tg_j = new TransformGroup(); tg_j.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
alpha = new Alpha(1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 0, 0, 300, 300, 0, 300, 300, 0);
Transform3D axis = new Transform3D(); axis.setRotation(new AxisAngle4f(0.0f, 0.0f, 1.0f, 1.57f));
PositionInterpolator posit = new PositionInterpolator(alpha, tg_j, axis, 0.0f, 0.6f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0); posit.setSchedulingBounds(bounds);
tg_j.addChild(posit); tg_j.addChild(createPomeranian2());
Appearance ap_yel = createAppearance(new Color3f(1.0f, 1.0f, 0.0f)); Sphere yball = new Sphere(0.6f, ap_yel);
int mode = TransparencyAttributes.NICEST; TransparencyAttributes transAttr_y = new TransparencyAttributes(mode, 1.0f); transAttr_y.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); ap_yel.setTransparencyAttributes(transAttr_y);
TransformGroup tg_y = new TransformGroup(); tg_y.addChild(yball); tg_j.addChild(tg_y);
tg.addChild(tg_j);
tg_2 = new TransformGroup(); t3d_2 = new Transform3D(); tg_2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
TransformGroup tg_sh = new TransformGroup(); Transform3D t3d_sh = new Transform3D(); tg_sh.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D axis2 = new Transform3D();
PositionInterpolator posit2 = new PositionInterpolator(alpha, tg_sh, axis2, 0.0f, 0.34f); posit2.setSchedulingBounds(bounds);
tg_sh.addChild(posit2); tg_sh.addChild(createPomeranianShadow()); tg_2.addChild(tg_sh); tg.addChild(tg_2);
t3d_2.setTranslation(new Vector3d(0.0, -1.0, 0.0)); t3d_2.setScale(2.7); tg_2.setTransform(t3d_2);
cdGroup = new CollisionDetectorGroup(tg_y); cdGroup.setSchedulingBounds(bounds); tg_y.addChild(cdGroup); tg.setCollidable(false);
objRoot.addChild(tg); objRoot.addChild(createAmbientLight());
objRoot.compile();
return objRoot;
}
private BranchGroup createPomeranian2() {
BranchGroup objRoot = new BranchGroup();
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_smooth.obj")); selector.addChild(createObjLoad("model/Pomeranian_tilt_-2_legs_bend_smooth.obj")); selector.addChild(createObjLoad("model/Pomeranian_jump_sm.obj", 0.12, -0.12, 1.07));
objRoot.addChild(selector);
objRoot.compile();
return objRoot;
}
private BranchGroup createPomeranianShadow() {
BranchGroup objRoot = new BranchGroup();
selector_2.setCapability(Switch.ALLOW_SWITCH_WRITE);
for (int i = 0; i < 16; i++) {
selector_2.addChild(createOrientedShape3D(1.0f, "model/pomeranian_shadow" + i + ".png")); }
System.out.println("count_2 * 2 % 16_initially: " + count_2 * 2 % 16);
flag_i_2.clear(); flag_i_2.set(0); selector_2.setChildMask(flag_i_2);
objRoot.addChild(selector_2);
objRoot.compile();
return objRoot;
}
public BranchGroup createOrientedShape3D(float size, String filename) {
BranchGroup root = new BranchGroup(); TransformGroup tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Appearance ap = new Appearance();
TransparencyAttributes attr = new TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f); ap.setTransparencyAttributes(attr);
TextureLoader loader = new TextureLoader(filename, this); ap.setTexture(loader.getTexture());
QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
Point3f[] p = new Point3f[4];
p[0] = new Point3f(size, 0.0f, size); p[1] = new Point3f(size, 0.0f, -size); p[2] = new Point3f(-size, 0.0f, -size); p[3] = new Point3f(-size, 0.0f, size); plane.setCoordinates(0, p);
TexCoord2f[] q = new TexCoord2f[4]; q[0] = new TexCoord2f(1.0f, 1.0f); q[1] = new TexCoord2f(0.0f, 1.0f); q[2] = new TexCoord2f(0.0f, 0.0f); q[3] = new TexCoord2f(1.0f, 0.0f); plane.setTextureCoordinates(0, 0, q);
OrientedShape3D oriShape = new OrientedShape3D(plane, ap, 0, new Point3f(0.0f, 0.0f, 0.0f));
Transform3D t3d = new Transform3D(); t3d.setScale(1.0);
tg.setTransform(t3d);
tg.addChild(oriShape);
root.addChild(tg);
root.compile();
return root; }
public BranchGroup createShape3D(int num) {
BranchGroup root = new BranchGroup(); TransformGroup tg = new TransformGroup();
Appearance ap = new Appearance();
float size = 0.042f;
TransparencyAttributes attr = new TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f); ap.setTransparencyAttributes(attr);
TextureLoader loader = new TextureLoader("model/transparent.png", this); ap.setTexture(loader.getTexture());
Point3f[] vertex = new Point3f[4]; vertex[0] = new Point3f(size, size, 0.0f); vertex[1] = new Point3f(-size, size, 0.0f); vertex[2] = new Point3f(-size, -size, 0.0f); vertex[3] = new Point3f(size, -size, 0.0f);
QuadArray quadA = new QuadArray(vertex.length, GeometryArray.COORDINATES | GeometryArray.NORMALS);
quadA.setCoordinates(0, vertex); Shape3D quad3Dw = new Shape3D(quadA, ap); Shape3D quad3Db = new Shape3D(quadA, ap);
if (num == 1) { tg.addChild(quad3Dw); quad3Dw.setUserData(new String("white")); } else if (num == 2) { tg.addChild(quad3Db); quad3Db.setUserData(new String("blue")); } root.addChild(tg);
root.compile();
return root; }
private BranchGroup createText2D() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
TransformGroup tg_2 = new TransformGroup(); Transform3D t3d_2 = new Transform3D();
t3d.setTranslation(new Vector3d(0.2, 0.35, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0); tg.setTransform(t3d);
t3d_2.setTranslation(new Vector3d(0.55, 0.35, 0.0)); t3d_2.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d_2.setScale(1.0); tg_2.setTransform(t3d_2);
text2d = new Text2D("", new Color3f(1.0f, 0.0f, 0.0f), "Helvetica", 24, Font.ITALIC); text2d_2 = new Text2D("", new Color3f(0.0f, 0.0f, 0.8f), "Helvetica", 28, Font.ITALIC);
tg.addChild(text2d); tg_2.addChild(text2d_2); objRoot.addChild(tg); objRoot.addChild(tg_2);
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 BranchGroup createObjLoad(String filename, double y, double z, double scale) {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(0.0, y, z)); t3d.setScale(scale); 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 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; }
private Light createAmbientLight() { AmbientLight light = new AmbientLight(new Color3f(0.1f, 0.1f, 0.1f));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.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); }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar();
if (key == 'd') { flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
flag_i_2.clear(); flag_i_2.set(count_2 * 2 % 16); selector_2.setChildMask(flag_i_2);
t3dstep.set(new Vector3d(0.0, 0.0, 0.1)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
count++; }
if (key == 's') {
flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
count_2++; flag_i_2.clear(); flag_i_2.set(count_2 * 2 % 16); selector_2.setChildMask(flag_i_2);
System.out.println("count_2*2 % 16_'s': " + count_2 * 2 % 16);
t3dstep.rotY(Math.PI / 4); 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);
t3dstep.rotY(-Math.PI / 4); tg_2.getTransform(t3d_2); t3d_2.get(matrix); t3d_2.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_2.mul(t3dstep); t3d_2.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg_2.setTransform(t3d_2);
count++;
}
if (key == 'f') {
flag_i.clear(); flag_i.set(count % 2); selector.setChildMask(flag_i);
count_2--; if (count_2 < 0) { count_2 = count_2 + 8; } flag_i_2.clear(); flag_i_2.set(count_2 * 2 % 16); selector_2.setChildMask(flag_i_2);
System.out.println("count_2*2 % 16'f': " + count_2 * 2 % 16);
t3dstep.rotY(-Math.PI / 4); 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);
t3dstep.rotY(Math.PI / 4); tg_2.getTransform(t3d_2); t3d_2.get(matrix); t3d_2.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_2.mul(t3dstep); t3d_2.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg_2.setTransform(t3d_2);
count++;
}
}
public void keyReleased(KeyEvent e) { keyNavBeh.setEnable(true); }
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) { case KeyEvent.VK_RIGHT: keyNavBeh.setEnable(false); break;
case KeyEvent.VK_LEFT: keyNavBeh.setEnable(false); break;
case KeyEvent.VK_KP_RIGHT: keyNavBeh.setEnable(false); break;
case KeyEvent.VK_KP_LEFT: keyNavBeh.setEnable(false); break;
}
char key = e.getKeyChar();
if (key == 'j') { alpha.setStartTime(System.currentTimeMillis()); flag_i.clear(); flag_i.set(2); selector.setChildMask(flag_i);
if (count_2 < 0) { count_2 = count_2 + 8; } flag_i_2.clear(); flag_i_2.set(count_2 * 2 % 16 + 1); selector_2.setChildMask(flag_i_2);
} }
class FloatingBall extends Behavior {
public TransformGroup tg = null; public Transform3D t3d = null;
private WakeupOnElapsedFrames wakeFrame = null;
private long start;
private long delay; private boolean firstTime = true;
private long current; private long elapsed; private long end;
public TransformGroup tg_2 = null; public Transform3D t3d_2 = null;
public TransformGroup tg_3_s = null; public Transform3D t3d_3_s = null;
private double x = 0.0; private double y = 0.0; private double z = 0.0;
private double ball_height = 0.1516;
private int count_3 = 0; private int timing_diff = 0;
private boolean done = false; private SimpleDateFormat format = new SimpleDateFormat("mm:ss");
public FloatingBall(double x, double y, double z, float col_r, float col_g, float col_b, int num, int t_diff, TransparencyAttributes transAttr) {
timing_diff = t_diff;
tg = new TransformGroup(); t3d = new Transform3D();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg_2 = new TransformGroup(); t3d_2 = new Transform3D();
tg_2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_2.setTranslation(new Vector3d(x, y, z)); tg_2.setTransform(t3d_2); Appearance ap_col = createAppearance(new Color3f(col_r, col_g, col_b)); Sphere ball = new Sphere(0.05f, ap_col);
transAttr.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); ap_col.setTransparencyAttributes(transAttr);
tg_2.addChild(createShape3D(num)); ball.setCollidable(false); tg_2.addChild(ball);
tg_3_s = new TransformGroup(); t3d_3_s = new Transform3D();
tg_3_s.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_3_s.setTranslation(new Vector3d(0.1516, -0.1516, -0.0001)); tg_3_s.setTransform(t3d_3_s);
Appearance ap_black = createAppearance(new Color3f(0.0f, 0.0f, 0.0f));
Cylinder ball_shdw = new Cylinder(0.05f, 0.0001f, ap_black);
tg_3_s.addChild(ball_shdw);
tg_3_s.setCollidable(false);
tg_2.addChild(tg_3_s);
tg.addChild(tg_2);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0); this.setSchedulingBounds(bounds);
start = System.currentTimeMillis();
}
public void initialize() { wakeFrame = new WakeupOnElapsedFrames(0); wakeupOn(wakeFrame); }
public void processStimulus(Enumeration criteria) {
if (count_3 % 2000 - timing_diff == 0) { x = Math.random() - 0.5; y = Math.random() - 0.5; z = Math.random() - 0.5;
}
if (ball_height >= 0.055) {
t3dstep.set(new Vector3d(x * 0.00005, y * 0.00005, z * 0.00005)); tg_2.getTransform(t3d_2); t3d_2.mul(t3dstep); tg_2.setTransform(t3d_2);
t3dstep.set(new Vector3d(y * 0.00005, -y * 0.00005, 0.0)); tg_3_s.getTransform(t3d_3_s); t3d_3_s.mul(t3dstep); tg_3_s.setTransform(t3d_3_s);
ball_height += y * 0.00005;
} else {
t3dstep.set(new Vector3d(x * 0.00005, -y * 0.00005 * 200, z * 0.00005)); tg_2.getTransform(t3d_2); t3d_2.mul(t3dstep); tg_2.setTransform(t3d_2);
t3dstep.set(new Vector3d(-y * 0.00005 * 200, y * 0.00005 * 200, 0.0)); tg_3_s.getTransform(t3d_3_s); t3d_3_s.mul(t3dstep); tg_3_s.setTransform(t3d_3_s);
ball_height += -y * 0.00005 * 200;
y = -y;
}
count_3++;
if (!done) { if (collision_w && collision_b) { end = System.currentTimeMillis(); elapsed = end - start - delay; String str = format.format(elapsed); done = true; } else { current = System.currentTimeMillis(); if (firstTime) { delay = current - start; firstTime = false; } elapsed = current - start - delay; // System.out.println("elapsed: "+elapsed); } }
if (!done) { String str = format.format(elapsed); text2d_2.setString(str);
if (elapsed >= 8000) { text2d.setString("GAME OVER"); text2d_2.setString(""); }
}
wakeupOn(wakeFrame);
} }
class CollisionDetectorGroup extends Behavior {
protected WakeupCriterion[] theCriteria; protected WakeupOr oredCriteria;
private boolean inCollision = false; private Group group; private Clip clip1; private Node theLeaf;
public CollisionDetectorGroup(Group gp) { group = gp; inCollision = false; }
public void initialize() {
theCriteria = new WakeupCriterion[3]; theCriteria[0] = new WakeupOnCollisionEntry(group); theCriteria[1] = new WakeupOnCollisionExit(group); theCriteria[2] = new WakeupOnCollisionMovement(group); oredCriteria = new WakeupOr(theCriteria); wakeupOn(oredCriteria); }
public void processStimulus(Enumeration criteria) {
WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement();
if (theCriterion instanceof WakeupOnCollisionEntry) { theLeaf = ((WakeupOnCollisionEntry) theCriterion).getTriggeringPath().getObject();
} else if (theCriterion instanceof WakeupOnCollisionExit) { theLeaf = ((WakeupOnCollisionExit) theCriterion).getTriggeringPath().getObject();
} else { theLeaf = ((WakeupOnCollisionMovement) theCriterion).getTriggeringPath().getObject();
}
inCollision = !inCollision; if (inCollision) {
clip1 = getSound("sound/bell03.wav");
clip1.setFramePosition(0); clip1.start();
wakeupOn(theCriteria[1]);
if ((theLeaf.getUserData()).equals("white")) { System.out.println("colliding with a white ball");
transAttr_w.setTransparency(0.8f); collision_w = true; }
if ((theLeaf.getUserData()).equals("blue")) { System.out.println("colliding with a blue ball");
transAttr_b.setTransparency(0.8f); collision_b = true; }
} else { wakeupOn(theCriteria[0]); } }
public Clip 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(); }
return clip; }
}
}