StatisticGraph.java 4.9 KB

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