package demo.helicopter; import java.awt.*; import java.awt.event.*; import mil.navy.nps.dis.*; public class StartPanel extends Frame implements WindowListener, ActionListener { private static int windowWidth; private static int windowHeight; //public AntennaStartPanel asp; public NetworkPlanningTerminalFileReader asp; private static int exitHash = 0; private static int redHash = 0; private static int blueHash = 0; private static int antennaHash = 0; private static int timeToLive = 15; // default ttl convention is local campus private static int sleepTime = 0; private static boolean rtpMatch = false; private static int lastEventCode; // prevent multiple copies of a live entity by listening first, > 5 seconds recommended // minimum listen time 2 sec needed or else panels will allow repeats... public static final int multicastListenDelay = 8; public static final int reportedMulticastListenDelay = multicastListenDelay + 2; //============================================================================================== // Constructor //============================================================================================== public StartPanel(int wdt, int ht) { setSize(wdt, ht); // set the main window dimensions setTitle(" Welcome to Capture the Flag! Please pick your team."); // title the main window setBackground(new Color(46, 147, 70)); // set main window background color 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(1,3)); buildTeamPanel(); // goes into grid position (1, 1) } // 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;} //============================================================================================= // 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 buildTeamPanel(){ Panel teamPanel = new Panel(); // create the panel teamPanel.setBackground(Color.lightGray); // set the color GridBagLayout gbl = new GridBagLayout(); // instantiate layout mgr. teamPanel.setLayout(gbl); // set the layout manager Button red = new Button ("Red Team"); // create the buttons Button blue = new Button("Blue Team"); Button antenna = new Button("Antenna"); Button exit = new Button(" Exit "); // create the buttons red.addActionListener(this); // main frame listens for blue.addActionListener(this); // the button presses antenna.addActionListener(this); // main frame listens for exit.addActionListener(this); // the button presses red.setBackground(Color.white); red.setForeground (new Color (255, 0, 0)); blue.setBackground(Color.white); blue.setForeground (new Color (0, 0, 255)); antenna.setBackground(Color.white); antenna.setForeground (new Color (255, 174, 23)); exit.setBackground(Color.white); red.setFont(new Font("SansSerif", Font.BOLD, 16)); blue.setFont(new Font("SansSerif", Font.BOLD, 16)); antenna.setFont(new Font("SansSerif", Font.BOLD, 16)); exit.setFont(new Font("SansSerif", Font.BOLD, 16)); redHash = red.hashCode(); // save the hashcode blueHash = blue.hashCode(); antennaHash = antenna.hashCode(); exitHash = exit.hashCode(); GridBagConstraints gbc = new GridBagConstraints(); // set up the gbc.fill = GridBagConstraints.CENTER; // gridbagConstraints gbc.weightx = 1; addComponent(teamPanel, red, gbl, gbc, 0, 0); // add the components built addComponent(teamPanel, blue, gbl, gbc, 0, 1); // above to the panel addComponent(teamPanel, antenna, gbl, gbc, 0, 2); // above to the panel addComponent(teamPanel, exit, gbl, gbc, 0, 3); // above to the panel add(teamPanel); // add the panel to the } // end buildTeamPanel() // frame // dwl end //============================================================================================== // 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.CENTER; 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 == exitHash){ dispose(); System.exit(0); } // end if if (eventCode == redHash){ setTitle("* * * Stand by to join Red team (listen " + reportedMulticastListenDelay + " seconds for other players...) "); // title the main window repaint (); System.out.println("* * * Stand by to join Red team (listen " + reportedMulticastListenDelay + " seconds for other players...) "); RedStartPanel rsp = new RedStartPanel (800, 650); this.setVisible(false); rsp.show(); rsp.setTimeToLive (timeToLive); rsp.setRtpHeaderEnabled (rtpMatch); dispose(); } // end if if (eventCode == blueHash){ setTitle("* * * Stand by to join Blue team (listen " + reportedMulticastListenDelay + " seconds for other players...) "); // title the main window repaint (); System.out.println("* * * Stand by to join Blue team (listen " + reportedMulticastListenDelay + " seconds for other players...) "); BlueStartPanel bsp = new BlueStartPanel (800, 650); this.setVisible(false); bsp.show(); bsp.setTimeToLive (timeToLive); bsp.setRtpHeaderEnabled (rtpMatch); dispose(); } // end if if (eventCode == antennaHash){ setTitle("* * * Standby start Antenna Panel (listen " + reportedMulticastListenDelay + " seconds for other Antennas...) "); // title the main window repaint (); System.out.println("* * * Standby start Antenna Panel (listen " + reportedMulticastListenDelay + " seconds for other players...) "); asp = new NetworkPlanningTerminalFileReader (); // go and get the file reader //asp = new AntennaStartPanel (800, 650); // for the Antenna //this.setVisible(false); //asp.show(); //asp.setTimeToLive (timeToLive); //asp.setRtpHeaderEnabled (rtpMatch); dispose(); } // end if } // end actionPerformed //============================================================================================== // Main //============================================================================================== public static void main(String[] args){ boolean ttlMatch = false; boolean sleepMatch = false; for (int index = 0; index < args.length; index++) { if (!rtpMatch) try { rtpMatch = ((args[index].compareToIgnoreCase ( "rtp") == 0) || (args[index].compareToIgnoreCase ("-rtp") == 0)); if (rtpMatch) continue; } catch (java.lang.NoSuchMethodError nsme) // prior to jdk 1.2 { rtpMatch = ((args[index].compareTo ( "rtp") == 0) || (args[index].compareTo ("-rtp") == 0) || (args[index].compareTo ( "RTP") == 0) || (args[index].compareTo ("-RTP") == 0)); if (rtpMatch) continue; } if (!(ttlMatch) && (args.length - index > 1)) { try { ttlMatch = ((args[index].compareToIgnoreCase ( "ttl") == 0) || (args[index].compareToIgnoreCase ("-ttl") == 0) || (args[index].compareToIgnoreCase ( "timeToLive") == 0) || (args[index].compareToIgnoreCase ("-timeToLive") == 0)); } catch (java.lang.NoSuchMethodError nsme) // prior to jdk 1.2 { ttlMatch = ((args[index].compareTo ( "ttl") == 0) || (args[index].compareTo ("-ttl") == 0) || (args[index].compareTo ( "timeToLive") == 0) || (args[index].compareTo ("-timeToLive") == 0)); } if (ttlMatch) { try { timeToLive = Integer.parseInt(args[index+1]); index++; } catch(Exception e) { System.out.println(e); System.out.println("illegal timeToLive value, exiting"); System.exit (-1); } if ((timeToLive < 0) || (timeToLive > 127)) { System.out.println ("multicast timeToLive ttl=" + timeToLive + " is out of range [0..127], exiting"); System.exit (-1); } continue; } } if (!(sleepMatch) && (args.length - index > 1)) { try { sleepMatch = ((args[index].compareToIgnoreCase ( "pause") == 0) || (args[index].compareToIgnoreCase ("-pause") == 0) || (args[index].compareToIgnoreCase ( "sleep") == 0) || (args[index].compareToIgnoreCase ("-sleep") == 0) || (args[index].compareToIgnoreCase ( "wait") == 0) || (args[index].compareToIgnoreCase ("-wait") == 0)); } catch (java.lang.NoSuchMethodError nsme) // prior to jdk 1.2 { sleepMatch = ((args[index].compareTo ( "pause") == 0) || (args[index].compareTo ("-pause") == 0) || (args[index].compareTo ( "sleep") == 0) || (args[index].compareTo ("-sleep") == 0) || (args[index].compareTo ( "wait") == 0) || (args[index].compareTo ("-wait") == 0)); } if (sleepMatch) { try { sleepTime = Integer.parseInt(args[index+1]); index++; } catch(Exception e) { System.out.println(e); System.out.println("illegal sleepTime value, exiting"); System.exit (-1); } if ((sleepTime < 0) || (sleepTime > 60)) { System.out.println ("sleepTime=" + sleepTime + " is out of range (0..60 sec), ignored"); System.exit (-1); } else { for (int sleepCount = 0; sleepCount < sleepTime; sleepCount++) { try { Thread.sleep( 1000 ); // ms } catch (InterruptedException ie) { System.out.println ("fitful sleep: " + ie); } if (sleepCount == 0) System.out.print("Pause before starting"); System.out.print("."); } System.out.println(); } continue; } } else { System.out.println("Usage: java demo.helicopter.StartPanel [-ttl ] [-pause ] [-rtp] [-trace]"); System.exit (-1); } } System.out.println ("multicast timeToLive ttl=" + timeToLive); System.out.println ("RTP headers prepended=" + rtpMatch); // Makes new StartPanel with size of wide 530, long 100 StartPanel mainBob = new StartPanel(530, 100); mainBob.show(); } // end main } // end class StartPanel