StatisticGraph.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 classes.HolonSwitch;
  13. import classes.TrackedDataSet;
  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. // 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<TrackedDataSet> objects = new ArrayList<>();
  35. /**
  36. * Constructor.
  37. *
  38. * @param model
  39. * the Model
  40. * @param control
  41. * the Controller
  42. */
  43. public StatisticGraph(final Model model, Control control) {
  44. this.controller = control;
  45. this.setBackground(Color.WHITE);
  46. }
  47. /**
  48. * Paints all Components on the Canvas.
  49. *
  50. * @param g
  51. * Graphics
  52. *
  53. */
  54. public void paintComponent(Graphics g) {
  55. super.paintComponent(g);
  56. // Graphics2D init
  57. g2 = (Graphics2D) g;
  58. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  59. g2.setRenderingHints(rh);
  60. // Paint the Grid
  61. g2.setStroke(new BasicStroke(0));
  62. g2.setColor(Color.BLACK);
  63. for (int i = 0; i <= this.getWidth(); i += 10) {
  64. g2.drawLine(i, 0, i, this.getHeight());
  65. }
  66. for (int i = 0; i <= this.getHeight(); i += 5) {
  67. g2.drawLine(0, i, this.getWidth(), i);
  68. }
  69. if(model != null){
  70. isSimRunning = model.getIsSimulation();
  71. // if sim is on
  72. if (isSimRunning) {
  73. g2.setStroke(new BasicStroke(3));
  74. // Calculate the Maximum
  75. calcMaximum();
  76. // Calculate values for each set and add them
  77. addValues();
  78. //Create Paths and draw them
  79. for (TrackedDataSet set : objects) {
  80. path.reset();
  81. switch (set.getProperty()) {
  82. case TrackedDataSet.CONSUMPTION:
  83. case TrackedDataSet.PRODUCTION:
  84. case TrackedDataSet.ACTIVATED_ELEMENTS:
  85. createPathFloats(set);
  86. break;
  87. case TrackedDataSet.ON_OFF:
  88. createPathBooleans(set);
  89. break;
  90. default:
  91. break;
  92. }
  93. g2.setColor(set.getColor());
  94. g2.draw(path);
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * Add an Object to the Graph if the maximum has not reached yet.
  101. *
  102. * @param obj
  103. * the Object to add
  104. */
  105. public void addObject(TrackedDataSet set) {
  106. if (objects.size() < MAX_OBJECTS && !objects.contains(set)) {
  107. objects.add(set);
  108. }
  109. }
  110. /**
  111. * Removes an Object from the Graph.
  112. *
  113. * @param obj
  114. * the Object to remove
  115. */
  116. public void removeObject() {
  117. for (TrackedDataSet set : objects) {
  118. controller.addTextToConsole("Function not available");
  119. }
  120. }
  121. /**
  122. * converts the number to fit the canvas.
  123. *
  124. * @param d
  125. * the number to convert
  126. * @return the converted number
  127. */
  128. public double convertToCanvasY(float d) {
  129. return Math.abs((this.getHeight() - (d * (this.getHeight() / maximum))));
  130. }
  131. /**
  132. * Does take into account which timestep is watched, calculates the max
  133. * values.
  134. *
  135. * @return the currentEnergy
  136. */
  137. public float getEnergyAtCurrentTimeStep(HolonObject obj) {
  138. float temp = 0;
  139. for (HolonElement e : obj.getElements()) {
  140. if (e.getActive()) {
  141. temp = temp + e.getEnergyAt()[model.getCurIteration()];
  142. }
  143. }
  144. return temp;
  145. }
  146. /**
  147. * Calculate the Max Value of the Graph
  148. */
  149. private void calcMaximum() {
  150. maximum = 0;
  151. for (TrackedDataSet set : objects) {
  152. int val = 0;
  153. switch (set.getProperty()) {
  154. case TrackedDataSet.CONSUMPTION:
  155. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  156. if (h.getEnergy() < 0) {
  157. val += h.getEnergy();
  158. }
  159. }
  160. val *= -1;
  161. break;
  162. case TrackedDataSet.PRODUCTION:
  163. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  164. if (h.getEnergy() > 0) {
  165. val += h.getEnergy();
  166. }
  167. }
  168. break;
  169. case TrackedDataSet.ACTIVATED_ELEMENTS:
  170. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  171. if (h.getEnergy() < 0) {
  172. val += h.getAmount();
  173. }
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. if (val > maximum) {
  180. maximum = val;
  181. }
  182. }
  183. }
  184. /**
  185. * Add the Current Values to each set
  186. */
  187. private void addValues() {
  188. for (TrackedDataSet set : objects) {
  189. switch (set.getProperty()) {
  190. case TrackedDataSet.CONSUMPTION:
  191. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  192. if (h.getEnergy() < 0) {
  193. set.setValAt(-h.getEnergyAt()[model.getCurIteration()], model.getCurIteration());
  194. }
  195. }
  196. break;
  197. case TrackedDataSet.PRODUCTION:
  198. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  199. if (h.getEnergy() > 0) {
  200. set.setValAt(h.getEnergyAt()[model.getCurIteration()], model.getCurIteration());
  201. }
  202. }
  203. break;
  204. case TrackedDataSet.ACTIVATED_ELEMENTS:
  205. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  206. if (h.getEnergy() < 0) {
  207. }
  208. }
  209. break;
  210. case TrackedDataSet.ON_OFF:
  211. if (((HolonSwitch) set.getCpsObject()).getState()) {
  212. set.setValAt(1, model.getCurIteration());
  213. } else {
  214. set.setValAt(0, model.getCurIteration());
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. }
  221. }
  222. /**
  223. * create Path with floats
  224. *
  225. * @param set
  226. */
  227. private void createPathFloats(TrackedDataSet set) {
  228. boolean init = true;
  229. for (int i = 0; i < set.getValues().length - 1; i++) {
  230. if (init && set.getValues()[i] != -1) {
  231. path.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  232. convertToCanvasY(Math.abs(set.getValues()[i])));
  233. }
  234. path.lineTo(model.getCurIteration() * this.getWidth() / model.getIterations(),
  235. convertToCanvasY(Math.abs(set.getValues()[i + 1])));
  236. }
  237. }
  238. /**
  239. * create Path with booleans(0 and 1)
  240. *
  241. * @param set
  242. */
  243. private void createPathBooleans(TrackedDataSet set) {
  244. boolean init = true;
  245. for (int i = 0; i < set.getValues().length - 1; i++) {
  246. if (init && set.getValues()[i] != -1) {
  247. path.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
  248. convertToCanvasY((float) (set.getValues()[i] * maximum)));
  249. }
  250. path.lineTo(model.getCurIteration() * this.getWidth() / model.getIterations(),
  251. convertToCanvasY((float) (set.getValues()[i + 1] * maximum)));
  252. }
  253. }
  254. }