Appendix C.

advertisement
56
Appendix C: Smart Objects One Beam Example Java Code
/*
Name:
Author:
Date:
Description:
BeamSize.java
Dustin Eggink
11/28/00, noted 07/09/01
The wall and beam example in Smart Objects One.
*/
///////////////////////////////// JAVA IMPORTS /////////////////////
import java.awt.*;
import java.applet.*;
///////////////////////////////// VRML IMPORTS //////////////////////
import vrml.external.field.EventOut;
// import some VRML libraries which will
import vrml.external.field.EventOutObserver;
// allow the applet to observe the VRML world.
import vrml.external.Node;
import vrml.external.Browser;
import vrml.external.exception.*;
import vrml.external.field.EventOutSFVec3f;
import vrml.external.field.EventInSFVec3f;
////////////////////////////////////////////////////////////////////
public class BeamScale extends Applet implements EventOutObserver
////////////////////////////////////////////////////////////////////
{
TextArea output = null;
Browser browser = null;
// reference to the VRML browser
Node wallMove = null;
EventOutSFVec3f translation = null;
float[] coord = new float[3];
float x, z, y;
//
//
//
//
//
Node object0 = null;
EventInSFVec3f scale0 = null;
float[] scl0 = new float[3];
Node object1 = null;
EventInSFVec3f scale1 = null;
float[] scl1 = new float[3];
Node object2 = null;
EventInSFVec3f scale2 = null;
float[] scl2 = new float[3];
Node object3 = null;
EventInSFVec3f scale3 = null;
float[] scl3 = new float[3];
Node object4 = null;
EventInSFVec3f scale4 = null;
float[] scl4 = new float[3];
// this node is the object of the first beam
// send out three floats to VRML
// they will go into the "scl0" array
boolean error = false;
//////////////////////////// INIT ////////////////////////////////////////
public void init()
{
System.out.println("BeamScale.init()..");
output = new TextArea(5, 40);
this node is the translation of the wall
three floats from VRML come into S/O
they will go into the "coord" array
renaming for clarity in the "if, then" statements
below.
57
add(output);
}
///////////////////////////// START ///////////////////////////////////////
public void start()
{
System.out.println("BeamScale.start()...");
output.appendText("\nBeamScale starting....\n");
try
{
java.lang.Thread.sleep(3000);
}
catch(InterruptedException e){}
try
{
while (browser==null)
{
browser=(Browser)vrml.external.Browser.getBrowser(this);
System.out.println("Got the browser: " + browser);
}
Node wallMove = browser.getNode("MOVE"); // "MOVE" is defined in the VRML as the
output.appendText("Got node: MOVE\n");
// object's planesensor.
translation = (EventOutSFVec3f) wallMove.getEventOut("translation_changed");
output.appendText("Got EventOut : translation_changed\n");
translation.advise(this, new Integer(1));
object0 = browser.getNode("BEAM0");
// The "BEAM 0-4" nodes are defined within
scale0 = (EventInSFVec3f) object0.getEventIn("set_scale"); // the VRML file, within which,
object1 = browser.getNode("BEAM1");
// the scale value is contained.
scale1 = (EventInSFVec3f) object1.getEventIn("set_scale");
object2 = browser.getNode("BEAM2");
scale2 = (EventInSFVec3f) object2.getEventIn("set_scale");
object3 = browser.getNode("BEAM3");
scale3 = (EventInSFVec3f) object3.getEventIn("set_scale");
object4 = browser.getNode("BEAM4");
scale4 = (EventInSFVec3f) object4.getEventIn("set_scale");
}
// now a series of required exceptions for the VRML
catch (InvalidNodeException ne)
{
add(new TextField("Failed to get node:" + ne));
error = true;
}
catch (InvalidEventInException ee)
{
add(new TextField("Failed to get EventIn:" + ee));
error = true;
}
catch (InvalidEventOutException ee)
{
add(new TextField("Failed to get EventOut:" + ee));
error = true;
}
}
58
/////////////////////////////// CALLBACK /////////////////////////////////////
public void callback(EventOut who, double where, Object which)
{
// callback is the procedure through
// which VRML
// intereracts with the Java applet.
System.out.println("BeamScale: EAI callback()...");
Integer whichNum = (Integer) which;
if (whichNum.intValue() == 1)
{
coord = translation.getValue();
// the "coord" array is now set to equal the
// X, Y and Z translation (movement) of the wall
x = coord[0];
y = coord[1];
z = coord[2];
scaleChange0();
scaleChange1();
scaleChange2();
scaleChange3();
scaleChange4();
}
// once the variables x, y and z are set to equal
// the places in the coord array, the scaleChange
// functions (defined below) are performed.
// this whole process will loop as long as the
// user is dragging the wall around, making the
// intValue equal to 1.
/////////////////////////////// CONSTRAINT FUNCTIONS /////////////////////////////////////
public void scaleChange0 ()
{
if ( ( x < -2.7 ) )
// just a simple "if,then" statement.
// "if" the translation of the
{
// wall (x, or coord[0]) exceeds 2.7, "then" set the values
scl0[0] = 1.0f;
// of the array "scl0" to the listed values. the first
scl0[1] = 1.5f;
// position in the array will be the X value,then Y, Z.
scl0[2] = 1.3f;
scale0.setValue(scl0);
// here is where the magic happens, "scale0" specifically
}
// refers the VRML set_scale function of "object0", which
else
// is BEAM0 in the VRML file.
{
scl0[0] = 1.0f;
scl0[1] = 1.0f;
scl0[2] = 1.0f;
// "else" set it back to the default scale of 1.0
scale0.setValue(scl0);
}
}
public void scaleChange1 ()
{
if ( ( x < -2.2 ) )
// the values of the "if" statement in functions
{
// scaleChange0 and 4 differ from 1-3 because of the
scl1[0] = 1.0f;
// notches in the wall at each end.
scl1[1] = 1.5f;
scl1[2] = 1.3f;
scale1.setValue(scl1);
}
else { scl1[0] = 1.0f;
}
59
scl1[1] = 1.0f;
scl1[2] = 1.0f;
scale1.setValue(scl1); }
}
public void scaleChange2 ()
{
if ( ( x < -2.2 ) )
{
scl2[0] = 1.0f;
scl2[1] = 1.5f;
scl2[2] = 1.3f;
scale2.setValue(scl2);
}
else
{
scl2[0] = 1.0f;
scl2[1] = 1.0f;
scl2[2] = 1.0f;
scale2.setValue(scl2);
}
}
public void scaleChange3 ()
{
if ( ( x < -2.2 ) )
{
scl3[0] = 1.0f;
scl3[1] = 1.5f;
scl3[2] = 1.3f;
scale3.setValue(scl3);
}
else
{
scl3[0] = 1.0f;
scl3[1] = 1.0f;
scl3[2] = 1.0f;
scale3.setValue(scl3);
}
}
public void scaleChange4 ()
{
if ( ( x < -2.7 ) )
{
scl4[0] = 1.0f;
scl4[1] = 1.5f;
scl4[2] = 1.3f;
scale4.setValue(scl4);
}
else
{
scl4[0] = 1.0f;
scl4[1] = 1.0f;
scl4[2] = 1.0f;
scale4.setValue(scl4);
}
} }
/////////////////////////// END /////////////////////////////////////////.
Download