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.0KB

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