UnitGraph.java 26 KB

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