// ********************************************************************** // EXECUTIVE SUMMARY // Module Name: World.java // Description: Definition of the world applet // Author: Kent A. Watsen, http://www.mbay.net/~watsen // ********************************************************************** package mil.navy.nps.eaiDemoBoids; // ********************************************************************** // IMPORTS // ********************************************************************** // Standard classes import java.awt.*; import java.util.*; import java.applet.*; // EAI classes import vrml.external.Node; import vrml.external.Browser; import vrml.external.field.*; import vrml.external.exception.*; import netscape.javascript.JSObject; // NPS library classes import mil.navy.nps.dis.*; import mil.navy.nps.math.*; // ********************************************************************** // CLASS // ********************************************************************** public class World extends Applet implements EventOutObserver { public static final boolean DEBUG = true; private NIU niu = null; private Browser browser = null; private Node rootNode = null; private Node globalClock = null; private EventInMFNode addChildren = null; private EventInMFNode removeChildren = null; private EventOutSFFloat globalTick = null; private float frameNum = 0f; private TextField frameField = null; private Entity entity = null; private Ownship ownship = null; private Class espduClass = null; int pduCount = 0; public String getName() { return "World"; } public void init() { if(DEBUG) System.out.println("initializing world"); // Create the AWT stuff add(new Button("Add Ownship")); add(new Button("Remove Ownship")); add(new Label("Framerate = ")); frameField = new TextField("", 3); frameField.setEditable(false); add(frameField); // Get a handle to the NIU while (niu == null) { niu = (NIU)(getAppletContext().getApplet("NIU")); } if(DEBUG) System.out.println("niu name is maybe= " + niu.getName() ); // trick for getting a handle to the browser plugin JSObject win = JSObject.getWindow(this); JSObject doc = (JSObject) win.getMember("document"); JSObject embeds = (JSObject) doc.getMember("embeds"); browser = (Browser) embeds.getSlot(0); if(DEBUG) { if(win == null) System.out.println("unable to find window we reside in"); if(doc == null) System.out.println("unable to find document within window we reside in"); if(embeds == null) System.out.println("unable to find list of embedded elements within document"); if((win != null) && (doc != null) && (embeds != null)) System.out.println("Got all JS objects OK"); if(browser == null) { System.out.println("got null value for VRML browser object; unable to find root node"); } else System.out.println("Got a value for the VRML browser object."); } if(DEBUG) System.out.println("Attempting to get root node in vrml scene from VRML browser object"); // Get the VaderTransform and GlobalClock nodes... try { rootNode = browser.getNode("RootNode"); globalClock = browser.getNode("GlobalClock"); } catch (InvalidNodeException e) { System.out.println("Invalid Node Exception"); } if(DEBUG) System.out.println("Made it past root node discovery"); // Get EventIns of the rootNode addChildren = (EventInMFNode) rootNode.getEventIn("addChildren"); removeChildren = (EventInMFNode) rootNode.getEventIn("removeChildren"); // Get the EventOut for GlobalClock's tick globalTick = (EventOutSFFloat) globalClock.getEventOut("fraction_changed"); // Set up a callback for globalTick globalTick.advise(this, new Integer(1)); if(DEBUG) System.out.println("Looking up ESPDU class"); // get handle to espduClass try { espduClass = Class.forName("mil.navy.nps.dis.EntityStatePdu"); } catch (ClassNotFoundException e) { System.out.println("espduClass not found"); } System.out.println("constructor for World.java finished"); } public void callback(EventOut who, double when, Object which) { Integer whichNum = (Integer) which; Vector pduVector = null; ProtocolDataUnit pdu = null; EntityID entityID = null; EntityStatePdu espdu = null; // First set framerate field if (whichNum.intValue() == 1) { frameNum++; if (frameNum % 10 == 0) { frameField.setText(String.valueOf(browser.getCurrentFrameRate())); } // Pick off and set PDUs from NIU pduVector = niu.getPDUs(); for (int i=0; i0; i--) // backwards 'cause delete shifts list { entity = Entity.get(i-1); if (entity.getPduTime() > 10.0) // 10 sec timeout { removeChildren.setValue(entity.getNode()); entity.dispose(); } else { entity.cycle(); } } // Finally, let the Ownship update itself ownship.cycle(); } } public boolean handleEvent(Event event) { if (event.id == Event.KEY_ACTION) { switch (event.key) { case Event.UP: { ownship.update(Ownship.UP); break; } case Event.DOWN: { ownship.update(Ownship.DOWN); break; } case Event.LEFT: { ownship.update(Ownship.LEFT); break; } case Event.RIGHT: { ownship.update(Ownship.RIGHT); break; } } } else if (event.id == Event.KEY_PRESS) { switch (event.key) { case 'f': { ownship.update(Ownship.FASTER); break; } case 's': { ownship.update(Ownship.SLOWER); break; } } } return super.handleEvent(event); } public boolean action(Event event, Object what) { if (event.target instanceof Button) { Button b = (Button) event.target; if (b.getLabel() == "Add Ownship") { System.out.println("World: got ownship hit"); if (ownship == null) // can only have one! { System.out.println("World: creating new Ownship"); ownship = new Ownship(browser, niu, getParameter("ownshipEntityID")); System.out.println("World: got a new Ownship instantiated"); addChildren.setValue(ownship.getNode()); System.out.println("World: ownship added to addChildren"); } else System.out.println("World: can only add one ownship, no action taken"); } else if (b.getLabel() == "Remove Ownship") { System.out.println("World: remove Ownship"); if (ownship != null) { removeChildren.setValue(ownship.getNode()); ownship.dispose(); ownship = null; System.out.println("World: Ownship removed"); } else System.out.println("World: Ownship null, nothing to remove"); } } return true; } }