UnitGraph.java 25 KB

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