UnitGraph.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. package ui.view;
  2. import classes.*;
  3. import ui.controller.Control;
  4. import ui.controller.SingletonControl;
  5. import ui.model.Model;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.awt.geom.CubicCurve2D;
  10. import java.awt.geom.GeneralPath;
  11. import java.awt.geom.Line2D;
  12. import java.util.ArrayDeque;
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15. import java.util.LinkedList;
  16. /**
  17. * This Class represents a Graph where the User can model the behavior of
  18. * elements and switches over time.
  19. *
  20. * @author Gruppe14
  21. */
  22. public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  23. private static final long serialVersionUID = 1L;
  24. public static final int STANDARD_GRAPH_ACCURACY = 100;
  25. private GeneralPath graphCurve = new GeneralPath();
  26. private float maximum = 0;
  27. // Information shown when a Point is Dragged
  28. private String dragInformation = "";
  29. // Points
  30. private Point recSize = new Point(8, 8); // Point Size
  31. private Graphics2D g2;
  32. private CubicCurve2D c = new CubicCurve2D.Double();
  33. private CubicCurve2D cr = new CubicCurve2D.Double();
  34. private CubicCurve2D cl = new CubicCurve2D.Double();
  35. private LinkedList<Point> pointList;
  36. // Scale for the Graph
  37. private double scaleX;
  38. private double scaleY;
  39. //private float[] arrayOfFloats = null;
  40. //private boolean[] arrayOfBooleans = null;
  41. private double width = -1;
  42. private double height = -1;
  43. private boolean isElement = false;
  44. private boolean isSwitch = false;
  45. private ArrayList<HolonElement> tempElements = new ArrayList<>();
  46. private Model model;
  47. private Control controller;
  48. private Line2D.Double line = null;
  49. private boolean pointDrag = false;
  50. private boolean init = true;
  51. private Point tempP = null;
  52. private double x = 0, y = 0;
  53. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  54. private int border = 4;
  55. private int textWidth = 0;
  56. private IGraphedElement current;
  57. /**
  58. * Constructor.
  59. *
  60. * @param model the Model
  61. * @param control the Controller
  62. */
  63. public UnitGraph(final Model model, Control control) {
  64. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  65. this.controller = control;
  66. this.model = model;
  67. this.pointList = new LinkedList<>();
  68. this.setBackground(Color.WHITE);
  69. this.addMouseListener(this);
  70. this.addMouseMotionListener(this);
  71. this.addComponentListener(this);
  72. }
  73. /**
  74. * Paints all Components on the Canvas.
  75. *
  76. * @param g Graphics
  77. */
  78. public void paintComponent(Graphics g) {
  79. super.paintComponent(g);
  80. g2 = (Graphics2D) g;
  81. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  82. g2.setRenderingHints(rh);
  83. g2.setStroke(new BasicStroke(0));
  84. graphCurve.reset();
  85. border = (int) recSize.getX() >> 1;
  86. // Draw the Vertical Lines
  87. g2.setColor(Color.BLACK);
  88. for (int i = border; i < this.getWidth() - border; i += border * 2) {
  89. g2.drawLine(i, border, i, this.getHeight() - border);
  90. }
  91. g2.drawLine(this.getWidth() - border, border, this.getWidth() - border, this.getHeight() - border);
  92. for (int i = border; i < this.getHeight() - border; i += border * 2) {
  93. g2.drawLine(border, i, this.getWidth() - border, i);
  94. }
  95. g2.drawLine(border, this.getHeight() - border, this.getWidth() - border, this.getHeight() - border);
  96. int effectiveX;
  97. if(current!=null)effectiveX=getEffectiveIndex(model, current, model.getCurIteration());
  98. else effectiveX=0;
  99. if (isElement) {
  100. // fill array with values from the pointList in a HolonElement
  101. fillArrayofValue();
  102. if (current != null) {
  103. // Draw the Lines
  104. g2.setStroke(new BasicStroke(2));
  105. g2.setColor(Color.BLACK);
  106. for (int i = 0; i < pointList.size() - 1; i++) {
  107. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  108. c.setCurve((x1 * scaleX) + border, (y1 * scaleY) + border, (ctrlx1 * scaleX) + border,
  109. (ctrly1 * scaleY) + border, (ctrlx2 * scaleX) + border, (ctrly2 * scaleY) + border,
  110. (x2 * scaleX) + border, (y2 * scaleY) + border);
  111. graphCurve.append(c, true);
  112. }
  113. g2.draw(graphCurve);
  114. // Draw the Points
  115. g2.setColor(Color.BLUE);
  116. for (Point aPointList : pointList) {
  117. g2.fillOval((int) (aPointList.getX() * scaleX - recSize.getX() / 2) + border,
  118. (int) (aPointList.getY() * scaleY - recSize.getY() / 2) + border,
  119. (int) recSize.getX(), (int) recSize.getY());
  120. }
  121. // Iteration Value
  122. //TODO: added function getGraphIterations see if it works
  123. textWidth = g.getFontMetrics().stringWidth("" + ((HolonElement)current).getAvailableEnergyAt(model.getCurIteration())/*arrayOfFloats[effectiveX]*/) + 2;
  124. if (textWidth
  125. + (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1) + 2
  126. + border <= this.getWidth()) {
  127. g2.drawString("" + ((HolonElement)current).getAvailableEnergyAt(model.getCurIteration()),
  128. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  129. + 2 + border,
  130. this.getHeight() - 10);
  131. } else {
  132. g2.drawString("" + ((HolonElement)current).getAvailableEnergyAt(model.getCurIteration()),
  133. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  134. + border - textWidth,
  135. this.getHeight() - 10);
  136. }
  137. }
  138. // drag Information
  139. if (tempP != null && pointDrag) {
  140. dragInformation = "" + convertToValueY(getYValueAt((int) tempP.getX()));
  141. textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX()))) + 2;
  142. if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
  143. g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) + 10 + border,
  144. (int) (tempP.getY() * scaleY) + 10);
  145. } else {
  146. g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) - textWidth,
  147. (int) (tempP.getY() * scaleY) + 10);
  148. }
  149. }
  150. /*
  151. * // Actual Iteration Point Visualization g2.setColor(Color.RED);
  152. * if (arrayOfFloats != null) { for (int i = 0; i <
  153. * arrayOfFloats.length; i++) { g2.fillOval((int) (i * width /
  154. * (model.getIterations() - 1) * scaleX - recSize.getX() /
  155. * 2)+border, (int) (convertToCanvasY((int) arrayOfFloats[i]) *
  156. * scaleY - recSize.getY() / 2)+border, (int) recSize.getX(), (int)
  157. * recSize.getY()); } }
  158. */
  159. } else if (isSwitch) {
  160. if (/*arrayOfBooleans*/current != null) {//Technically this test should be unnecessary
  161. // array fillen
  162. fillArrayofBooleans();
  163. // Draw the Lines
  164. g2.setStroke(new BasicStroke(2));
  165. g2.setColor(Color.BLACK);
  166. for (int i = 0; i < pointList.size() - 1; i++) {
  167. // Left out of bounce
  168. if ((i == 1 || i == 2) && pointList.get(i).getX() < 0) {
  169. line = new Line2D.Double(border, pointList.get(i).getY() * scaleY, border,
  170. pointList.get(i + 1).getY() * scaleY);
  171. }
  172. // Right out of bounce
  173. else if (i == pointList.size() - 4 && pointList.get(pointList.size() - 3).getX() > width) {
  174. line = new Line2D.Double(pointList.get(i).getX() * scaleX + border,
  175. pointList.get(i).getY() * scaleY, this.getWidth() - border,
  176. pointList.get(i + 1).getY() * scaleY);
  177. } else if (i == pointList.size() - 3 && pointList.get(pointList.size() - 3).getX() > width) {
  178. line = new Line2D.Double(this.getWidth() - border, pointList.get(i).getY() * scaleY,
  179. this.getWidth() - border, pointList.get(i + 1).getY() * scaleY);
  180. } else if (i == pointList.size() - 2 && pointList.get(pointList.size() - 2).getX() > width) {
  181. line = new Line2D.Double(this.getWidth() - border, pointList.get(i).getY() * scaleY,
  182. pointList.get(i + 1).getX() * scaleX + border, pointList.get(i + 1).getY() * scaleY);
  183. } else {
  184. line = new Line2D.Double(pointList.get(i).getX() * scaleX + border,
  185. pointList.get(i).getY() * scaleY, pointList.get(i + 1).getX() * scaleX + border,
  186. pointList.get(i + 1).getY() * scaleY);
  187. }
  188. graphCurve.append(line, true);
  189. }
  190. g2.draw(graphCurve);
  191. /*
  192. * // Draw the Points g2.setColor(Color.BLUE); for (int i = 0; i
  193. * < pointList.size() - 0; i++) { g2.fillOval((int)
  194. * (pointList.get(i).getX() * scaleX - recSize.getX() / 2) +
  195. * border, (int) (pointList.get(i).getY() * scaleY -
  196. * recSize.getY() / 2), (int) recSize.getX(), (int)
  197. * recSize.getY()); }
  198. */
  199. // Iteration Value
  200. g2.setColor(Color.BLUE);
  201. textWidth = g.getFontMetrics().stringWidth("" + ((HolonSwitch)current).getState(model.getCurIteration())/*arrayOfBooleans[effectiveX]*/) + 2;
  202. if (textWidth
  203. + (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1) + 2
  204. + border <= this.getWidth()) {
  205. g2.drawString("" + ((HolonSwitch)current).getState(model.getCurIteration()),
  206. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  207. + 2 + border,
  208. this.getHeight() - 10);
  209. } else {
  210. g2.drawString("" + ((HolonSwitch)current).getState(model.getCurIteration()),
  211. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  212. + border - textWidth,
  213. this.getHeight() - 10);
  214. }
  215. }
  216. // When the switch graph is dragged
  217. if (tempP != null && pointDrag)
  218. {
  219. try {
  220. int i;
  221. for (i = 0; (i * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  222. + border < getMousePosition().getX()); i++) {
  223. }
  224. dragInformation = "" + i;
  225. textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX())))
  226. + 2;
  227. if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
  228. g2.drawString(dragInformation, (int) (getMousePosition().getX()) + 10 + border,
  229. (int) (getMousePosition().getY() * scaleY) + 10);
  230. } else {
  231. g2.drawString(dragInformation, (int) (getMousePosition().getX()) - textWidth,
  232. (int) (getMousePosition().getY() * scaleY) + 10);
  233. }
  234. } catch (Exception e) {
  235. }
  236. }
  237. }
  238. // Iteration Line TODO: repeat
  239. g2.setColor(Color.BLUE);
  240. g2.setStroke(new BasicStroke(1));
  241. g2.drawLine(border + (effectiveX) * (this.getWidth() - border * 2) / /*(model.getIterations() - 1)*/100-1,
  242. 0, border + (effectiveX) * (this.getWidth() - border * 2) / /*(model.getIterations() - 1)*/100-1,
  243. this.getHeight());
  244. // algorithmus
  245. controller.calculateStateForTimeStep(model.getCurIteration());
  246. }
  247. @Override
  248. public void mouseDragged(MouseEvent e) {
  249. if (isElement) {
  250. elementDragged(e);
  251. } else if (isSwitch) {
  252. switchDragged(e);
  253. }
  254. }
  255. /**
  256. * When a Point of a Holon Element is dragged.
  257. *
  258. * @param e MouseEvent
  259. */
  260. public void elementDragged(MouseEvent e) {
  261. if (pointDrag && tempP != null) {
  262. // Out of Bounds verhindern
  263. int i = pointList.indexOf(tempP);
  264. x = (e.getX() - border) / scaleX;
  265. y = (e.getY() - border) / scaleY;
  266. // y
  267. if (e.getY() <= border) {
  268. y = 0;
  269. } else if (this.getHeight() - border <= e.getY()) {
  270. y = (this.getHeight() - border * 2) / scaleY;
  271. }
  272. // x
  273. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x + 2
  274. || pointList.get(i - 1).getX() > x - 2 || pointList.getFirst().getX() > x - 2
  275. || pointList.getLast().getX() < x + 2) {
  276. x = tempP.getX();
  277. }
  278. tempP.setLocation(x, y);
  279. repaint();
  280. }
  281. }
  282. /**
  283. * When a Point of a switch is dragged.
  284. *
  285. * @param e MouseEvent
  286. */
  287. public void switchDragged(MouseEvent e) {
  288. if (pointDrag && tempP != null && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
  289. int i = pointList.indexOf(tempP);
  290. x = (e.getX() - border) / scaleX;
  291. if (pointList.get(i + 1).getY() == tempP.getY()) {
  292. // x
  293. if (pointList.get(i + 1).getX() <= x + 1 || pointList.get(i - 2).getX() >= x - 1) {
  294. x = tempP.getX();
  295. }
  296. pointList.get(i - 1).setLocation(x, pointList.get(i - 1).getY());
  297. } else {
  298. // x
  299. if (pointList.get(i + 2).getX() <= x + 1 || pointList.get(i - 1).getX() >= x - 1) {
  300. x = tempP.getX();
  301. }
  302. pointList.get(i + 1).setLocation(x, pointList.get(i + 1).getY());
  303. }
  304. tempP.setLocation(x, tempP.getY());
  305. repaint();
  306. }
  307. }
  308. @Override
  309. public void mouseMoved(MouseEvent e) {
  310. }
  311. @Override
  312. public void mouseClicked(MouseEvent e) {
  313. }
  314. @Override
  315. public void mouseEntered(MouseEvent e) {
  316. }
  317. @Override
  318. public void mouseExited(MouseEvent e) {
  319. }
  320. @Override
  321. public void mousePressed(MouseEvent e) {
  322. if (isElement) {
  323. elementPressed(e);
  324. } else if (isSwitch) {
  325. switchPressed(e);
  326. }
  327. }
  328. /**
  329. * When a point of a Holon Element is pressed.
  330. *
  331. * @param e MouseEvent
  332. */
  333. public void elementPressed(MouseEvent e) {
  334. boolean added = false;
  335. boolean deletePoint = false;
  336. int x = (int) ((e.getX() - border) / scaleX);
  337. double y = (e.getY() - border) / scaleY;
  338. // Click on Point
  339. tempP = null;
  340. if (pointList != null) {
  341. // look if a point was clicked
  342. for (Point p : pointList) {
  343. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  344. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  345. if (e.getButton() == MouseEvent.BUTTON3) {//TODO: test
  346. tempP = p;
  347. deletePoint = true;
  348. } else {
  349. pointDrag = true;
  350. tempP = p;
  351. }
  352. }
  353. }
  354. // New Point
  355. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  356. && e.getX() != this.getWidth() / scaleX) {
  357. for (int i = 0; i < pointList.size(); i++) {
  358. // When a point already exist on this x position
  359. if (x == pointList.get(i).getX() || x == width || x == 0) {
  360. break;
  361. }
  362. if (x < pointList.get(i).getX() && !added) {
  363. if (e.getY() <= border) {
  364. pointList.add(i, new Point((int) (x), 0));
  365. } else {
  366. pointList.add(i, new Point((int) (x), (int) y));
  367. }
  368. added = true;
  369. pointDrag = true;
  370. tempP = pointList.get(i);
  371. }
  372. }
  373. }
  374. // Delete a Point
  375. if (deletePoint && tempP.getX() != 0
  376. && /*(*//*tempP.getX() != this.getWidth() / scaleX || */tempP != pointList.getLast())/*)*/ {
  377. pointList.remove(tempP);
  378. }
  379. repaint();
  380. }
  381. }
  382. /**
  383. * When a point of a Switch is pressed.
  384. *
  385. * @param e MouseEvent
  386. */
  387. public void switchPressed(MouseEvent e) {
  388. boolean added = false;
  389. boolean deletePoint = false;
  390. double x = (e.getX() - border) / scaleX;
  391. e.getY();
  392. // Halbe Iterations Distanz
  393. double dist = (width / (model.getIterations() - 1)) / 2;
  394. // Click on Point
  395. tempP = null;
  396. if (pointList != null) {
  397. for (Point p : pointList) {
  398. if (x >= p.getX() - dist * 2 && x <= p.getX() + dist * 2) {
  399. if (e.getButton() == MouseEvent.BUTTON3) {
  400. tempP = p;
  401. deletePoint = true;
  402. } else {
  403. pointDrag = true;
  404. tempP = p;
  405. }
  406. }
  407. }
  408. // New Point
  409. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
  410. for (int i = 0; i < pointList.size() && !added; i++) {
  411. if (x < pointList.get(i).getX() - dist) {
  412. // double p1, p2 um location der points zu bestimmen
  413. double p1 = pointList.get(i - 1).getX();
  414. double p2 = pointList.get(i).getX();
  415. // Punkte hinzufügen, je nachdem ob true oder false
  416. if (pointList.get(i - 1).getY() != (int) (height / 6)
  417. && pointList.get(i).getY() != (int) (height / 6)) {
  418. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height - height / 6)));
  419. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height / 6)));
  420. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height / 6)));
  421. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height - height / 6)));
  422. added = true;
  423. } else if (pointList.get(i - 1).getY() == (int) (height / 6)
  424. && pointList.get(i).getY() == (int) (height / 6)) {
  425. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height / 6)));
  426. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height - height / 6)));
  427. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height - height / 6)));
  428. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height / 6)));
  429. added = true;
  430. }
  431. }
  432. }
  433. }
  434. // Delete a Point
  435. if (deletePoint && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
  436. int i = pointList.indexOf(tempP);
  437. // If Right, else if Left
  438. if (tempP.getY() == (int) (height / 6) && i < pointList.size() - 1 && i > 0) {
  439. pointList.remove(i);
  440. pointList.remove(i - 1);
  441. pointList.remove(i - 2);
  442. pointList.remove(i - 3);
  443. } else if (tempP.getY() == (int) (height - height / 6)) {
  444. pointList.remove(i + 2);
  445. pointList.remove(i + 1);
  446. pointList.remove(i);
  447. pointList.remove(i - 1);
  448. }
  449. }
  450. repaint();
  451. }
  452. }
  453. @Override
  454. public void mouseReleased(MouseEvent e) {
  455. if (pointDrag) {
  456. pointDrag = false;
  457. tempP = null;
  458. }
  459. /**
  460. * reset the dragInformation.
  461. */
  462. dragInformation = "";
  463. repaint();
  464. }
  465. /**
  466. * When the Component is Resized.
  467. *
  468. * @param e ComponentEvent
  469. */
  470. public void componentResized(ComponentEvent e) {
  471. // Wenn ein anderes Element genommen wird
  472. if (init) {
  473. init = false;
  474. // for scale on the first initialisation
  475. if (width == -1 && height == -1) {
  476. width = this.getWidth() - (border * 2);
  477. height = this.getHeight() - (border * 2);
  478. }
  479. // Scale
  480. scaleX = (this.getWidth() - (border * 2)) / width;
  481. scaleY = (this.getHeight() - (border * 2)) / height;
  482. // set the scroll graph invisible
  483. this.getParent().getParent().setVisible(false);
  484. }
  485. // Scale
  486. scaleX = (this.getWidth() - (border * 2)) / width;
  487. scaleY = (this.getHeight() - (border * 2)) / height;
  488. repaint();
  489. }
  490. @Override
  491. public void componentHidden(ComponentEvent e) {
  492. }
  493. @Override
  494. public void componentMoved(ComponentEvent e) {
  495. }
  496. @Override
  497. public void componentShown(ComponentEvent e) {
  498. }
  499. /**
  500. * Empty the Graph.
  501. */
  502. public void empty() {
  503. pointList = null;
  504. tempElements = null;
  505. current = null;
  506. isSwitch = false;
  507. isElement = false;
  508. repaint();
  509. }
  510. /**
  511. * Resets the Points for the Element.
  512. */
  513. public void reset() {
  514. pointList.removeAll(pointList);
  515. if (isSwitch) {
  516. pointList.addFirst(new Point(-border, (int) (height / 6)));
  517. pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  518. } else {
  519. pointList.addFirst(new Point(0, 0));
  520. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  521. }
  522. repaint();
  523. }
  524. /**
  525. * converts the number to fit the canvas.
  526. *
  527. * @param d the number to convert
  528. * @return the converted number
  529. */
  530. public double convertToCanvasY(float d) {
  531. return (height - (d * (height / maximum)));
  532. }
  533. /**
  534. * converts the number to fit the value.
  535. *
  536. * @param d the number to convert
  537. * @return the converted number
  538. */
  539. public float convertToValueY(double d) {
  540. return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
  541. }
  542. /**
  543. * Visualize the HolonElement on the Graph.
  544. *
  545. * @param selectedElement which should be visualized
  546. */
  547. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  548. //maybe linkedlist better?
  549. //arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
  550. current = selectedElement.get(selectedElement.size()-1);
  551. tempElements=selectedElement;
  552. pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  553. isSwitch = false;
  554. isElement = true;
  555. maximum = getMaximum(selectedElement.get(selectedElement.size() - 1));
  556. // First time clicked on the Element
  557. if (pointList.isEmpty()) {
  558. pointList.addFirst(new Point(0, 0));
  559. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  560. }
  561. repaint();
  562. }
  563. /**
  564. * Visualize the Switch on the Graph.
  565. *
  566. * @param s which should be visualized
  567. */
  568. public void repaintWithNewSwitch(HolonSwitch s) {
  569. //arrayOfBooleans = s.getValueArray();
  570. current=s;
  571. pointList = s.getGraphPoints();
  572. isSwitch = true;
  573. isElement = false;
  574. // First time clicked on the Element
  575. if (pointList.isEmpty()) {
  576. pointList.addFirst(new Point(-border, (int) (height / 6)));
  577. pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  578. }
  579. repaint();
  580. }
  581. /**
  582. * Build a Curve for the Graph.
  583. *
  584. * @param p1 startpoint
  585. * @param p2 endpoint
  586. * @return the CubicCurve2D for the Graph
  587. */
  588. public CubicCurve2D buildCurve(Point p1, Point p2) {
  589. x1 = (int) p1.getX();
  590. y1 = (int) p1.getY();
  591. x2 = (int) p2.getX();
  592. y2 = (int) p2.getY();
  593. // calculate the controllpoints
  594. ctrlx1 = x1 + (x2 - x1) / 2;
  595. ctrlx2 = x2 - (x2 - x1) / 2;
  596. if (y1 < y2) {
  597. ctrly1 = y1 + (y2 - y1) / 10;
  598. ctrly2 = y2 - (y2 - y1) / 10;
  599. } else {
  600. ctrly1 = y1 - (y1 - y2) / 10;
  601. ctrly2 = y2 + (y1 - y2) / 10;
  602. }
  603. // set the curve
  604. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  605. x2 * scaleX, y2 * scaleY);
  606. return c;
  607. }
  608. /**
  609. * Fills the Arrays with booleans.
  610. */
  611. public void fillArrayofBooleans() {
  612. for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {
  613. int t = (int) getYValueAt((int) (i * width / (STANDARD_GRAPH_ACCURACY - 1)));
  614. if (t <= height / 2) {
  615. ((HolonSwitch)current).setActiveAt(i, true);
  616. } else {
  617. ((HolonSwitch)current).setActiveAt(i, false);
  618. }
  619. }
  620. }
  621. /**
  622. * Fills the Arrays of each HolonElement.
  623. */
  624. @SuppressWarnings("unchecked")
  625. public void fillArrayofValue() {
  626. for (HolonElement he : tempElements) {
  627. maximum = getMaximum(he);
  628. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  629. //foreach(Point p: pointList)
  630. System.out.println("------------");
  631. System.out.println("pointList[");
  632. for(Point p: pointList)
  633. {
  634. System.out.println(p);
  635. }
  636. System.out.println("]");
  637. for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {//!!!!!
  638. he.setAvailableEnergyPerElementAt(i, convertToValueY(getYValueAt2((int) (i * width / (100 - 1)))));
  639. }
  640. he.setTestGraphPoints(convertUnitGraphToHolonElemntPointList(pointList));
  641. System.out.println("TestgraphPoints:");
  642. he.testFunctiongetAvailableEnergyAt(0);
  643. LinkedList<Point> testUnitGraph = this.convertHolonElementPointListToUnitGraph(he.getTestGraphPoints());
  644. System.out.println("TestUnitGraph[");
  645. for(Point p: testUnitGraph)
  646. {
  647. System.out.println(p);
  648. }
  649. System.out.println("NochmalChecknachGraohPoint");
  650. LinkedList<Point> testGraphPoint = this.convertUnitGraphToHolonElemntPointList(testUnitGraph);
  651. for(Point p: testGraphPoint)
  652. {
  653. System.out.println(p);
  654. }
  655. }
  656. }
  657. /**
  658. * Convert the graph widget point to a pointlist from a holon element
  659. * @param unitgraph
  660. */
  661. public LinkedList<Point> convertUnitGraphToHolonElemntPointList(LinkedList<Point> unitgraph)
  662. {
  663. LinkedList<Point> graphpoints = new LinkedList<Point>();
  664. if(width == 0 || height == 0) return graphpoints;
  665. for(Point p: unitgraph)
  666. {
  667. //CalcX
  668. int x = (int )((p.getX() / width)* 100.0);
  669. //CalcY
  670. int y = (int )((1.0-(p.getY() / height))* 100.0); //1.0- because 0 is at the top, width is at the bottom
  671. //AddPoint
  672. graphpoints.add(new Point(x, y));
  673. }
  674. return graphpoints;
  675. }
  676. public LinkedList<Point> convertHolonElementPointListToUnitGraph(LinkedList<Point> graphPoints)
  677. {
  678. LinkedList<Point> unitgraph = new LinkedList<Point>();
  679. for(Point p: graphPoints)
  680. {
  681. //CalcX
  682. int x = (int )((p.getX() / 100.0)* width);
  683. //CalcY
  684. int y = (int )((1.0-(p.getY() / 100.0))* height); //1.0- because 0 is at the top, width is at the bottom
  685. //AddPoint
  686. unitgraph.add(new Point(x, y));
  687. }
  688. return unitgraph;
  689. }
  690. /**
  691. * Get the Y Value at the x Coordination.
  692. *
  693. * @param xVal the x value for the y value
  694. * @return y, the value at x
  695. */
  696. public float getYValueAt(int xVal) {
  697. for (int i = 0; i < pointList.size() - 1; i++) {
  698. // get the Points
  699. if (xVal <= pointList.get(i + 1).getX()) {
  700. // Curve erstellen
  701. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  702. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  703. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  704. return getIntersectionPoint(l1, l2);
  705. }
  706. }
  707. return 0;
  708. }
  709. /**
  710. * Get y value at the x Coordination via curves.
  711. *
  712. * @param xVal the x value for the y value
  713. * @return y value at x
  714. */
  715. public float getYValueAt2(int xVal) {
  716. for (int i = 0; i < pointList.size() - 1; i++) {
  717. // get the Points
  718. if (xVal >= pointList.get(i).getX()) {
  719. // Curve erstellen
  720. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  721. c.subdivide(cl, cr);
  722. // Teil der Kurve aussuchen
  723. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  724. c = cl;
  725. // Kurve Links von "unten"
  726. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  727. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  728. if (c.contains(xVal * scaleX, j * scaleY)) {
  729. return (float) (j);
  730. }
  731. }
  732. } else {// Kurve Links von "oben"
  733. for (float j = 0; j < height; j += 0.1f) {
  734. if (c.contains(xVal * scaleX, j * scaleY)) {
  735. return (float) (j);
  736. }
  737. }
  738. }
  739. } else {
  740. c = cr;
  741. // Kurve Links von "unten"
  742. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  743. for (float j = 0; j < height; j += 0.1f) {
  744. if (c.contains(xVal * scaleX, j * scaleY)) {
  745. return (float) (j);
  746. }
  747. }
  748. } else {// Kurve Links von "oben"
  749. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  750. if (c.contains(xVal * scaleX, j * scaleY)) {
  751. return (float) (j);
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. // else
  759. return getYValueAt(xVal);
  760. }
  761. /**
  762. * Get the Intersection Point of 2 Lines.
  763. *
  764. * @param l1 the first Line
  765. * @param l2 the second Line
  766. * @return The Intersection Point
  767. */
  768. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  769. if (!l1.intersectsLine(l2)) {
  770. return 0;// null;
  771. }
  772. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  773. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  774. double det = sx * ry - sy * rx;
  775. if (det == 0) {
  776. return 0;// null;
  777. } else {
  778. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  779. if (z < 0 || z > 1) {
  780. return 0;// new Point(0, 0); // intersection at end point!
  781. }
  782. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  783. // (py + z * ry));
  784. }
  785. } // end intersection line-line
  786. public void update(ArrayList<AbstractCpsObject> obj) {
  787. ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
  788. AbstractCpsObject u = null;
  789. queue.addAll(obj);
  790. while (!queue.isEmpty()) {
  791. u = queue.pop();
  792. repaintGraph(u);
  793. }
  794. empty();
  795. if (u instanceof CpsUpperNode)
  796. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  797. queue.add(adjacent);
  798. }
  799. }
  800. void repaintGraph(AbstractCpsObject u) {
  801. ArrayList<HolonElement> list = new ArrayList<>();
  802. if (u instanceof HolonObject) {
  803. for (HolonElement ele : ((HolonObject) u).getElements()) {
  804. list.add(ele);
  805. repaintWithNewElement(list);
  806. fillArrayofValue();
  807. list.remove(0);
  808. }
  809. } else if (u instanceof HolonSwitch) {
  810. repaintWithNewSwitch((HolonSwitch) u);
  811. fillArrayofBooleans();
  812. }
  813. }
  814. float getMaximum(HolonElement ele) {
  815. if (ele.isFlexible()) {
  816. return ele.getFlexibleEnergyAvailablePerElement();
  817. } else {
  818. return ele.getEnergyPerElement();
  819. }
  820. }
  821. /**
  822. * sets the localPeriod of the Current Graph
  823. * @param localPeriod
  824. */
  825. public void setLocalPeriod(int localPeriod){
  826. if(isElement)for(IGraphedElement e:tempElements)e.setLocalPeriod(localPeriod);
  827. else if(isSwitch)current.setLocalPeriod(localPeriod);
  828. }
  829. /**
  830. * gets the LocalPeriod of the CurrentGraph
  831. * @return localPeriod of the current Element or Switch
  832. */
  833. public int getLocalPeriod(){
  834. if(current!=null)return current.getLocalPeriod();
  835. else return model.getGraphIterations();//TODO: maybe rename
  836. }
  837. public boolean isStretching(){
  838. return current.isStretching();
  839. }
  840. public void setStretching(boolean b){
  841. if(isElement)for(IGraphedElement e:tempElements)e.setStretching(b);
  842. else if(isSwitch)current.setStretching(b);
  843. }
  844. static int lv=0;
  845. /**
  846. * Determines the index of the internal value array
  847. * of an element that should be used, since elements only save 100 values,
  848. * but iterations and local period can be anything between 1 and 100000.
  849. *
  850. * @param m the corresponding model.
  851. * @param e the element for which the calculation should be made.
  852. * @param timeStep the iteration for which the calculation should be made.
  853. * @return
  854. */
  855. public static int getEffectiveIndex(Model m, IGraphedElement e, int timeStep){
  856. if(e.isStretching())return timeStep*100/(m==null?STANDARD_GRAPH_ACCURACY:m.getIterations());
  857. else return timeStep%e.getLocalPeriod()*100/e.getLocalPeriod();
  858. }
  859. /**
  860. * Same as getEffectiveIndex(Model, IGraphedElement, int),
  861. * but using the Model obtained from the singleton controller
  862. * to determine the total number of iterations(for "use global").
  863. */
  864. public static int getEffectiveIndex(IGraphedElement e, int timeStep){
  865. return getEffectiveIndex(SingletonControl.getInstance().getControl()==null ? null : SingletonControl.getInstance().getControl().getModel(),e,timeStep);
  866. }
  867. /**
  868. * Same as getEffectiveIndex(Model, IGraphedElement),
  869. * but the current iteration is also obtained from the standard model.
  870. */
  871. public static int getEffectiveIndex(IGraphedElement e){
  872. return getEffectiveIndex(e,SingletonControl.getInstance().getControl().getModel().getCurIteration());
  873. }
  874. }