You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SWFrame.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. SWFrame.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. import java.awt.Frame;
  17. import java.awt.Menu;
  18. import java.awt.MenuBar;
  19. import java.awt.MenuItem;
  20. import java.awt.MenuShortcut;
  21. import java.awt.Dimension;
  22. import java.awt.Insets;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.ActionEvent;
  25. class SWFrame extends Frame implements ActionListener {
  26. private Game game;
  27. private Display display;
  28. private Menu menu;
  29. Game getGame() { return game; }
  30. Display getDisplay() { return display; }
  31. Menu getMenu() { return menu; }
  32. SWFrame(Game theGame, Display d) {
  33. super("Space War!");
  34. game = theGame;
  35. display = d;
  36. add(display);
  37. // create menu
  38. menu = new Menu("Game");
  39. MenuItem item1 = new MenuItem("Add Robot", new MenuShortcut('a'));
  40. MenuItem item2 = new MenuItem("Reset Ships", new MenuShortcut('r'));
  41. MenuItem item3 = new MenuItem("Quit", new MenuShortcut('q'));
  42. item1.setActionCommand("Add Robot");
  43. item2.setActionCommand("Reset Ships");
  44. item3.setActionCommand("Quit");
  45. menu.add(item1);
  46. menu.add(item2);
  47. menu.add(item3);
  48. menu.addActionListener(this);
  49. setMenuBar(new MenuBar());
  50. getMenuBar().add(menu);
  51. Dimension screenSize = new Dimension(500, 500);
  52. setSize(screenSize);
  53. setVisible(true);
  54. toFront();
  55. Insets inset = getInsets();
  56. int displayWidth = screenSize.width - inset.left - inset.right;
  57. int displayHeight = screenSize.height - inset.top - inset.bottom;
  58. display.setSize(displayWidth, displayHeight);
  59. }
  60. public void actionPerformed(ActionEvent e) {
  61. String s = e.getActionCommand();
  62. if (s.equals("Add Robot")) {
  63. getGame().addRobot();
  64. }
  65. else if (s.equals("Reset Ships")) {
  66. getGame().resetShips();
  67. }
  68. else if (s.equals("Quit")) {
  69. getGame().quit();
  70. }
  71. }
  72. }