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.

Display.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Display.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. import java.util.Vector;
  17. import java.util.Enumeration;
  18. import java.awt.Graphics;
  19. import java.awt.Canvas;
  20. import java.awt.Image;
  21. /**
  22. * The display aspects capture the look and feel of the Game in modular
  23. * pluggable units.
  24. *
  25. * The model is that constructing a concrete subclass of Display attaches that
  26. * kind of display to the game. It will Display the game as it goes along.
  27. * A game can have any number of displays. Any of the displays will accept
  28. * keyboard input.
  29. *
  30. */
  31. class Display extends Canvas {
  32. private static Vector DISPLAYS = new Vector(2);
  33. private static Vector PLAYERS = new Vector(2);
  34. private static Pilot pilot1, pilot2;
  35. Game game;
  36. SWFrame frame;
  37. Image offImage;
  38. Graphics offGraphics;
  39. Game getGame() { return game; }
  40. static Pilot getPilot1() { return pilot1; }
  41. static Pilot getPilot2() { return pilot2; }
  42. Display(Game g) {
  43. super();
  44. game = g;
  45. frame = new SWFrame(game, this);
  46. DISPLAYS.addElement(this);
  47. }
  48. void noticeSizeChange() {
  49. initializeOffImage();
  50. }
  51. private void initializeOffImage () {
  52. int w = getSize().width;
  53. int h = getSize().height;
  54. if ( w > 0 & h > 0) {
  55. offImage = createImage(w, h);
  56. offGraphics = offImage.getGraphics();
  57. }
  58. }
  59. /*
  60. * In our double buffering scheme, painting just means copying the buffer
  61. * to the screen. The Display aspect draws into the buffer.
  62. */
  63. public void paint(Graphics g) {
  64. if (offImage != null)
  65. g.drawImage(offImage, 0, 0, null);
  66. }
  67. public void update(Graphics g) {
  68. /*
  69. * There are 4 steps to this:
  70. * - clear the double buffer
  71. * - paint the objects into the double buffer
  72. * - paint the status into the double buffer
  73. * - paint the doublebuffer into the buffer
  74. */
  75. offGraphics.setColor(getBackground());
  76. offGraphics.fillRect(0, 0, getBounds().width, getBounds().height);
  77. paintObjects(offGraphics);
  78. paintStatus(offGraphics);
  79. g.drawImage(offImage, 0, 0, null);
  80. }
  81. void paintObjects(Graphics g) { }
  82. void paintStatus(Graphics g) {}
  83. static aspect DisplayAspect {
  84. after (String mode) returning (Game game): call(Game+.new(String)) && args(mode) {
  85. new Display1(game);
  86. new Display2(game);
  87. if ( mode.equals("1") ) {
  88. pilot1 = game.newPlayer(1);
  89. }
  90. else if ( mode.equals("2") ) {
  91. pilot1 = game.newPlayer(1);
  92. pilot2 = game.newPlayer(2);
  93. }
  94. else if (mode. equals("demo")) {
  95. pilot1 = game.newRobot(1);
  96. pilot2 = game.newRobot(2);
  97. } else {
  98. game.error("Invalid mode: " + mode);
  99. game.quit();
  100. }
  101. }
  102. /*
  103. * I'm not really sure this belongs here.
  104. *
  105. * Being here what it does is makes the Display aspect
  106. * responsible for having the Players couple up to it. That's
  107. * kind of nice, but its a bit incomplete, since Player is
  108. * really part of the GUI, not part of the core Game.
  109. *
  110. * In a future re-factoring this will get worked out better.
  111. * What will happen is that GUI will be an aspect that has the
  112. * core GUI. Each of the different kinds of displays will be
  113. * aspects that tie themselves in.
  114. */
  115. after () returning (Player player): call(Player+.new(..)) {
  116. Enumeration elements = DISPLAYS.elements();
  117. while ( elements.hasMoreElements() ) {
  118. Display display = (Display)elements.nextElement();
  119. display.addKeyListener(player);
  120. }
  121. }
  122. after() returning (Display display): call(Display+.new(..)) {
  123. display.noticeSizeChange();
  124. }
  125. after(Display display) returning (): call(void setSize(..)) && target(display) {
  126. display.noticeSizeChange();
  127. }
  128. after() returning : call(void Game.clockTick()) {
  129. Enumeration elements = DISPLAYS.elements();
  130. while ( elements.hasMoreElements() ) {
  131. Display display = (Display)elements.nextElement();
  132. display.repaint();
  133. }
  134. }
  135. }
  136. }