HolonCanvas.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package ui.view;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.util.ArrayList;
  8. import javax.swing.JPanel;
  9. import classes.Constants;
  10. import classes.HolonBody;
  11. import classes.SubNet;
  12. import ui.controller.Control;
  13. import ui.model.Model;
  14. public class HolonCanvas extends JPanel {
  15. /**
  16. *
  17. */
  18. private static final long serialVersionUID = 1L;
  19. // Rendering
  20. private Graphics2D g2;
  21. // Ball objects
  22. private ArrayList<HolonBody> bodies = new ArrayList<>();
  23. private int subCount;
  24. // Frames
  25. private int currentFrameRate;
  26. long previousTime = System.currentTimeMillis();
  27. long currentTime = previousTime;
  28. long elapsedTime;
  29. long totalElapsedTime = 0;
  30. int frameCount = 0;
  31. private Dimension center;
  32. private ArrayList<SubNet> subnets;
  33. private Control controller;
  34. private Model model;
  35. public HolonCanvas(Model mod, Control control) {
  36. // Wire up Events
  37. this.controller = control;
  38. this.model = mod;
  39. subnets = controller.getSimManager().getSubNets();
  40. subCount = subnets.size();
  41. previousTime = System.currentTimeMillis();
  42. currentTime = previousTime;
  43. totalElapsedTime = 0;
  44. frameCount = 0;
  45. }
  46. // Start Render and Update Threads
  47. public void paintComponent(Graphics g) {
  48. super.paintComponent(g);
  49. if (!controller.getSimManager().getSubNets().equals(subnets)) {
  50. subnets = controller.getSimManager().getSubNets();
  51. subCount = subnets.size();
  52. // bodies = new ArrayList<>();
  53. // createBodies(subCount);
  54. calcCenter();
  55. if (bodies.isEmpty()) {
  56. createBodies(subCount);
  57. } else {
  58. addNewBodies(subCount);
  59. }
  60. }
  61. currentTime = System.currentTimeMillis();
  62. elapsedTime = (currentTime - previousTime); // elapsed time in seconds
  63. totalElapsedTime += elapsedTime;
  64. if (totalElapsedTime > 1000) {
  65. currentFrameRate = frameCount;
  66. frameCount = 0;
  67. totalElapsedTime = 0;
  68. }
  69. updateGame(elapsedTime / 1000f);
  70. render(g);
  71. try {
  72. // Thread.sleep(getFpsDelay(maxFrameRate));
  73. Thread.sleep(5);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. previousTime = currentTime;
  78. frameCount++;
  79. repaint();
  80. }
  81. // updates the bodies according to the changes of subnets
  82. private void addNewBodies(int subCount) {
  83. ArrayList<HolonBody> newBodies = new ArrayList<>();
  84. for (int i = 0; i < bodies.size(); i++) {
  85. for (int j = 0; j < subCount; j++) {
  86. if (model.getSubNetColors().get(j) == bodies.get(i).getColor()) {
  87. bodies.get(i).setRadius(subnets.get(j).getObjects().size() * 5 + 10);
  88. bodies.get(i).setMass(subnets.get(j).getObjects().size() * 5 + 10);
  89. newBodies.add(bodies.get(i));
  90. break;
  91. }
  92. }
  93. }
  94. bodies = newBodies;
  95. for (int i = bodies.size(); i < subCount; i++) {
  96. HolonBody temp = new HolonBody((center.width + 1) + (-1 * i), (center.height + 1) + (-1 * i),
  97. subnets.get(i).getObjects().size() * 5 + 10, subnets.get(i).getObjects().size() * 5 + 10,
  98. model.getSubNetColors().get(i));
  99. bodies.add(temp);
  100. }
  101. }
  102. // creates the first round of bodies
  103. private void createBodies(int subCount) {
  104. for (int i = 0; i < subCount; i++) {
  105. HolonBody temp = new HolonBody((center.width + 1) + (-1 * i), (center.height + 1) + (-1 * i),
  106. subnets.get(i).getObjects().size() * 5 + 10, subnets.get(i).getObjects().size() * 5 + 10,
  107. model.getSubNetColors().get(i));
  108. bodies.add(temp);
  109. }
  110. }
  111. public void render(Graphics g) {
  112. // System.out.printf("Width: %d Height: %d\n", getWidth(), getHeight());
  113. // Create BufferStrategy for rendering/drawing
  114. this.g2 = (Graphics2D) g;
  115. // Turn on anti-aliasing
  116. this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  117. // Render Background
  118. this.g2.setColor(Color.WHITE);
  119. this.g2.fillRect(0, 0, getWidth(), getHeight());
  120. // Render Game Objects
  121. for (int i = 0; i < subCount; i++) {
  122. bodies.get(i).setRadius((subnets.get(i).getObjects().size() * 5 + 10)*controller.getHolonBodyScale()/100);
  123. bodies.get(i).draw(this.g2, controller.getHolonBodyScale());
  124. }
  125. }
  126. public void updateGame(float elapsedSeconds) {
  127. // step the position of movable objects based off their velocity/gravity
  128. // and elapsedTime
  129. calcCenter();
  130. for (int i = 0; i < subCount; i++) {
  131. bodies.get(i).position
  132. .setX((float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
  133. - ((bodies.get(i).position.getX() - center.getWidth()) / 100)));
  134. bodies.get(i).position
  135. .setY((float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
  136. - ((bodies.get(i).position.getY() - center.getHeight()) / 100)));
  137. if (Math.abs(bodies.get(i).velocity.getX()) < Constants.epsilon)
  138. bodies.get(i).velocity.setX(0);
  139. if (Math.abs(bodies.get(i).velocity.getY()) < Constants.epsilon)
  140. bodies.get(i).velocity.setY(0);
  141. }
  142. checkCollisions();
  143. }
  144. // Insertion sort for Sweep and Prune
  145. public void insertionSort(ArrayList<HolonBody> a) {
  146. for (int p = 1; p < subCount; p++) {
  147. Comparable tmp = a.get(p);
  148. int j = p;
  149. for (; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--)
  150. a.set(j, a.get(j - 1));
  151. a.set(j, (HolonBody) tmp);
  152. }
  153. }
  154. public void checkCollisions() {
  155. insertionSort(bodies);
  156. for (int i = 0; i < subCount; i++) {
  157. // Ball to Ball collision
  158. for (int j = i + 1; j < subCount; j++) {
  159. if ((bodies.get(i).position.getX() + bodies.get(i).getRadius()) < (bodies.get(j).position.getX()
  160. - bodies.get(j).getRadius()))
  161. break;
  162. if ((bodies.get(i).position.getY() + bodies.get(i).getRadius()) < (bodies.get(j).position.getY()
  163. - bodies.get(j).getRadius())
  164. || (bodies.get(j).position.getY() + bodies.get(j).getRadius()) < (bodies.get(i).position.getY()
  165. - bodies.get(i).getRadius()))
  166. continue;
  167. bodies.get(i).resolveCollision(bodies.get(j));
  168. }
  169. }
  170. }
  171. public void calcCenter() {
  172. center = this.getSize();
  173. center.height /= 2;
  174. center.width /= 2;
  175. }
  176. }