// NavigateDyn.java: Applet that shows how to retrieve viewpoints // // Daniel.Schneider@tecfa.unige.ch 1/98 // Freeware if you give a line of credit :) // // Changes in relation to Navigate.java in this directory // We query the Viewpoints Group for its viewpoints // We use arrays to store things // inspired by: http://www.tomco.net/~raf/faqs/TestExternal4.html // (TestExternal4 applet by Ross A. Finlayson 1997) // Things have changed since the Late Nite Book ! // See the initScene method // node.getExposedField("children") -> .... // MFNode -> EventOutMFNode import java.awt.*; import java.applet.*; import java.awt.event.*; import vrml.external.field.*; import vrml.external.Node; import vrml.external.Browser; import vrml.external.exception.*; public class NavigateDyn extends Applet { // The Browser Browser browser = null; // Various stuff in the VRML scene we hang on to // array for the various View Points found Node[] VPList ; // array for the handles to the set_bind EventIn Fields EventInSFBool[] set_VPList ; // array with the buttons generated Button[] VPButtons ; // The number of Viewpoints found in the scene int nVPs = 0; // "Aborted" state (used to disable some UI or code // if we've already had a fatal error) boolean aborted = false; /** Initialize the Applet */ public void init() { // Connect to the browser browser = Browser.getBrowser (this); // Misc other UI setup Color c = Color.white; System.out.println("Setting bg color to: " + c); setBackground(c); System.out.println("NavigateDyn.init() done."); } /** Start the applet running */ public void start(){ System.out.println("Navigate Applet: start()..."); // Initialize some VRML stuff and paint buttons initScene(); } /** Stop the applet */ public void stop(){ System.out.println("Navigate Applet: stop()..."); } /** This applet is about to be unloaded. Do final cleanup. */ public void destroy() { System.out.println("Navigate Applet: destroy()..."); } /** Handle a fatal error condition */ public void die(String s) { System.out.println("Navigate Applet: FATAL ERROR!"); System.out.println("--> " + s); System.out.println("Navigate Applet: Aborting...\n"); aborted = true; // Causes update() to display a warning } /** Set up some stuff in the VRML scene */ public void initScene() { System.out.println("initScene()..."); int i; // Get the ViewPoints try { // Get the Group node called Viewpoints Node Viewpoints = browser.getNode("Viewpoints"); // Extract the list of children VPList = ((EventOutMFNode) (Viewpoints.getEventOut("children_changed"))).getValue(); // Initalize arrays for storing handles and Button strings nVPs = VPList.length; set_VPList = new EventInSFBool[nVPs]; VPButtons = new Button[nVPs]; for (i=0; i < nVPs ; i++) { // Get the handles for the Viewpoints set_bind Event fields System.out.println ("initScene: node '" + VPList[i] + "')..."); set_VPList[i] = (EventInSFBool)VPList[i].getEventIn("set_bind"); System.out.println ("initScene: got the EventIn: " + set_VPList[i]); // Insert push buttons into the Java applet String buttonLabel = "VP-" + i; add (VPButtons[i] = new Button(buttonLabel)); System.out.println ("initScene: Generated Button + string: " + VPButtons[i]); } } catch(InvalidNodeException e){ System.out.println("InvalidNodeException: " + e); die("initScene: VP node not found!"); return; } // or should I move this into the inner loop ? catch(InvalidEventInException e) { die("bindToViewpoint: InvalidEventInException: " + e); return; } catch(Exception e) { die("Strange exception: " + e); return; } } /** Handle actions from AWT widgets */ public void actionPerformed (ActionEvent event) { Object button = event.getSource(); for (int i=0; i < nVPs ; i++) { if (button == VPButtons[i]) { // get the corresponding VP EventInSFBool set_bindVP = set_VPList[i]; set_bindVP.setValue(true); System.out.println("action(): set_bound " + set_bindVP); } } } }