// GetNodeInfo.java: Applet to control the Get Node Info demo // inspired by the descent applet ../descent/* // Daniel.Schneider@tecfa.unige.ch 2/98 // hmm actually this applet doesn't do anything of interest besides // demoing how to to retrieve information from a VRML node. 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 GetNodeInfo extends Applet implements ActionListener { // The Browser Browser browser = null; // Various UI widgets Button getInfoButton; TextArea output = null; // Various stuff in the VRML scene we hang on to Node positionAvatar = null; EventOutSFVec3f positionCoords = null; EventOutSFRotation orientationCoords = null; // Variables to store current position and orientation float[] currentPosition; float[] currentOrientation; // "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); if (browser == null) { die("init: NULL browser!"); } System.out.println("Got the browser: " + browser); // Add the output pane and the button to the Java Panel output = new TextArea("Hello EAI apprentice ...\n", 5, 60); add(output); add (getInfoButton = new Button("Where Am I ?")); // register the button for event handling getInfoButton.addActionListener(this); // Show the real estate of the applet Color c = Color.white; setBackground(c); // Initialize some VRML stuff initScene(); System.out.println("GetNodeInfo.init() done."); } /** Start the applet running */ public void start(){ System.out.println("GetNodeInfo Applet: start()..."); } /** Stop the applet */ public void stop(){ System.out.println("GetNodeInfo Applet: stop()..."); } /** This applet is about to be unloaded. Do final cleanup. */ public void destroy() { System.out.println("GetNodeInfo Applet: destroy()..."); } /** Handle a fatal error condition */ public void die(String s) { System.out.println("GetNodeInfo Applet: FATAL ERROR!"); System.out.println("--> " + s); System.out.println("GetNodeInfo Applet: Aborting...\n"); aborted = true; // Causes a warning in the action method } /** Get reference to stuff in the VRML scene */ public void initScene() { System.out.println("initScene()..."); // Get the ViewPoints try { positionAvatar = browser.getNode("Spy"); System.out.println("ProxminitySensor Node " + positionAvatar + " found."); positionCoords = (EventOutSFVec3f) positionAvatar.getEventOut("position_changed"); orientationCoords = (EventOutSFRotation) positionAvatar.getEventOut("orientation_changed"); } catch(InvalidNodeException e){ System.out.println("InvalidNodeException: " + e); die("initScene: \"Spy\" node not found!"); return; } catch (InvalidEventOutException ee) { System.out.println("InvalidEventOutException: " + ee); die("initScene: EventOut Field not found!"); return; } } /** Handle actions from AWT widgets */ public void actionPerformed (ActionEvent event) { // System.out.println("K was here\n"); if (aborted == true) { output.append("Something is badly wrong, check the Java Console"); return; } Object clickedButton = event.getSource(); if (clickedButton == getInfoButton) { currentPosition = positionCoords.getValue(); String outString = "Current Position is at" + " x=" + currentPosition [0] + " y=" + currentPosition [1] + " z=" + currentPosition [2] + "\n"; // System.out.println(outString); output.append(outString); currentOrientation = orientationCoords.getValue(); output.append("Current Orientation is" + " x=" + currentOrientation [0] + " y=" + currentOrientation [1] + " z=" + currentOrientation [2] + " angle=" + currentOrientation [3] + "\n"); } } }