// Example applet illustrating sending/receiving of events and registering // of callbacks on EventOuts. import java.awt.*; import java.applet.*; import vrml.external.field.EventOut; import vrml.external.field.EventInSFColor; import vrml.external.field.EventOutSFColor; import vrml.external.field.EventOutSFTime; import vrml.external.field.EventOutObserver; import vrml.external.Node; import vrml.external.Browser; import vrml.external.exception.*; import netscape.javascript.JSObject; public class RGBTest extends Applet implements EventOutObserver { TextArea output = null; Browser browser = null; Node material = null; EventInSFColor diffuseColor = null; EventOutSFColor outputColor = null; EventOutSFTime touchTime = null; boolean error = false; public void init() { add(new Button("Red")); add(new Button("Green")); add(new Button("Blue")); output = new TextArea(5, 40); add(output); JSObject win = JSObject.getWindow(this); JSObject doc = (JSObject) win.getMember("document"); JSObject embeds = (JSObject) doc.getMember("embeds"); browser = (Browser) embeds.getSlot(0); // Now we've got the handle to the VRML Browser. try { // Get the material node... material = browser.getNode("MAT"); // Get the diffuseColor EventIn diffuseColor = (EventInSFColor) material.getEventIn("set_diffuseColor"); // Get the Touch Sensor Node sensor = browser.getNode("TOUCH"); // Get its touchTime EventOut touchTime = (EventOutSFTime) sensor.getEventOut("touchTime"); // Set up the callback touchTime.advise(this, new Integer(1)); // Get its diffuseColor EventOut outputColor = (EventOutSFColor) material.getEventOut("diffuseColor"); // Set up its callback outputColor.advise(this, new Integer(2)); } 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; } } public void callback(EventOut who, double when, Object which) { Integer whichNum = (Integer) which; if (whichNum.intValue() == 1) { // Make the timestamp and new time show up in the textarea. double val = touchTime.getValue(); output.appendText("Got time " + val + " at time " + when + "\n"); } if (whichNum.intValue() == 2) { // Make the new color of the sphere and timestamp // show up in the textarea. float[] val = outputColor.getValue(); output.appendText("Got color " + val[0] + ", " + val[1] + ", " + val[2] + " at time " + when + "\n"); } } public boolean action(Event event, Object what) { if (error) { showStatus("Problems! Had an error during initialization"); return true; // Uh oh... } if (event.target instanceof Button) { Button b = (Button) event.target; if (b.getLabel() == "Red") { showStatus ("Red!"); float[] val = new float[3]; val[0] = 0.8f; val[1] = 0.2f; val[2] = 0.2f; diffuseColor.setValue(val); } else if (b.getLabel() == "Green") { showStatus("Green!"); float[] val = new float[3]; val[0] = 0.2f; val[1] = 0.8f; val[2] = 0.2f; diffuseColor.setValue(val); } else if (b.getLabel() == "Blue") { showStatus("Blue!"); float[] val = new float[3]; val[0] = 0.2f; val[1] = 0.2f; val[2] = 0.8f; diffuseColor.setValue(val); } } return true; } }