StatisticGraph.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.geom.GeneralPath;
  8. import java.util.ArrayList;
  9. import javax.swing.JPanel;
  10. import classes.HolonElement;
  11. import classes.HolonObject;
  12. import ui.controller.Control;
  13. import ui.model.Model;
  14. public class StatisticGraph extends JPanel {
  15. /**
  16. *
  17. */
  18. private static final long serialVersionUID = 1L;
  19. // Max Objects
  20. final int MAX_OBJECTS = 8;
  21. // Maximum y Value
  22. double maximum = 0;
  23. // is the Simulation running?
  24. private boolean isSimRunning;
  25. // Colours
  26. private ArrayList<Color> colors = new ArrayList<>();
  27. // model and controller
  28. private Model model;
  29. private Control controller;
  30. // Graphics2D
  31. private Graphics2D g2;
  32. GeneralPath path = new GeneralPath();
  33. // Data
  34. private ArrayList<HolonObject> objects = new ArrayList<>();
  35. private ArrayList<GeneralPath> objPaths = new ArrayList<>();
  36. /**
  37. * Constructor.
  38. *
  39. * @param model
  40. * the Model
  41. * @param control
  42. * the Controller
  43. */
  44. public StatisticGraph(final Model model, Control control) {
  45. this.controller = control;
  46. this.model = model;
  47. colors.add(Color.BLUE);
  48. colors.add(Color.RED);
  49. colors.add(Color.GREEN);
  50. colors.add(Color.CYAN);
  51. colors.add(Color.ORANGE);
  52. colors.add(Color.PINK);
  53. colors.add(Color.MAGENTA);
  54. colors.add(Color.YELLOW);
  55. this.setBackground(Color.WHITE);
  56. }
  57. /**
  58. * Paints all Components on the Canvas.
  59. *
  60. * @param g
  61. * Graphics
  62. *
  63. */
  64. public void paintComponent(Graphics g) {
  65. super.paintComponent(g);
  66. // Graphics2D init
  67. g2 = (Graphics2D) g;
  68. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  69. g2.setRenderingHints(rh);
  70. // Paint the Grid
  71. g2.setStroke(new BasicStroke(0));
  72. g2.setColor(Color.BLACK);
  73. for (int i = 0; i <= this.getWidth(); i += 10) {
  74. g2.drawLine(i, 0, i, this.getHeight());
  75. }
  76. for (int i = 0; i <= this.getHeight(); i += 5) {
  77. g2.drawLine(0, i, this.getWidth(), i);
  78. }
  79. // Reset?
  80. if (!isSimRunning && model.getIsSimulation()) {
  81. for (int i = 0; i < objects.size(); i++) {
  82. objPaths.get(i).reset();
  83. objPaths.get(i).moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  84. convertToCanvasY(Math.abs(objects.get(i).getCurrentEnergy())));
  85. }
  86. }
  87. isSimRunning = model.getIsSimulation();
  88. // if sim is on
  89. if (isSimRunning) {
  90. maximum = 0;
  91. for (HolonObject obj : objects) {
  92. if (maximum <= (Math.abs(obj.getCurrentEnergy()))) {
  93. maximum = (Math.abs(obj.getCurrentEnergy()));
  94. }
  95. }
  96. for (int i = 0; i < objects.size(); i++) {
  97. g2.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  98. (int) (Math.random() * 255)));
  99. objPaths.get(i).lineTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  100. convertToCanvasY(Math.abs(getEnergyAtCurrentTimeStep(objects.get(i)))));
  101. objPaths.get(i).moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  102. convertToCanvasY(Math.abs(getEnergyAtCurrentTimeStep(objects.get(i)))));
  103. }
  104. }
  105. g2.setStroke(new BasicStroke(3));
  106. for (int i = 0; i < objPaths.size(); i++) {
  107. g2.setColor(colors.get(i));
  108. g2.draw(objPaths.get(i));
  109. }
  110. }
  111. /**
  112. * Add an Object to the Graph if the maximum has not reached yet.
  113. *
  114. * @param obj
  115. * the Object to add
  116. */
  117. public void addObject(HolonObject obj) {
  118. if (objects.size() < MAX_OBJECTS && !objects.contains(obj)) {
  119. objects.add((HolonObject) obj);
  120. objPaths.add(new GeneralPath());
  121. objPaths.get(objPaths.size() - 1)
  122. .moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1, convertToCanvasY(Math
  123. .abs(objects.get(objects.size() - 1).getCurrentEnergyAtTimeStep(model.getCurIteration()))));
  124. }
  125. }
  126. /**
  127. * Removes an Object from the Graph.
  128. *
  129. * @param obj
  130. * the Object to remove
  131. */
  132. public void removeObject(HolonObject obj) {
  133. if (objects.contains(obj)) {
  134. objPaths.remove(objects.indexOf(obj));
  135. objects.remove(obj);
  136. }
  137. }
  138. /**
  139. * converts the number to fit the canvas.
  140. *
  141. * @param d
  142. * the number to convert
  143. * @return the converted number
  144. */
  145. public double convertToCanvasY(float d) {
  146. return Math.abs((this.getHeight() - (d * (this.getHeight() / maximum))));
  147. }
  148. /**
  149. * Does take into account which timestep is watched, calculates the max
  150. * values.
  151. *
  152. * @return the currentEnergy
  153. */
  154. public float getEnergyAtCurrentTimeStep(HolonObject obj) {
  155. float temp = 0;
  156. for (HolonElement e : obj.getElements()) {
  157. if (e.getActive()) {
  158. temp = temp + e.getEnergyAt()[model.getCurIteration()];
  159. }
  160. }
  161. return temp;
  162. }
  163. }