// NavigateTour.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 : // - We query the Viewpoints Group for its viewpoints // - We use arrays to store things (hey i am learning Java) // - We organize a short tour thru the scene // 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 vrml.external.field.*; import vrml.external.Node; import vrml.external.Browser; import vrml.external.exception.*; public class NavigateTour extends Applet { // The Browser Browser browser = null; // Various UI widgets Button startTourButton; Label tourStatus; // 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 for (int count = 0; count < 10; count++) { browser = Browser.getBrowser (this); if (browser != null) break; try {Thread.sleep (200);} catch (InterruptedException ignored) {} System.out.println ("browser was null, trying again"); } if (browser == null) { throw new Error ("Failed to get the browser after 10 tries!"); } System.out.println("Got the browser: " + browser); // Paint tour button add (startTourButton = new Button ("Start Tour")) ; add (tourStatus = new Label (" (OFF) ")) ; // Misc other UI setup Color c = Color.white; System.out.println("Setting bg color to: " + c); setBackground(c); System.out.println("Navigate.init() done."); // Initialize some VRML stuff and paint buttons initScene(); } /** Start the applet running */ public void start(){ System.out.println("Navigate Applet: start()..."); } /** 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 Viewpoint 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; } } /** Organise a tour thru the various viewpoints */ public void doVPTour () { //hmm don't know if this is the right (easy) way to do it tourStatus.setText("Touring.."); for (int i=0; i < nVPs ; i++) { set_VPList[i].setValue(true); // wait for 2000 ms try {Thread.sleep (2000);} catch (InterruptedException ignored) {} } set_VPList[0].setValue(true); tourStatus.setText(" (OFF) "); } /** Handle actions from AWT widgets */ public boolean action(Event event, Object what) { if (event.target instanceof Button) { Button b = (Button) event.target; if (b == startTourButton) { doVPTour(); } else { for (int i=0; i < nVPs ; i++) { if (b == VPButtons[i]) { // get the corresponding VP EventInSFBool set_bindVP = set_VPList[i]; set_bindVP.setValue(true); System.out.println("action(): set_bound " + set_bindVP); } } } // end else } // end button handling return true; } }