HolonCanvas.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. *
  3. */
  4. package ui.view;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.util.ArrayList;
  12. import javax.swing.JPanel;
  13. import javax.swing.Timer;
  14. import classes.HolonBody;
  15. import classes.SubNet;
  16. import ui.controller.Control;
  17. import ui.model.Model;
  18. /**
  19. * This class shows the holon view
  20. *
  21. * @author Gruppe14
  22. *
  23. */
  24. public class HolonCanvas extends JPanel implements ActionListener {
  25. private static final long serialVersionUID = 1L;
  26. // edge Object Start Point
  27. private Model model;
  28. private final Control controller;
  29. Graphics2D g2; // For Painting
  30. private int N;
  31. private ArrayList<HolonBody> bodies;
  32. private Timer timer = new Timer(33, this);
  33. private Dimension center;
  34. private ArrayList<SubNet> subnets;
  35. private int rx, ry;
  36. /**
  37. * Constructor.
  38. *
  39. * @param mod
  40. * the Model
  41. * @param control
  42. * the Controller
  43. */
  44. public HolonCanvas(Model mod, Control control) {
  45. this.controller = control;
  46. this.model = mod;
  47. subnets = controller.getSimManager().getSubNets();
  48. N = subnets.size();
  49. bodies = new ArrayList<>();
  50. calcCenter();
  51. timer.start();
  52. }
  53. public void paintComponent(Graphics g) {
  54. super.paintComponent(g);
  55. if (!controller.getSimManager().getSubNets().equals(subnets)) {
  56. subnets = controller.getSimManager().getSubNets();
  57. N = subnets.size();
  58. N += 1;
  59. bodies = new ArrayList<>();
  60. startthebodies(N);
  61. }
  62. for (int i = 0; i < N; i++) {
  63. g.setColor(bodies.get(i).color);
  64. if (i != 0) {
  65. g.fillOval(bodies.get(i).rx, bodies.get(i).ry, subnets.get(i - 1).getObjects().size() * 8,
  66. subnets.get(i - 1).getObjects().size() * 8);
  67. } else
  68. g.fillOval(bodies.get(0).rx, bodies.get(0).ry, 0, 0);
  69. }
  70. addforces(N);
  71. }
  72. // Initialize N bodies with random positions
  73. public void startthebodies(int N) {
  74. double solarmass = 1.98892e30;
  75. calcCenter();
  76. // the central mass
  77. bodies.add(0, new HolonBody(center.width, center.height, 0, 0, 1e6 * solarmass, new Color(0, 0, 0)));
  78. for (int i = 0; i < N - 1; i++) {
  79. int px = (int) (Math.random() * 201);
  80. int py = (int) (Math.random() * 201);
  81. double mass = (subnets.get(i).getObjects().size()) / 10 * solarmass * 10 + 1e20;
  82. Color color = model.getSubNetColors().get(i);
  83. bodies.add(new HolonBody(px, py, 0, 0, mass, color));
  84. }
  85. }
  86. // reset the forces, then add all the new forces
  87. public void addforces(int N) {
  88. for (int i = 0; i < N; i++) {
  89. bodies.get(i).resetForce();
  90. for (int j = 0; j < N; j++) {
  91. if (i != j)
  92. bodies.get(i).addForce(bodies.get(j));
  93. }
  94. }
  95. // loop again and update the bodies using timestep dt
  96. for (int i = 1; i < N; i++) {
  97. rx = bodies.get(i).rx;
  98. ry = bodies.get(i).ry;
  99. bodies.get(i).update(1e-9);
  100. if (checkCollison(i)) {
  101. bodies.get(i).rx = rx;
  102. bodies.get(i).ry = ry;
  103. bodies.get(i).vx = 0;
  104. bodies.get(i).vy = 0;
  105. }
  106. }
  107. }
  108. // check if body would collide with any other body
  109. private boolean checkCollison(int i) {
  110. boolean collision = false;
  111. int r1 = (subnets.get(i - 1).getObjects().size() * 8) / 2;
  112. int r2 = 1;
  113. for (int j = 0; j < N; j++) {
  114. if (j != i) {
  115. int dist = (int) (Math.pow(rx - bodies.get(j).rx, 2) + Math.pow(ry - bodies.get(j).ry, 2));
  116. if (j != 0)
  117. r2 = (subnets.get(j - 1).getObjects().size() * 8) / 2;
  118. if (Math.pow(r2 - r1, 2) <= dist && dist <= Math.pow(r2 + r1, 2)) {
  119. collision = true;
  120. }
  121. }
  122. }
  123. return collision;
  124. }
  125. @Override
  126. public void actionPerformed(ActionEvent arg0) {
  127. repaint();
  128. }
  129. // calculate center of canvas for center gravity
  130. public void calcCenter() {
  131. center = this.getSize();
  132. center.height /= 2;
  133. center.width /= 2;
  134. }
  135. }