HolonCanvas.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 HolonBody currentBall;
  24. private int subCount;
  25. // Frames
  26. private int currentFrameRate;
  27. long previousTime = System.currentTimeMillis();
  28. long currentTime = previousTime;
  29. long elapsedTime;
  30. long totalElapsedTime = 0;
  31. int frameCount = 0;
  32. private Dimension center;
  33. private ArrayList<SubNet> subnets;
  34. private Control controller;
  35. private Model model;
  36. public HolonCanvas(Model mod, Control control) {
  37. // Wire up Events
  38. this.controller = control;
  39. this.model = mod;
  40. subnets = controller.getSimManager().getSubNets();
  41. subCount = subnets.size();
  42. previousTime = System.currentTimeMillis();
  43. currentTime = previousTime;
  44. totalElapsedTime = 0;
  45. frameCount = 0;
  46. }
  47. // Start Render and Update Threads
  48. public void paintComponent(Graphics g) {
  49. super.paintComponent(g);
  50. if (!controller.getSimManager().getSubNets().equals(subnets)) {
  51. subnets = controller.getSimManager().getSubNets();
  52. subCount = subnets.size();
  53. bodies = new ArrayList<>();
  54. createBodies(subCount);
  55. }
  56. currentTime = System.currentTimeMillis();
  57. elapsedTime = (currentTime - previousTime); // elapsed time in seconds
  58. totalElapsedTime += elapsedTime;
  59. if (totalElapsedTime > 1000) {
  60. currentFrameRate = frameCount;
  61. frameCount = 0;
  62. totalElapsedTime = 0;
  63. }
  64. updateGame(elapsedTime / 1000f);
  65. render(g);
  66. try {
  67. // Thread.sleep(getFpsDelay(maxFrameRate));
  68. Thread.sleep(5);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. previousTime = currentTime;
  73. frameCount++;
  74. repaint();
  75. }
  76. private void createBodies(int subCount) {
  77. calcCenter();
  78. for(int i = 0; i<subCount; i++){
  79. HolonBody temp = new HolonBody(center.width+i, center.height+i, subnets.get(i).getObjects().size()*5+10, subnets.get(i).getObjects().size()*5+10, model.getSubNetColors().get(i));
  80. bodies.add(temp);
  81. }
  82. }
  83. public void render(Graphics g) {
  84. // System.out.printf("Width: %d Height: %d\n", getWidth(), getHeight());
  85. // Create BufferStrategy for rendering/drawing
  86. this.g2 = (Graphics2D) g;
  87. // Turn on anti-aliasing
  88. this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  89. // Render Background
  90. this.g2.setColor(Color.WHITE);
  91. this.g2.fillRect(0, 0, getWidth(), getHeight());
  92. // Render Game Objects
  93. for (int i = 0; i < subCount; i++) {
  94. bodies.get(i).draw(this.g2);
  95. }
  96. HolonBody tempBall = currentBall;
  97. if (tempBall != null)
  98. tempBall.draw(this.g2);
  99. }
  100. public void updateGame(float elapsedSeconds) {
  101. // step the position of movable objects based off their velocity/gravity
  102. // and elapsedTime
  103. calcCenter();
  104. for (int i = 0; i < subCount; i++) {
  105. bodies.get(i).position.setX((float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
  106. - ((bodies.get(i).position.getX() - center.getWidth()) / 50)));
  107. bodies.get(i).position.setY((float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
  108. - ((bodies.get(i).position.getY() - center.getHeight()) / 50)));
  109. if (Math.abs(bodies.get(i).velocity.getX()) < Constants.epsilon)
  110. bodies.get(i).velocity.setX(0);
  111. if (Math.abs(bodies.get(i).velocity.getY()) < Constants.epsilon)
  112. bodies.get(i).velocity.setY(0);
  113. }
  114. checkCollisions();
  115. }
  116. // Insertion sort for Sweep and Prune
  117. public void insertionSort(ArrayList<HolonBody> a) {
  118. for (int p = 1; p < subCount; p++) {
  119. Comparable tmp = a.get(p);
  120. int j = p;
  121. for (; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--)
  122. a.set(j, a.get(j - 1));
  123. a.set(j, (HolonBody) tmp);
  124. }
  125. }
  126. public void checkCollisions() {
  127. insertionSort(bodies);
  128. for (int i = 0; i < subCount; i++) {
  129. // Ball to Ball collision
  130. for (int j = i + 1; j < subCount; j++) {
  131. if ((bodies.get(i).position.getX() + bodies.get(i).getRadius()) < (bodies.get(j).position.getX()
  132. - bodies.get(j).getRadius()))
  133. break;
  134. if ((bodies.get(i).position.getY() + bodies.get(i).getRadius()) < (bodies.get(j).position.getY()
  135. - bodies.get(j).getRadius())
  136. || (bodies.get(j).position.getY() + bodies.get(j).getRadius()) < (bodies.get(i).position.getY()
  137. - bodies.get(i).getRadius()))
  138. continue;
  139. bodies.get(i).resolveCollision(bodies.get(j));
  140. }
  141. }
  142. }
  143. public void calcCenter() {
  144. center = this.getSize();
  145. center.height /= 2;
  146. center.width /= 2;
  147. }
  148. }