StatisticGraph.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.Image;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.geom.GeneralPath;
  11. import java.util.ArrayList;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15. import classes.HolonElement;
  16. import classes.HolonObject;
  17. import classes.HolonSwitch;
  18. import classes.TrackedDataSet;
  19. import ui.controller.Control;
  20. import ui.model.Model;
  21. public class StatisticGraph extends JPanel {
  22. /**
  23. *
  24. */
  25. private static final long serialVersionUID = 1L;
  26. // Maximum y Value
  27. double maximum = 0;
  28. // is the Simulation running?
  29. private boolean isSimRunning;
  30. // model and controller
  31. private Model model;
  32. private Control controller;
  33. // Graphics2D
  34. private Graphics2D g2;
  35. GeneralPath path = new GeneralPath();
  36. // Data
  37. private ArrayList<TrackedDataSet> objects = 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. this.setBackground(Color.WHITE);
  50. }
  51. /**
  52. * Paints all Components on the Canvas.
  53. *
  54. * @param g
  55. * Graphics
  56. *
  57. */
  58. public void paintComponent(Graphics g) {
  59. super.paintComponent(g);
  60. // Graphics2D init
  61. g2 = (Graphics2D) g;
  62. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  63. g2.setRenderingHints(rh);
  64. // Paint the Grid
  65. g2.setStroke(new BasicStroke(0));
  66. g2.setColor(Color.BLACK);
  67. for (int i = 0; i <= this.getWidth(); i += 10) {
  68. g2.drawLine(i, 0, i, this.getHeight());
  69. }
  70. for (int i = 0; i <= this.getHeight(); i += 5) {
  71. g2.drawLine(0, i, this.getWidth(), i);
  72. }
  73. isSimRunning = model.getIsSimulation();
  74. // if sim is on
  75. if (isSimRunning) {
  76. g2.setStroke(new BasicStroke(3));
  77. // Calculate the Maximum
  78. calcMaximum();
  79. // Calculate values for each set and add them
  80. addValues();
  81. // Create Paths and draw them
  82. for (TrackedDataSet set : objects) {
  83. path.reset();
  84. switch (set.getProperty()) {
  85. case TrackedDataSet.CONSUMPTION:
  86. case TrackedDataSet.PRODUCTION:
  87. case TrackedDataSet.ACTIVATED_ELEMENTS:
  88. createPathFloats(set);
  89. break;
  90. case TrackedDataSet.ON_OFF:
  91. createPathBooleans(set);
  92. break;
  93. default:
  94. break;
  95. }
  96. g2.setColor(set.getColor());
  97. g2.draw(path);
  98. }
  99. }
  100. }
  101. /**
  102. * Add an Object to the Graph if the maximum has not reached yet.
  103. *
  104. * @param obj
  105. * the Object to add
  106. */
  107. public void addObject(TrackedDataSet set) {
  108. objects.add(set);
  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. val += h.getAmount();
  172. }
  173. break;
  174. case TrackedDataSet.ON_OFF:
  175. val = 1;
  176. break;
  177. default:
  178. break;
  179. }
  180. if (val > maximum) {
  181. maximum = val;
  182. }
  183. }
  184. }
  185. /**
  186. * Add the Current Values to each set
  187. */
  188. private void addValues() {
  189. for (TrackedDataSet set : objects) {
  190. int val = 0;
  191. switch (set.getProperty()) {
  192. case TrackedDataSet.CONSUMPTION:
  193. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  194. if (h.getEnergy() < 0) {
  195. val += Math.abs(h.getEnergyAt()[model.getCurIteration()]);
  196. }
  197. set.setValAt(val, model.getCurIteration());
  198. }
  199. break;
  200. case TrackedDataSet.PRODUCTION:
  201. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  202. if (h.getEnergy() > 0) {
  203. val += Math.abs(h.getEnergyAt()[model.getCurIteration()]);
  204. }
  205. set.setValAt(val, model.getCurIteration());
  206. }
  207. break;
  208. case TrackedDataSet.ACTIVATED_ELEMENTS:
  209. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  210. if (h.getActive()) {
  211. val += h.getAmount();
  212. }
  213. set.setValAt(val, model.getCurIteration());
  214. }
  215. break;
  216. case TrackedDataSet.ON_OFF:
  217. if (((HolonSwitch) set.getCpsObject()).getManualMode()) {
  218. if (((HolonSwitch) set.getCpsObject()).getActiveManual()) {
  219. set.setValAt(1, model.getCurIteration());
  220. } else {
  221. set.setValAt(0, model.getCurIteration());
  222. }
  223. } else {
  224. if (((HolonSwitch) set.getCpsObject()).getActiveAt()[model.getCurIteration()]) {
  225. set.setValAt(1, model.getCurIteration());
  226. } else {
  227. set.setValAt(0, model.getCurIteration());
  228. }
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. }
  236. /**
  237. * create Path with floats
  238. *
  239. * @param set
  240. */
  241. private void createPathFloats(TrackedDataSet set) {
  242. boolean init = true;
  243. path.moveTo(0, 0);
  244. for (int i = 0; i < model.getCurIteration() - 1; i++) {
  245. controller.addTextToConsole(path.getCurrentPoint().getX() + ", " + path.getCurrentPoint().getY());
  246. if (init /* && set.getValues()[i] != -1 */) {
  247. path.moveTo(i * this.getWidth() / model.getIterations() - 1, convertToCanvasY(set.getValues()[i]));
  248. init = false;
  249. }
  250. if (!init) {
  251. if (set.getValues()[i + 1] != -1) {
  252. path.lineTo((i + 1) * this.getWidth() / model.getIterations(),
  253. convertToCanvasY(set.getValues()[i + 1]));
  254. } else {
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. * create Path with booleans(0 and 1)
  262. *
  263. * @param set
  264. */
  265. private void createPathBooleans(TrackedDataSet set) {
  266. boolean init = true;
  267. for (int i = 0; i < model.getCurIteration() - 1; i++) {
  268. if (init && set.getValues()[i] != -1) {
  269. path.moveTo(i * this.getWidth() / model.getIterations() - 1,
  270. convertToCanvasY((float) (set.getValues()[i] * maximum)));
  271. init = false;
  272. }
  273. if (!init) {
  274. if (set.getValues()[i + 1] != -1) {
  275. path.lineTo((i + 1) * this.getWidth() / model.getIterations(),
  276. convertToCanvasY((float) (set.getValues()[i + 1] * maximum)));
  277. } else {
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. }