UnitGraph.java 22 KB

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