// A simple ball that grows and shrinks // Freeware (but please keep the reference to Tiny3D) // // Code inspired from: // Tiny3D -- simple example of an "authoring tool" in Java // using the VRML External Interface. // Kenneth B. Russell (kbrussel@media.mit.edu) import java.applet.*; import java.awt.*; import java.util.*; import vrml.external.field.*; import vrml.external.exception.*; import vrml.external.Node; import vrml.external.Browser; /** * @author AUTHORNAME Daniel.Schneider@tecfa.unige.ch * @version VERSIONDATA 0.1 **/ public class BallGrow extends Applet { // Browser we're using Browser browser; // Root of the scene graph (to which we add our clumps) Node root; // The set_scale field we want to work on EventInSFVec3f set_scale; // The set_translation field we want to work on EventInSFVec3f set_translation; // // Java Widgets // // One instance variable per slider, // to tell what controls have been used java.awt.Scrollbar scalex; java.awt.Scrollbar scaley; java.awt.Scrollbar scalez; java.awt.Scrollbar transx; java.awt.Scrollbar transy; java.awt.Scrollbar transz; // the reset Button Button resetButton; // output TextArea output = null; // here we also could add parameters to deal with scrollbar values // Params to reset the ball float[] initScale = new float[3]; float[] initTrans = new float[3]; public void init () { // // Build the user interface // setBackground(new Color(0xd9d9d9)); // OK this layout is very simple, but all I could figure out // in reasonable time :) // Default "as it comes" layout // add(new java.awt.Label("FeedBack Information:")); output = new TextArea(5, 40); add(output); add(resetButton = new Button("RESET")); // Initialize scale scrollbars and their labels // We use a sinple grid layout, i.e. all elements get the same size. // Not sure that I use the Scrollbar arguments in the right way (sorry) // Panel scalePanel = new Panel (); scalePanel.setLayout (new GridLayout (3,2)); scalePanel.add(new java.awt.Label("X-Scale: ")); scalePanel.add(scalex = new java.awt.Scrollbar(0, 10, 10, 1, 100)); scalePanel.add(new java.awt.Label("Y-Scale: ")); scalePanel.add(scaley = new java.awt.Scrollbar(0, 10, 10, 1, 100)); scalePanel.add(new java.awt.Label("Z-Scale: ")); scalePanel.add(scalez = new java.awt.Scrollbar(0, 10, 10, 1, 100)); // Initialize translation scrollbars and their labels // Panel transPanel = new Panel (); transPanel.setLayout (new GridLayout (3,2)); transPanel.add(new java.awt.Label("X-Translation: ")); transPanel.add(transx = new java.awt.Scrollbar(0, 10, 10, -100, 100)); transPanel.add(new java.awt.Label("Y-Translation: ")); transPanel.add(transy = new java.awt.Scrollbar(0, 10, 10, -100, 100)); transPanel.add(new java.awt.Label("Z-Translation: ")); transPanel.add(transz = new java.awt.Scrollbar(0, 10, 10, -100, 100)); // let's add the two panels to the applet (mother panel) add (scalePanel); add (transPanel); // // We use these parameters to reset the ball // initTrans[0] = 0.0f; initTrans[1] = 0.0f; initTrans[2] = 0.0f; initScale[0] = 1.0f; initScale[1] = 1.0f; initScale[2] = 1.0f; // // VRML Interface // // Get the browser browser = Browser.getBrowser(this); // Get handle to ROOT of the scene graph (which is a transform node) try { // get the handle for the root object and give some feedback root = browser.getNode("ROOT"); output.appendText("Got root :) \n"); // Get the reference to the set_scale event set_scale = (EventInSFVec3f) root.getEventIn("scale"); output.appendText("Got set_scale :) \n"); // Get the reference to the set_translation event set_translation = (EventInSFVec3f) root.getEventIn("translation"); output.appendText("Got set_translation :) \n"); } catch (InvalidNodeException e) { output.appendText("PROBLEMS!: " + e + "\n"); } catch (InvalidEventInException e) { output.appendText("PROBLEMS!: " + e + "\n"); } } // // Callback of Java Widgets (i.e. user input to the Java applet) // public boolean handleEvent (Event event) { if (event.target instanceof Scrollbar) { // // First we look after the scrollbars // Scrollbar bar = (Scrollbar) event.target; if ((bar == scalex) || (bar == scaley) || (bar == scalez)) { float[] val = new float[3]; // Center about origin val[0] = ((float) scalex.getValue()) / 10.0f; val[1] = ((float) scaley.getValue()) / 10.0f; val[2] = ((float) scalez.getValue()) / 10.0f; // write it out to the scene output.appendText("Set Scale Value:" + " x=" + val[0] + " y=" + val[1] + " z=" + val[2] + "\n"); set_scale.setValue(val); } else { if ((bar == transx) || (bar == transy) || (bar == transz)) { float[] val = new float[3]; // Center about origin val[0] = ((float) transx.getValue()) / 10.0f; val[1] = ((float) transy.getValue()) / 10.0f; val[2] = ((float) transz.getValue()) / 10.0f; // write it out to the scene output.appendText("Set Trans Value:" + " x=" + val[0] + " y=" + val[1] + " z=" + val[2] + "\n"); set_translation.setValue(val); }} return true; } else { // // The User want's to reset the scene // if (event.target instanceof Button) { Button b = (Button) event.target; // if (b.getLabel() == "RESET") ... i like the following better if (b == resetButton) { set_scale.setValue(initScale); set_translation.setValue(initTrans); } return true; } } // hmm not sure what this does return super.handleEvent(event); } }