/* 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.behaviors.keyboard.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import java.io.*;import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.geometry.Text2D;import com.sun.j3d.utils.image.TextureLoader;
import java.awt.event.ActionListener;import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;import java.awt.event.KeyEvent;
public class Mykeynavbeh extends Applet implements MouseListener, KeyListener {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null; private Transform3D t3d_vt = new Transform3D();
// private TransformGroup tg = null; // private Transform3D t3d = null; private Transform3D t3dstep = new Transform3D(); private Matrix4d matrix = new Matrix4d();
private PickCanvas pickCanvas = null; private String sensorID = null;
private Shape3D quad3D; private Text2D text2d_message; private Text2D text2d_message2;
private Label label = null; private TextField text = null; private Label label2 = null; private TextField text2 = null; private Button sign_up = null;
private Label label3 = null; private TextField text3 = null; private Label label4 = null; private TextField text4 = null; private Button log_in = null;
private Panel card1 = null; private Panel card2 = null; private CardLayout layout = null;
private Panel p = null;
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);
canvas.addKeyListener(this); canvas.addMouseListener(this);
universe.addBranchGraph(scene);
label = new Label("User Name:"); label.setFont(new Font("serif", Font.BOLD, 15)); text = new TextField("", 10); text.setFont(new Font("Times New Roman", Font.BOLD, 15));
label2 = new Label("Password:"); label2.setFont(new Font("serif", Font.BOLD, 15)); text2 = new TextField("", 10); text2.setEchoChar('*');
sign_up = new Button("Sign_up"); sign_up.setFont(new Font("serif", Font.BOLD, 15));
label3 = new Label("User Name:"); label3.setFont(new Font("serif", Font.BOLD, 15)); text3 = new TextField("", 10); text3.setFont(new Font("Times New Roman", Font.BOLD, 15));
label4 = new Label("Password:"); label4.setFont(new Font("serif", Font.BOLD, 15)); text4 = new TextField("", 10); text4.setEchoChar('*');
log_in = new Button("Log_in"); log_in.setFont(new Font("serif", Font.BOLD, 15));
card1 = new Panel(); card2 = new Panel();
card2.add(label); card2.add(text); card2.add(label2); card2.add(text2); card2.add(sign_up);
card2.add(label3); card2.add(text3); card2.add(label4); card2.add(text4); card2.add(log_in);
layout = new CardLayout();
p = new Panel(); p.setLayout(layout);
p.add(card1); p.add(card2);
add(p, "North");
sign_up.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { p.setVisible(false);
text2d_message.setString(""); text2d_message2.setString(""); } });
log_in.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { p.setVisible(false);
text2d_message.setString(""); text2d_message2.setString(""); } });
}
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(0.8f, 0.8f, 0.7f); background.setApplicationBounds(bounds); objRoot.addChild(background);
objRoot.addChild(createPicture(0.0, -0.17, -9.97, 0.0f, "model/Mona_Lisa.obj")); objRoot.addChild(createPicture(-2.7, -0.17, -9.97, 0.0f, "model/Dance_Class.obj"));
objRoot.addChild(createPicture(3.2, -0.15, -9.97, 0.0f, "model/Girl_Pearl_Earring.obj")); objRoot.addChild(createPicture(9.97, -0.14, -2.0, -1.57f, "model/Gleaners.obj")); objRoot.addChild(createPicture(9.97, -0.17, 1.0, -1.57f, "model/Rideau.obj")); objRoot.addChild(createPicture(4.7, -0.17, 9.97, -3.14f, "model/Fifer.obj")); objRoot.addChild(createPicture(2.0, -0.17, 9.97, -3.14f, "model/Sunflowers.obj")); objRoot.addChild(createPicture(-5.0, -0.07, 9.97, -3.14f, "model/Dimanche.obj")); objRoot.addChild(createPicture(-14.96, -0.17, 4.0, 1.57f, "model/Woman_Parasol.obj")); objRoot.addChild(createPicture(-14.96, -0.2, -0.0, 1.57f, "model/Scream.obj")); objRoot.addChild(createPicture(-14.96, -0.2, -4.0, 1.57f, "model/Mill.obj")); objRoot.addChild(createPicture(-14.96, -0.2, -8.0, 1.57f, "model/Starry_Night.obj"));
objRoot.addChild(createMuseum()); objRoot.addChild(createPicking());
objRoot.addChild(createText2D_Message());
return objRoot; }
private BranchGroup createMuseum() {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D 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, 1.0f, 0.0f, -1.2f)); t3d.setScale(1.0);
tg.setTransform(t3d);
ObjectFile loader = new ObjectFile(); Scene s = null;
File file = new java.io.File("model/museum.obj");
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.addChild(createAmbientLight());
objRoot.compile();
return objRoot;
}
private BranchGroup createPicture(double x, double y, double z, float r, String filename) {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
// tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(x, y, z)); t3d.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, r)); t3d.setScale(0.01);
tg.setTransform(t3d);
ObjectFile loader = new ObjectFile(); 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.addChild(createLight(-0.6f, -0.2f, -1.0f)); objRoot.addChild(createLight(0.6f, 0.2f, 1.0f));
objRoot.compile();
return objRoot;
}
public BranchGroup createPicking() { BranchGroup objRoot = new BranchGroup();
pickCanvas = new PickCanvas(canvas, objRoot);
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.3, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0); tg.setTransform(t3d);
tg.addChild(createText2D(" ", -0.054, "right eye")); tg.addChild(createText2D(" ", 0.0002, "left eye"));
tg.addChild(createShape3D("Mona_Lisa", 0.0, -0.47, 0.40f, 0.52f)); tg.addChild(createShape3D("Dance_Class", -2.7, -0.47, 0.539f, 0.5f));
objRoot.addChild(tg);
objRoot.compile();
return objRoot; }
private BranchGroup createText2D(String eye, double x, String sensorString) {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(x, -0.259, -9.9601)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0);
tg.setTransform(t3d);
Text2D text2d = new Text2D(eye, new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 3, Font.ITALIC);
// text2d.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
text2d.setUserData(sensorString);
tg.addChild(text2d);
objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
private BranchGroup createText2D_Message() {
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.5, 0.3, -9.96)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0); tg.setTransform(t3d);
t3d_2.setTranslation(new Vector3d(-3.3, 0.3, -9.96)); t3d_2.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d_2.setScale(1.0); tg_2.setTransform(t3d_2);
text2d_message = new Text2D("", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 48, Font.ITALIC); text2d_message2 = new Text2D("", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 48, Font.ITALIC);
tg.addChild(text2d_message); objRoot.addChild(tg); tg_2.addChild(text2d_message2); objRoot.addChild(tg_2);
objRoot.compile();
return objRoot;
}
public BranchGroup createShape3D(String sensorString, double x_pos, double y_pos, float x_size, float y_size) {
BranchGroup root = new BranchGroup(); TransformGroup tg = new TransformGroup();
Appearance ap = new Appearance();
float x = x_size; float y = y_size;
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(x, y, 0.0f); vertex[1] = new Point3f(-x, y, 0.0f); vertex[2] = new Point3f(-x, -y, 0.0f); vertex[3] = new Point3f(x, -y, 0.0f);
QuadArray quadA = new QuadArray(vertex.length, GeometryArray.COORDINATES | GeometryArray.NORMALS);
quadA.setCoordinates(0, vertex); quad3D = new Shape3D(quadA, ap); quad3D.setUserData(sensorString);
Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(x_pos, y_pos, -9.97)); t3d.setScale(1.0);
tg.setTransform(t3d);
tg.addChild(quad3D);
root.addChild(tg);
root.compile();
return root; }
private Light createLight(float x, float y, float z) { DirectionalLight light = new DirectionalLight(true, new Color3f(0.2f, 0.2f, 0.2f), new Vector3f(x, y, z));
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 applet = new Mykeynavbeh(); Frame frame = new MainFrame(applet, 1000, 600); }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar();
if (key == 'd') { t3dstep.set(new Vector3d(0.0, 0.0, -0.3)); viewtrans.getTransform(t3d_vt); t3d_vt.mul(t3dstep); viewtrans.setTransform(t3d_vt); }
if (key == 's') {
t3dstep.rotY(Math.PI / 256); viewtrans.getTransform(t3d_vt); t3d_vt.get(matrix); t3d_vt.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_vt.mul(t3dstep); t3d_vt.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); viewtrans.setTransform(t3d_vt);
}
if (key == 'f') {
t3dstep.rotY(-Math.PI / 256); viewtrans.getTransform(t3d_vt); t3d_vt.get(matrix); t3d_vt.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_vt.mul(t3dstep); t3d_vt.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); viewtrans.setTransform(t3d_vt);
}
if (key == 'c') { t3dstep.set(new Vector3d(0.0, 0.0, 0.3)); viewtrans.getTransform(t3d_vt); t3d_vt.mul(t3dstep); viewtrans.setTransform(t3d_vt); } }
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
public void mousePressed(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) {
pickCanvas.setShapeLocation(evt.getX(), evt.getY()); PickResult result = pickCanvas.pickClosest();
if (result != null) { Node node = result.getNode(PickResult.SHAPE3D); sensorID = (node.getUserData()).toString();
switch (sensorID) { case "right eye":
System.out.println("The right eye is clicked: " + sensorID); text2d_message.setString("The right eye is clicked:"); text2d_message2.setString("");
text.setText("" + '\u0000'); text2.setText("" + '\u0000'); text3.setText("" + '\u0000'); text4.setText("" + '\u0000');
p.setVisible(true); card1.setVisible(false); card2.setVisible(true);
break;
case "left eye":
System.out.println("The left eye is clicked: " + sensorID); text2d_message.setString("The left eye is clicked: "); text2d_message2.setString("");
break;
case "Mona_Lisa":
System.out.println("Mona_Lisa is clicked: " + sensorID); text2d_message.setString("Mona_Lisa is clicked: "); text2d_message2.setString("");
break;
case "Dance_Class":
System.out.println("Dance_Class is clicked: " + sensorID); text2d_message2.setString("Dance_Class is clicked: "); text2d_message.setString("");
break;
}
} else {
System.out.println("" + sensorID); text2d_message.setString(""); text2d_message2.setString(""); }
sensorID = null;
}
public void mouseReleased(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }}