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.

Display1.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. Display1.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. import java.awt.Graphics;
  17. import java.awt.Color;
  18. import java.util.Random;
  19. /**
  20. * This is the standard display aspect.
  21. */
  22. class Display1 extends Display {
  23. /*
  24. * Here's the color scheme for the game. No other places in this file
  25. * should say Color.xxx. Instead, that color should be given a symbolic
  26. * name here.
  27. */
  28. private static Color backgroundColor = Color.black;
  29. private static Color player1ShipColor = Color.white;
  30. private static Color player2ShipColor = Color.gray;
  31. private static Color robotShipColor = new Color(0xa00000);
  32. private static Color flameColor = Color.red;
  33. private static Color shipExplosionColor = Color.red;
  34. private static Color bulletColor = Color.green;
  35. private static Color energyPacketOuterColor = Color.blue;
  36. private static Color energyPacketInnerColor = new Color(0x7070FF);
  37. private static Color statusLabelsColor = Color.white;
  38. private static Color statusMeterBorderColor = Color.white;
  39. private static Color energyStatusMeterColor = Color.blue;
  40. private static Color damageStatusMeterColor = Color.red;
  41. Display1(Game game) {
  42. super(game);
  43. frame.setLocation(20, 20);
  44. }
  45. void noticeSizeChange() {
  46. super.noticeSizeChange();
  47. setBackground(backgroundColor);
  48. }
  49. void paintObjects(Graphics g) {
  50. SpaceObject[] objects = game.getRegistry().getObjects();
  51. final int len = objects.length;
  52. for (int i = 0; i < len; i++) {
  53. objects[i].paint(g);
  54. }
  55. }
  56. static aspect SpaceObjectPainting {
  57. abstract private void SpaceObject.paint(Graphics g);
  58. /*
  59. * Ships are by far and away the most complex of the space Objects
  60. * to paint. First off, we need to set the color when the ship
  61. * is made.
  62. */
  63. private Color Ship.color;
  64. after(Pilot pilot) returning (Ship ship): call(Ship Game.newShip(Pilot)) && args(pilot) {
  65. if (pilot.getNumber() == 1)
  66. ship.color = player1ShipColor;
  67. else if (pilot.getNumber() == 2)
  68. ship.color = player2ShipColor;
  69. else
  70. ship.color = robotShipColor;
  71. }
  72. private void Ship.paint(Graphics g) {
  73. final double PI = Math.PI;
  74. int[] radius = {15, 12, -4, 12, -9, -15, -9};
  75. double[] angle = {0, PI * 3/4, 0, -PI * 3/4, PI/8, 0, -PI/8};
  76. int[] x;
  77. int[] y;
  78. Random random = new Random();
  79. if (this.getDamage() >= this.MAX_DAMAGE) {
  80. int lines = 20;
  81. x = new int[lines];
  82. y = new int[lines];
  83. g.setColor(shipExplosionColor);
  84. for (int i = 0; i < lines; i++) {
  85. x[i] = (int)(this.getXPos()) + random.nextInt() % 20;
  86. y[i] = (int)(this.getYPos()) + random.nextInt() % 20;
  87. }
  88. for (int i = 0; i < lines; i++)
  89. g.drawLine(x[i], y[i], x[(i + 1) % lines], y[(i + 1) % lines]);
  90. } else {
  91. x = new int[7];
  92. y = new int[7];
  93. g.setColor(this.color);
  94. radius[5] += random.nextInt() % 3;
  95. // convert coordinates from polar to cartesian
  96. for (int i = 0; i < 7; i++) {
  97. x[i] = (int)
  98. (this.getXPos() +
  99. Math.cos(this.getOrientation() + angle[i]) * radius[i]);
  100. y[i] = (int)
  101. (this.getYPos() +
  102. Math.sin(this.getOrientation() + angle[i]) * radius[i]);
  103. }
  104. // draw the body as a polygon
  105. g.drawPolygon(x, y, 4);
  106. // if the ship is accelerating, draw in a flame
  107. if (this.getRAcc() != 0) {
  108. g.setColor(flameColor);
  109. g.drawLine(x[4], y[4], x[5], y[5]);
  110. g.drawLine(x[5], y[5], x[6], y[6]);
  111. }
  112. }
  113. }
  114. /*
  115. * Bullets
  116. */
  117. private void Bullet.paint(Graphics g) {
  118. g.setColor(bulletColor);
  119. g.fillOval((int)this.getXPos() - 1,
  120. (int)this.getYPos() - 1,
  121. 3,
  122. 3);
  123. }
  124. /*
  125. * energy packets
  126. */
  127. private void EnergyPacket.paint(Graphics g) {
  128. g.setColor(energyPacketOuterColor);
  129. g.fillOval((int)this.getXPos() - 5,
  130. (int)this.getYPos() - 5,
  131. 10, 10);
  132. g.setColor(energyPacketInnerColor);
  133. g.fillOval((int)this.getXPos() - 2,
  134. (int)this.getYPos() - 2,
  135. 3, 3);
  136. }
  137. }
  138. void paintStatus(Graphics g) {
  139. int left1 = 60;
  140. int top1 = 0;
  141. int left2 = 200;
  142. int top2 = 0;
  143. g.setColor(statusLabelsColor);
  144. g.drawString("energy:", 5, top1 + 15);
  145. g.drawString("damage:", 5, top1 + 30);
  146. if (getPilot1() != null)
  147. paintLevels(g, getPilot1().getShip(), top1, left1);
  148. if (getPilot2() != null)
  149. paintLevels(g, getPilot2().getShip(), top2, left2);
  150. }
  151. static void paintLevels(Graphics g, Ship ship, int top, int left) {
  152. if (ship == null)
  153. return;
  154. else if (ship.isAlive()) {
  155. g.setColor(statusMeterBorderColor);
  156. g.drawRect(left, top + 6, 101, 10);
  157. g.drawRect(left, top + 21, 101, 10);
  158. g.setColor(energyStatusMeterColor);
  159. g.fillRect(left + 1, top + 7, (int)(ship.getEnergyLevel()*100), 9);
  160. g.setColor(damageStatusMeterColor);
  161. g.fillRect(left + 1, top + 22, (int)(ship.getDamageLevel()*100), 9);
  162. }
  163. else {
  164. g.setColor(damageStatusMeterColor);
  165. g.drawString("Ship is destroyed", left+1, top+15);
  166. }
  167. }
  168. }