UnitGraph.java 26 KB

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