HolonCanvas.java 6.6 KB

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