package demo.helicopter; import java.awt.*; import java.awt.event.*; import java.text.*; // for class DecimalFormat import java.util.*; // for class Vector import mil.navy.nps.dis.*; import mil.navy.nps.testing.*; import mil.navy.nps.util.*; import mil.navy.nps.disEnumerations.*; public class RedStartPanel extends Frame implements WindowListener, ActionListener, ItemListener{ private BehaviorStreamBufferUDP behaviorStreamBuffer; private boolean eagleHeard = false; private boolean falconHeard = false; private boolean shrikeHeard = false; private boolean blasterHeard = false; private boolean crusherHeard = false; private boolean renegadeHeard = false; private boolean redNancyHeard = false; private boolean agentDriven = false; private boolean selection = false; private static int windowWidth; private static int windowHeight; private static int multicastFieldHash = 0; private static int portFieldHash = 0; private static int startHash = 0; private static int rejoinHash = 0; private static int exitHash = 0; private static int playerHash = 0; private static int siteHash = 0; private static int applicationHash = 0; private static String multiAddr; private static int portNum; private static int timeToLive; private static boolean rtpHeaderEnabled; private static short siteNum; private static short appNum; private static short playerId; private static Color panelColor; private static java.awt.List playerNames; private static java.awt.Checkbox agentCheck; static String marking; // 11 characters max //============================================================================================== // Constructor //============================================================================================== public RedStartPanel(int wdt, int ht) { setSize(wdt, ht); // set the main window dimensions setTitle("Stand by while listening for other players..."); // title the main window panelColor = new Color(255, 60, 60); setBackground(panelColor); // set main window background color setMultiAddr("224.2.181.145"); setPortNum(62040); setSiteNum((short)0); setAppNum((short)1); setPlayerId((short)22); // defaults setMarking("Eagle 22"); eagleHeard = false; falconHeard = false; shrikeHeard = false; blasterHeard = false; crusherHeard = false; renegadeHeard = false; redNancyHeard = false; selection = false; Toolkit tk = Toolkit.getDefaultToolkit(); // retrieve the dimesions of the Dimension dim = tk.getScreenSize(); // local host computer screen setLocation((dim.width - wdt)/2 , (dim.height - ht)/2);// locate main window in center of setWindowWidth(wdt); setWindowHeight(ht); addWindowListener(this); // for handling window close events setLayout(new GridLayout(2, 2)); behaviorStreamBuffer = new BehaviorStreamBufferUDP (multiAddr, portNum); Thread pduReader = new Thread(behaviorStreamBuffer); pduReader.start(); for (int sleepCount = 0; sleepCount < StartPanel.multicastListenDelay; sleepCount++) { try { Thread.sleep( 1000 ); // ms } catch (InterruptedException ie) { System.out.println ("fitful sleep: " + ie); } if (sleepCount == 0) System.out.print("Listening " + StartPanel.multicastListenDelay + " seconds for other vehicles"); System.out.print("."); java.util.Vector pdus = behaviorStreamBuffer.receivedPdus(); for (Enumeration enum = pdus.elements(); enum.hasMoreElements();) { ProtocolDataUnit tempPdu = (ProtocolDataUnit) enum.nextElement(); UnsignedByte pduType = tempPdu.getPduType(); if (pduType.shortValue() == PduTypeField.ENTITYSTATE) { EntityStatePdu tempEntityPdu = (EntityStatePdu) tempPdu; if (tempEntityPdu.getEntityID().getEntityID().intValue() == 20) { redNancyHeard = true; } // end if if (tempEntityPdu.getEntityID().getEntityID().intValue() == 22) { eagleHeard = true; } if (tempEntityPdu.getEntityID().getEntityID().intValue() == 23) { falconHeard = true; } if (tempEntityPdu.getEntityID().getEntityID().intValue() == 24) { shrikeHeard = true; } if (tempEntityPdu.getEntityID().getEntityID().intValue() == 27) { blasterHeard = true; } if (tempEntityPdu.getEntityID().getEntityID().intValue() == 28) { crusherHeard = true; } if (tempEntityPdu.getEntityID().getEntityID().intValue() == 29) { renegadeHeard = true; } } } } behaviorStreamBuffer.shutdown (); setTitle("Please select your Red team player identification and agent status:"); // title the main window buildPlayerPanel(); // goes into grid position (1, 1) buildButtonPanel(); // goes into grid position (1, 2) buildNetworkInfoPanel(); // goes into grid position (2, 1) buildIdInfoPanel(); // goes into grid position (2, 2) } // end constructor //============================================================================================== // Properties //============================================================================================== public void setWindowWidth(int wdt) {windowWidth = wdt;} public int getWindowWidth() {return windowWidth;} public void setWindowHeight(int ht) {windowHeight = ht;} public int getWindowHeight() {return windowHeight;} public void setMultiAddr(String addr) {multiAddr = addr;} public String getMultiAddr() {return multiAddr;} public void setPortNum(int setValue) {portNum = setValue;} public int getPortNum() {return portNum;} public void setSiteNum(short setValue) {siteNum = setValue;} public short getSiteNum() {return siteNum;} public void setAppNum(short setValue) {appNum = setValue;} public short getAppNum() {return appNum;} public void setPlayerId(short setValue) {playerId = setValue;} public short getPlayerId() {return playerId;} public void setMarking (String setValue) {marking = setValue;} public String getMarking () {return marking;} public void setTimeToLive (int ttl) {timeToLive = ttl;} public int getTimeToLive () {return timeToLive;} public void setRtpHeaderEnabled (boolean RtpMatch) {rtpHeaderEnabled = RtpMatch;} public boolean getRtpHeaderEnabled () {return rtpHeaderEnabled;} public void setAgentDriven (boolean isAgent) {agentDriven = isAgent;} //***************************** public boolean getAgentDriven () {return agentDriven;} //***************************** //============================================================================================= // Implements WindowListener //============================================================================================= public void windowClosing(WindowEvent evt){ // allows the close option on the if(evt.getSource().equals(this)){ // file menu to function properly System.exit(0); // as well as the X button } // end if } // end windowDeactivated public void windowClosed(WindowEvent e) {} // completes the implementation of public void windowIconified(WindowEvent e) {} // WindowListener in 'adapter' public void windowOpened(WindowEvent e) {} // fashion public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} //============================================================================================== // Panel Builders //============================================================================================== public void buildNetworkInfoPanel(){ Panel networkInfoPanel = new Panel(); // build the panel networkInfoPanel.setBackground(panelColor); // set panel color GridBagLayout gbl = new GridBagLayout(); // instantiate layout mgr. networkInfoPanel.setLayout(gbl); // set panel layout manager Label portNumber = new Label("Multicast Port Number"); // build textfield labels Label multiAddress = new Label("Multicast Address"); // and set text portNumber.setFont(new Font("SansSerif", Font.BOLD, 16)); multiAddress.setFont(new Font("SansSerif", Font.BOLD, 16)); portNumber.setForeground (new Color (180, 180, 180)); multiAddress.setForeground (new Color (180, 180, 180)); TextField multicastField = new TextField(multiAddr, 20); // build textFields multicastField.setBackground (new Color (200, 200, 100)); multicastField.setFont(new Font("SansSerif", Font.PLAIN, 14)); TextField portField = new TextField((new Integer(portNum)).toString(), 20); portField.setBackground (new Color (200, 200, 100)); portField.setFont(new Font("SansSerif", Font.PLAIN, 14)); portField.setColumns(20); multicastFieldHash = multicastField.hashCode(); // grab and save hash codes portFieldHash = portField.hashCode(); // for the textfields GridBagConstraints gbc = new GridBagConstraints(); // set up the gbc.fill = GridBagConstraints.NONE; // gridbagConstraints gbc.weighty = 1; addComponent(networkInfoPanel, multiAddress, gbl, gbc, 0, 0); // add components built addComponent(networkInfoPanel, multicastField, gbl, gbc, 1, 0);// above to the network addComponent(networkInfoPanel, portNumber, gbl, gbc, 2, 0); // panel addComponent(networkInfoPanel, portField, gbl, gbc, 3, 0); add(networkInfoPanel); // add the panel to the } // end buildNetworkInfoPanel() // frame public void buildPlayerPanel(){ Panel playerPanel = new Panel(); // build the panel playerPanel.setBackground(panelColor); // set panel color GridBagLayout gbl = new GridBagLayout(); // instantiate layout mgr. playerPanel.setLayout(gbl); // set the layout manager Label blank = new Label(" "); blank.setFont(new Font("Monospaced", Font.PLAIN, 8)); Label playerId = new Label("Player Entity Identification"); // build label and set font playerId.setFont(new Font("SansSerif", Font.BOLD, 16)); playerId.setForeground (new Color (255, 255, 255)); playerId.setAlignment (Label.CENTER); playerNames = new java.awt.List(15, false); // 15 rows, no multiple selections playerNames.setBackground (new Color (240, 240, 160)); playerNames.setFont(new Font("Monospaced", Font.PLAIN, 14)); if (!eagleHeard) { playerNames.add(" Eagle helo 22 " , 0); playerNames.select (0); } else playerNames.add("[Eagle helo 22] active", 0); if (!falconHeard) { playerNames.add(" Falcon helo 23 " , 1); if (playerNames.getSelectedIndex() == -1) playerNames.select (1); } else playerNames.add("[Falcon helo 23] active", 1); if (!shrikeHeard) { playerNames.add(" Shrike helo 24 " , 2); if (playerNames.getSelectedIndex() == -1) playerNames.select (2); } else playerNames.add("[Shrike helo 24] active", 2); if (!blasterHeard) { playerNames.add(" Blaster tank 27 " , 3); if (playerNames.getSelectedIndex() == -1) playerNames.select (3); } else playerNames.add("[Blaster tank 27] active", 3); if (!crusherHeard) { playerNames.add(" Crusher tank 28 " , 4); if (playerNames.getSelectedIndex() == -1) playerNames.select (4); } else playerNames.add("[Crusher tank 28] active", 4); if (!renegadeHeard) { playerNames.add(" Renegade tank 29 " , 5); if (playerNames.getSelectedIndex() == -1) playerNames.select (5); } else playerNames.add("[Renegade tank 29] active", 5); if (!redNancyHeard) { playerNames.add(" RedNancy Human 20 " , 6); if (playerNames.getSelectedIndex() == -1) playerNames.select (6); } else playerNames.add("[RedNancy Human 20] active", 6); playerHash = playerNames.hashCode(); // retrive hashcode playerNames.addItemListener( (ItemListener) this); // frame listens to list GridBagConstraints gbc = new GridBagConstraints(); // set up the gridbagConstraints gbc.fill = GridBagConstraints.BOTH; gbc.insets = new Insets (5, 5, 5, 5); gbc.weighty = 1; addComponent(playerPanel, blank, gbl, gbc, 0, 0); // to the panel addComponent(playerPanel, playerId, gbl, gbc, 1, 0); // add the components built above addComponent(playerPanel, playerNames, gbl, gbc, 2, 0); add(playerPanel); // add the panel to the } // end buildPlayerPanel() // frame public void buildButtonPanel(){ Panel buttonPanel = new Panel(); // create the panel buttonPanel.setBackground(panelColor); // set the color GridBagLayout gbl = new GridBagLayout(); // instantiate layout mgr. buttonPanel.setLayout(gbl); // set the layout manager agentCheck = new Checkbox("Agent Driven", agentDriven); agentCheck.setForeground(new Color (180, 180, 180)); agentCheck.setFont(new Font("SansSerif", Font.BOLD, 16)); Button start = new Button(" Start "); // create the buttons Button rejoin = new Button(" Rejoin "); Button exit = new Button(" Exit "); start.setFont(new Font("SansSerif", Font.BOLD, 14)); start.setForeground (new Color (255, 0, 0)); rejoin.setFont(new Font("SansSerif", Font.BOLD, 14)); rejoin.setForeground (new Color (0, 0, 255)); exit.setFont(new Font("SansSerif", Font.BOLD, 14)); exit.setForeground (new Color (0, 0, 0)); agentCheck.addItemListener(this); start.addActionListener(this); // main frame listens for rejoin.addActionListener(this); // the button presses exit.addActionListener(this); // the button presses startHash = start.hashCode(); // save the hashcode rejoinHash = rejoin.hashCode(); exitHash = exit.hashCode(); GridBagConstraints gbc = new GridBagConstraints(); // set up the gbc.fill = GridBagConstraints.NONE; // gridbagConstraints gbc.weighty = 1; addComponent(buttonPanel, agentCheck, gbl, gbc, 0, 0); addComponent(buttonPanel, start, gbl, gbc, 1, 0); // add the components built addComponent(buttonPanel, rejoin, gbl, gbc, 2, 0); // above to the panel addComponent(buttonPanel, exit, gbl, gbc, 3, 0); add(buttonPanel); // add the panel to the frame } // end buildButtonPanel() public void buildIdInfoPanel(){ Panel idInfoPanel = new Panel(); // create the panel idInfoPanel.setBackground(panelColor); // set the color GridBagLayout gbl = new GridBagLayout(); // create layout mgr idInfoPanel.setLayout(gbl); // set layout mgr Label siteId = new Label("DIS Site ID"); siteId.setFont(new Font("SansSerif", Font.BOLD, 16)); siteId.setForeground (new Color (180, 180, 180)); Label applicationId = new Label("DIS Application ID"); applicationId.setFont(new Font("SansSerif", Font.BOLD, 16)); applicationId.setForeground (new Color (180, 180, 180)); TextField siteField = new TextField((new Integer(siteNum)).toString()); siteField.setBackground (new Color (200, 200, 100)); siteField.setFont(new Font("SansSerif", Font.PLAIN, 14)); TextField applicationField = new TextField((new Integer(appNum)).toString()); applicationField.setBackground (new Color (200, 200, 100)); applicationField.setFont(new Font("SansSerif", Font.PLAIN, 14)); siteHash = siteField.hashCode(); // retrieve and save the applicationHash = applicationField.hashCode(); // hashcodes siteField.addActionListener(this); // frame listens to site applicationField.addActionListener(this); // & application fields GridBagConstraints gbc = new GridBagConstraints(); // set up the gbc.fill = GridBagConstraints.NONE; // gridbagConstraints gbc.weighty = 1; addComponent(idInfoPanel, siteId, gbl, gbc, 0, 0); // add the components built addComponent(idInfoPanel, siteField, gbl, gbc, 1, 0); // above to the panel addComponent(idInfoPanel, applicationId, gbl, gbc, 2, 0); addComponent(idInfoPanel, applicationField, gbl, gbc, 3, 0); add(idInfoPanel); } // buildIdInfoPanel() //============================================================================================== // Utility Methods //============================================================================================== public void addComponent(Container container, Component component, GridBagLayout gbl, GridBagConstraints gbc, int columnNumber, int rowNumber){ container.add(component); gbc.gridx = rowNumber; gbc.gridy = columnNumber; gbc.anchor = GridBagConstraints.WEST; gbl.setConstraints(component, gbc); } // end addComponent() //============================================================================================= // Implements ActionListener //============================================================================================= public void actionPerformed(ActionEvent evt){ System.out.println("entering actionPerformed() w/args... " + "\n event = " + evt); int eventCode = evt.getSource().hashCode(); if (eventCode == startHash) { if (playerNames.getSelectedIndex() == -1) { System.out.println("Please select a vehicle before starting..."); return; } if (agentDriven) //************************* { if (playerId >= 22 && playerId <= 24) { setTitle("Switching to AgentHeloControlPanel..."); // title the main window AgentHeloControlPanel ahcp = new AgentHeloControlPanel(multiAddr, portNum, timeToLive, siteNum, appNum, playerId, marking, rtpHeaderEnabled); this.setVisible(false); ahcp.show(); } // end if if (playerId >= 27 && playerId <= 29) { setTitle("Switching to AgentTankControlPanel..."); // title the main window AgentTankControlPanel atcp = new AgentTankControlPanel(multiAddr, portNum, timeToLive, siteNum, appNum, playerId, marking, rtpHeaderEnabled); this.setVisible(false); atcp.show(); } else System.out.println("RedStartPanel playerId out of range!" ); dispose(); } else // not agentDriven { if ( playerId == 20 ) { setTitle("Switching to HumanControlPanel..."); // title the main window HumanControlPanel hucp = new HumanControlPanel(multiAddr, portNum, timeToLive, siteNum, appNum, playerId, marking, rtpHeaderEnabled); this.setVisible(false); hucp.show(); } // end if if (playerId >= 22 && playerId <= 24) { setTitle("Switching to HeloControlPanel..."); // title the main window HeloControlPanel hcp = new HeloControlPanel(multiAddr, portNum, timeToLive, siteNum, appNum, playerId, marking, rtpHeaderEnabled); this.setVisible(false); hcp.show(); } // end if if (playerId >= 27 && playerId <= 29) { setTitle("Switching to TankControlPanel..."); // title the main window TankControlPanel tcp = new TankControlPanel(multiAddr, portNum, timeToLive, siteNum, appNum, playerId, marking, rtpHeaderEnabled); this.setVisible(false); tcp.show(); } else System.out.println("RedStartPanel playerId out of range!" ); dispose(); } } if (eventCode == rejoinHash){ dispose(); StartPanel mainBob = new StartPanel(500, 100); mainBob.show(); } // end if if (eventCode == exitHash){ dispose(); System.exit(0); } // end if if (eventCode == siteHash){ String newText = evt.getActionCommand(); System.out.println("entering if check for eventCode == siteHash; newText = " + newText); Integer temp = new Integer(0); try { siteNum = (short)temp.parseInt(newText); } // end try catch (NumberFormatException nfe){ System.out.println("String entered in the Site Number field cannot be parsed to " + "to an integer, site number set to zero instead"); siteNum = 0; } // end catch } // end if if (eventCode == applicationHash){ String newText = evt.getActionCommand(); System.out.println("entering if check for eventCode == applicationHash; newText = " + newText); } // end if } // end actionPerformed //============================================================================================= // Implements ItemListener //============================================================================================= public void itemStateChanged(ItemEvent evt) { if (eagleHeard && playerNames.getSelectedIndex() == 0) { playerNames.deselect (0); return; } if (falconHeard && playerNames.getSelectedIndex() == 1) { playerNames.deselect (1); return; } if (shrikeHeard && playerNames.getSelectedIndex() == 2) { playerNames.deselect (2); return; } if (blasterHeard && playerNames.getSelectedIndex() == 3) { playerNames.deselect (3); return; } if (crusherHeard && playerNames.getSelectedIndex() == 4) { playerNames.deselect (4); return; } if (renegadeHeard && playerNames.getSelectedIndex() == 5) { playerNames.deselect (5); return; } if (redNancyHeard && playerNames.getSelectedIndex() == 6) { playerNames.deselect (6); return; } agentDriven = agentCheck.getState(); System.out.println("agentDriven = " + agentDriven); String newText = evt.getItem().toString(); selection = true; if (newText.equals("0")) {playerId = 22; marking = "Eagle 22";} else if (newText.equals("1")) {playerId = 23; marking = "Falcon 23";} else if (newText.equals("2")) {playerId = 24; marking = "Shrike 24";} else if (newText.equals("3")) {playerId = 27; marking = "Blaster 27";} else if (newText.equals("4")) {playerId = 28; marking = "Crusher 28";} else if (newText.equals("5")) {playerId = 29; marking = "Renegade 29";} else if (newText.equals("6")) {playerId = 20; marking = "RedNancy 20";} else System.out.println("RedStartPanel mismatched entity number; newText = " + newText); } // end itemStateChanged //============================================================================================== // Main //============================================================================================== public static void main(String[] args){ RedStartPanel joeBob = new RedStartPanel(650, 650); joeBob.show(); } // end main } // end class StartPanel