UnitGraph.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. line = new Line2D.Double(pointList.get(i).getX() * scaleX + border,
  178. pointList.get(i).getY() * scaleY, pointList.get(i + 1).getX() * scaleX + border,
  179. pointList.get(i + 1).getY() * scaleY);
  180. graphCurve.append(line, true);
  181. }
  182. g2.draw(graphCurve);
  183. /*
  184. * // Draw the Points g2.setColor(Color.BLUE); for (int i = 0; i
  185. * < pointList.size() - 0; i++) { g2.fillOval((int)
  186. * (pointList.get(i).getX() * scaleX - recSize.getX() / 2) +
  187. * border, (int) (pointList.get(i).getY() * scaleY -
  188. * recSize.getY() / 2), (int) recSize.getX(), (int)
  189. * recSize.getY()); }
  190. */
  191. // Iteration Value
  192. g2.setColor(Color.BLUE);
  193. textWidth = g.getFontMetrics().stringWidth("" + arrayOfBooleans[model.getCurIteration()]) + 2;
  194. if (textWidth
  195. + (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1) + 2
  196. + border <= this.getWidth()) {
  197. g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
  198. (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
  199. + 2 + border,
  200. this.getHeight() - 10);
  201. } else {
  202. g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
  203. (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
  204. + border - textWidth,
  205. this.getHeight() - 10);
  206. }
  207. }
  208. // When the switch graph is dragged
  209. if (tempP != null && pointDrag) {
  210. try {
  211. int i;
  212. for (i = 0; (i * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
  213. + border < getMousePosition().getX()); i++) {
  214. }
  215. dragInformation = "" + i;
  216. textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX())))
  217. + 2;
  218. if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
  219. g2.drawString(dragInformation, (int) (getMousePosition().getX()) + 10 + border,
  220. (int) (getMousePosition().getY() * scaleY) + 10);
  221. } else {
  222. g2.drawString(dragInformation, (int) (getMousePosition().getX()) - textWidth,
  223. (int) (getMousePosition().getY() * scaleY) + 10);
  224. }
  225. } catch (Exception e) {
  226. }
  227. }
  228. }
  229. // Iteration Line
  230. g2.setColor(Color.BLUE);
  231. g2.setStroke(new BasicStroke(1));
  232. g2.drawLine(border + (model.getCurIteration()) * (this.getWidth() - border * 2) / (model.getIterations() - 1),
  233. 0, border + (model.getCurIteration()) * (this.getWidth() - border * 2) / (model.getIterations() - 1),
  234. this.getHeight());
  235. // algorithmus
  236. controller.calculateStateForTimeStep(model.getCurIteration());
  237. }
  238. @Override
  239. public void mouseDragged(MouseEvent e) {
  240. if (isElement) {
  241. elementDragged(e);
  242. } else if (isSwitch) {
  243. switchDragged(e);
  244. }
  245. }
  246. /**
  247. * When a Point of a Holon Element is dragged.
  248. *
  249. * @param e
  250. * MouseEvent
  251. */
  252. public void elementDragged(MouseEvent e) {
  253. if (pointDrag && tempP != null) {
  254. // Out of Bounds verhindern
  255. int i = pointList.indexOf(tempP);
  256. x = (e.getX() - border) / scaleX;
  257. y = (e.getY() - border) / scaleY;
  258. // y
  259. if (e.getY() <= border) {
  260. y = 0;
  261. } else if (this.getHeight() - border <= e.getY()) {
  262. y = (this.getHeight() - border * 2) / scaleY;
  263. }
  264. // x
  265. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x + 2
  266. || pointList.get(i - 1).getX() > x - 2 || pointList.getFirst().getX() > x - 2
  267. || pointList.getLast().getX() < x + 2) {
  268. x = tempP.getX();
  269. }
  270. tempP.setLocation(x, y);
  271. repaint();
  272. }
  273. }
  274. /**
  275. * When a Point of a switch is dragged.
  276. *
  277. * @param e
  278. * MouseEvent
  279. */
  280. public void switchDragged(MouseEvent e) {
  281. if (pointDrag && tempP != null && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
  282. int i = pointList.indexOf(tempP);
  283. x = (e.getX() - border) / scaleX;
  284. if (pointList.get(i + 1).getY() == tempP.getY()) {
  285. // x
  286. if (pointList.get(i + 1).getX() <= x + 1 || pointList.get(i - 2).getX() >= x - 1) {
  287. x = tempP.getX();
  288. }
  289. pointList.get(i - 1).setLocation(x, pointList.get(i - 1).getY());
  290. } else {
  291. // x
  292. if (pointList.get(i + 2).getX() <= x + 1 || pointList.get(i - 1).getX() >= x - 1) {
  293. x = tempP.getX();
  294. }
  295. pointList.get(i + 1).setLocation(x, pointList.get(i + 1).getY());
  296. }
  297. tempP.setLocation(x, tempP.getY());
  298. repaint();
  299. }
  300. }
  301. @Override
  302. public void mouseMoved(MouseEvent e) {
  303. }
  304. @Override
  305. public void mouseClicked(MouseEvent e) {
  306. }
  307. @Override
  308. public void mouseEntered(MouseEvent e) {
  309. }
  310. @Override
  311. public void mouseExited(MouseEvent e) {
  312. }
  313. @Override
  314. public void mousePressed(MouseEvent e) {
  315. if (isElement) {
  316. elementPressed(e);
  317. } else if (isSwitch) {
  318. switchPressed(e);
  319. }
  320. }
  321. /**
  322. * When a point of a Holon Element is pressed.
  323. *
  324. * @param e
  325. * MouseEvent
  326. */
  327. public void elementPressed(MouseEvent e) {
  328. boolean added = false;
  329. boolean deletePoint = false;
  330. int x = (int) ((e.getX() - border) / scaleX);
  331. double y = (e.getY() - border) / scaleY;
  332. // Click on Point
  333. tempP = null;
  334. if (pointList != null) {
  335. // look if a point was clicked
  336. for (Point p : pointList) {
  337. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  338. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  339. if (e.getButton() == MouseEvent.BUTTON3) {
  340. tempP = p;
  341. deletePoint = true;
  342. } else {
  343. pointDrag = true;
  344. tempP = p;
  345. }
  346. }
  347. }
  348. // New Point
  349. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  350. && e.getX() != this.getWidth() / scaleX) {
  351. for (int i = 0; i < pointList.size(); i++) {
  352. // When a point already exist on this x position
  353. if (x == pointList.get(i).getX() || x == width || x == 0) {
  354. break;
  355. }
  356. if (x < pointList.get(i).getX() && !added) {
  357. if (e.getY() <= border) {
  358. pointList.add(i, new Point((int) (x), 0));
  359. } else {
  360. pointList.add(i, new Point((int) (x), (int) y));
  361. }
  362. added = true;
  363. pointDrag = true;
  364. tempP = pointList.get(i);
  365. }
  366. }
  367. }
  368. // Delete a Point
  369. if (deletePoint && tempP.getX() != 0
  370. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  371. pointList.remove(tempP);
  372. }
  373. repaint();
  374. }
  375. }
  376. /**
  377. * When a point of a Switch is pressed.
  378. *
  379. * @param e
  380. * MouseEvent
  381. */
  382. public void switchPressed(MouseEvent e) {
  383. boolean added = false;
  384. boolean deletePoint = false;
  385. double x = (e.getX() - border) / scaleX;
  386. e.getY();
  387. // Halbe Iterations Distanz
  388. double dist = (width / (model.getIterations() - 1)) / 2;
  389. // Click on Point
  390. tempP = null;
  391. if (pointList != null) {
  392. for (Point p : pointList) {
  393. if (x >= p.getX() - dist * 2 && x <= p.getX() + dist * 2) {
  394. if (e.getButton() == MouseEvent.BUTTON3) {
  395. tempP = p;
  396. deletePoint = true;
  397. } else {
  398. pointDrag = true;
  399. tempP = p;
  400. }
  401. }
  402. }
  403. // New Point
  404. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
  405. for (int i = 0; i < pointList.size() && !added; i++) {
  406. if (x < pointList.get(i).getX() - dist) {
  407. // double p1, p2 um location der points zu bestimmen
  408. double p1 = pointList.get(i - 1).getX();
  409. double p2 = pointList.get(i).getX();
  410. // Punkte hinzufügen, je nachdem ob true oder false
  411. if (pointList.get(i - 1).getY() != (int) (height / 6)
  412. && pointList.get(i).getY() != (int) (height / 6)) {
  413. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height - height / 6)));
  414. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height / 6)));
  415. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height / 6)));
  416. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height - height / 6)));
  417. added = true;
  418. } else if (pointList.get(i - 1).getY() == (int) (height / 6)
  419. && pointList.get(i).getY() == (int) (height / 6)) {
  420. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height / 6)));
  421. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height - height / 6)));
  422. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height - height / 6)));
  423. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height / 6)));
  424. added = true;
  425. }
  426. }
  427. }
  428. }
  429. // Delete a Point
  430. if (deletePoint && tempP.getX() != 0
  431. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  432. int i = pointList.indexOf(tempP);
  433. //If Right, else if Left
  434. if (tempP.getY() == (int) (height / 6) && i < pointList.size() - 1 && i>0) {
  435. pointList.remove(i);
  436. pointList.remove(i - 1);
  437. pointList.remove(i - 2);
  438. pointList.remove(i - 3);
  439. } else if (tempP.getY() == (int) (height - height / 6)) {
  440. pointList.remove(i + 2);
  441. pointList.remove(i + 1);
  442. pointList.remove(i);
  443. pointList.remove(i - 1);
  444. }
  445. }
  446. repaint();
  447. }
  448. }
  449. @Override
  450. public void mouseReleased(MouseEvent e) {
  451. if (pointDrag) {
  452. pointDrag = false;
  453. tempP = null;
  454. }
  455. /**
  456. * reset the dragInformation.
  457. */
  458. dragInformation = "";
  459. repaint();
  460. }
  461. /**
  462. * When the Component is Resized.
  463. *
  464. * @param e
  465. * ComponentEvent
  466. */
  467. public void componentResized(ComponentEvent e) {
  468. // Wenn ein anderes Element genommen wird
  469. if (init) {
  470. init = false;
  471. // for scale on the first initialisation
  472. if (width == -1 && height == -1) {
  473. width = this.getWidth() - (border * 2);
  474. height = this.getHeight() - (border * 2);
  475. }
  476. // Scale
  477. scaleX = (this.getWidth() - (border * 2)) / width;
  478. scaleY = (this.getHeight() - (border * 2)) / height;
  479. }
  480. // Scale
  481. scaleX = (this.getWidth() - (border * 2)) / width;
  482. scaleY = (this.getHeight() - (border * 2)) / height;
  483. repaint();
  484. }
  485. @Override
  486. public void componentHidden(ComponentEvent e) {
  487. }
  488. @Override
  489. public void componentMoved(ComponentEvent e) {
  490. }
  491. @Override
  492. public void componentShown(ComponentEvent e) {
  493. }
  494. /**
  495. * Empty the Graph.
  496. */
  497. public void empty() {
  498. pointList = null;
  499. tempElements = null;
  500. arrayOfFloats = null;
  501. arrayOfBooleans = null;
  502. isSwitch = false;
  503. isElement = false;
  504. repaint();
  505. }
  506. /**
  507. * Resets the Points for the Element.
  508. */
  509. public void reset() {
  510. pointList.removeAll(pointList);
  511. if (isSwitch) {
  512. pointList.addFirst(new Point(-border, (int) (height / 6)));
  513. pointList.addLast(new Point((int) (width) + 4, (int) (height / 6)));
  514. } else {
  515. pointList.addFirst(new Point(0, 0));
  516. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  517. }
  518. repaint();
  519. }
  520. /**
  521. * converts the number to fit the canvas.
  522. *
  523. * @param d
  524. * the number to convert
  525. * @return the converted number
  526. */
  527. public double convertToCanvasY(float d) {
  528. return (height - (d * (height / maximum)));
  529. }
  530. /**
  531. * converts the number to fit the value.
  532. *
  533. * @param d
  534. * the number to convert
  535. * @return the converted number
  536. */
  537. public float convertToValueY(double d) {
  538. return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
  539. }
  540. /**
  541. * Visualize the HolonElement on the Graph.
  542. *
  543. * @param selectedElement
  544. * which should be visualized
  545. */
  546. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  547. arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getEnergyAt();
  548. tempElements = selectedElement;
  549. pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  550. isSwitch = false;
  551. isElement = true;
  552. maximum = selectedElement.get(selectedElement.size() - 1).getEnergy();
  553. // First time clicked on the Element
  554. if (pointList.isEmpty()) {
  555. pointList.addFirst(new Point(0, 0));
  556. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  557. }
  558. repaint();
  559. }
  560. /**
  561. * Visualize the Switch on the Graph.
  562. *
  563. * @param s
  564. * which should be visualized
  565. */
  566. public void repaintWithNewSwitch(HolonSwitch s) {
  567. arrayOfBooleans = s.getActiveAt();
  568. pointList = s.getGraphPoints();
  569. isSwitch = true;
  570. isElement = false;
  571. // First time clicked on the Element
  572. if (pointList.isEmpty()) {
  573. pointList.addFirst(new Point(-border, (int) (height / 6)));
  574. pointList.addLast(new Point((int) ((this.getWidth() - border) / scaleX), (int) (height / 6)));
  575. }
  576. repaint();
  577. }
  578. /**
  579. * Build a Curve for the Graph.
  580. *
  581. * @param p1
  582. * startpoint
  583. * @param p2
  584. * endpoint
  585. *
  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 < arrayOfBooleans.length; i++) {
  613. int t = (int) getYValueAt2((int) (i * width / (model.getIterations() - 1)));
  614. if (t <= height / 2) {
  615. arrayOfBooleans[i] = true;
  616. } else {
  617. arrayOfBooleans[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 = he.getEnergy();
  628. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  629. for (int i = 0; i < arrayOfFloats.length; i++) {
  630. he.getEnergyAt()[i] = convertToValueY(getYValueAt2((int) (i * width / (model.getIterations() - 1))));
  631. }
  632. arrayOfFloats = he.getEnergyAt();
  633. }
  634. }
  635. /**
  636. * Get the Y Value at the x Coordination.
  637. *
  638. * @param xVal
  639. * the x value for the y value
  640. * @return y, the value at x
  641. */
  642. public float getYValueAt(int xVal) {
  643. for (int i = 0; i < pointList.size() - 1; i++) {
  644. // get the Points
  645. if (xVal <= pointList.get(i + 1).getX()) {
  646. // Curve erstellen
  647. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  648. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  649. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  650. return getIntersectionPoint(l1, l2);
  651. }
  652. }
  653. return 0;
  654. }
  655. /**
  656. * Get y value at the x Coordination via curves.
  657. *
  658. * @param xVal
  659. * the x value for the y value
  660. * @return y value at x
  661. */
  662. public float getYValueAt2(int xVal) {
  663. for (int i = 0; i < pointList.size() - 1; i++) {
  664. // get the Points
  665. if (xVal >= pointList.get(i).getX()) {
  666. // Curve erstellen
  667. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  668. c.subdivide(cl, cr);
  669. // Teil der Kurve aussuchen
  670. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  671. c = cl;
  672. // Kurve Links von "unten"
  673. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  674. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  675. if (c.contains(xVal * scaleX, j * scaleY)) {
  676. return (float) (j);
  677. }
  678. }
  679. } else {// Kurve Links von "oben"
  680. for (float j = 0; j < height; j += 0.1f) {
  681. if (c.contains(xVal * scaleX, j * scaleY)) {
  682. return (float) (j);
  683. }
  684. }
  685. }
  686. } else {
  687. c = cr;
  688. // Kurve Links von "unten"
  689. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  690. for (float j = 0; j < height; j += 0.1f) {
  691. if (c.contains(xVal * scaleX, j * scaleY)) {
  692. return (float) (j);
  693. }
  694. }
  695. } else {// Kurve Links von "oben"
  696. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  697. if (c.contains(xVal * scaleX, j * scaleY)) {
  698. return (float) (j);
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. // else
  706. return getYValueAt(xVal);
  707. }
  708. /**
  709. * Get the Intersection Point of 2 Lines.
  710. *
  711. * @param l1
  712. * the first Line
  713. * @param l2
  714. * the second Line
  715. *
  716. * @return The Intersection Point
  717. */
  718. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  719. if (!l1.intersectsLine(l2)) {
  720. return 0;// null;
  721. }
  722. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  723. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  724. double det = sx * ry - sy * rx;
  725. if (det == 0) {
  726. return 0;// null;
  727. } else {
  728. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  729. if (z < 0 || z > 1) {
  730. return 0;// new Point(0, 0); // intersection at end point!
  731. }
  732. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  733. // (py + z * ry));
  734. }
  735. } // end intersection line-line
  736. public void update(ArrayList<AbstractCpsObject> obj) {
  737. ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
  738. ArrayList<HolonElement> list = new ArrayList<>();
  739. AbstractCpsObject u = null;
  740. queue.addAll(obj);
  741. while (!queue.isEmpty()) {
  742. u = queue.pop();
  743. if (u instanceof HolonObject) {
  744. for (HolonElement ele : ((HolonObject) u).getElements()) {
  745. list.add(ele);
  746. repaintWithNewElement(list);
  747. fillArrayofValue();
  748. list.remove(0);
  749. }
  750. } else if (u instanceof HolonSwitch) {
  751. repaintWithNewSwitch((HolonSwitch) u);
  752. fillArrayofBooleans();
  753. }
  754. }
  755. empty();
  756. if (u instanceof CpsUpperNode)
  757. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  758. queue.add(adjacent);
  759. }
  760. }
  761. }