StatisticGraph.java 5.0 KB

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