UnitGraph.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. 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. // array fillen
  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 (int i = 0; i < pointList.size() - 0; i++) {
  114. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2) + border,
  115. (int) (pointList.get(i).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. }
  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 != pointList.getFirst() && tempP != pointList.getLast()) {
  436. int i = pointList.indexOf(tempP);
  437. // If Right, else if Left
  438. if (tempP.getY() == (int) (height / 6) && i < pointList.size() - 1 && i > 0) {
  439. pointList.remove(i);
  440. pointList.remove(i - 1);
  441. pointList.remove(i - 2);
  442. pointList.remove(i - 3);
  443. } else if (tempP.getY() == (int) (height - height / 6)) {
  444. pointList.remove(i + 2);
  445. pointList.remove(i + 1);
  446. pointList.remove(i);
  447. pointList.remove(i - 1);
  448. }
  449. }
  450. repaint();
  451. }
  452. }
  453. @Override
  454. public void mouseReleased(MouseEvent e) {
  455. if (pointDrag) {
  456. pointDrag = false;
  457. tempP = null;
  458. }
  459. /**
  460. * reset the dragInformation.
  461. */
  462. dragInformation = "";
  463. repaint();
  464. }
  465. /**
  466. * When the Component is Resized.
  467. *
  468. * @param e
  469. * ComponentEvent
  470. */
  471. public void componentResized(ComponentEvent e) {
  472. // Wenn ein anderes Element genommen wird
  473. if (init) {
  474. init = false;
  475. // for scale on the first initialisation
  476. if (width == -1 && height == -1) {
  477. width = this.getWidth() - (border * 2);
  478. height = this.getHeight() - (border * 2);
  479. }
  480. // Scale
  481. scaleX = (this.getWidth() - (border * 2)) / width;
  482. scaleY = (this.getHeight() - (border * 2)) / height;
  483. // set the scroll graph invisible
  484. this.getParent().getParent().setVisible(false);
  485. }
  486. // Scale
  487. scaleX = (this.getWidth() - (border * 2)) / width;
  488. scaleY = (this.getHeight() - (border * 2)) / height;
  489. repaint();
  490. }
  491. @Override
  492. public void componentHidden(ComponentEvent e) {
  493. }
  494. @Override
  495. public void componentMoved(ComponentEvent e) {
  496. }
  497. @Override
  498. public void componentShown(ComponentEvent e) {
  499. }
  500. /**
  501. * Empty the Graph.
  502. */
  503. public void empty() {
  504. pointList = null;
  505. tempElements = null;
  506. arrayOfFloats = null;
  507. arrayOfBooleans = null;
  508. isSwitch = false;
  509. isElement = false;
  510. repaint();
  511. }
  512. /**
  513. * Resets the Points for the Element.
  514. */
  515. public void reset() {
  516. pointList.removeAll(pointList);
  517. if (isSwitch) {
  518. pointList.addFirst(new Point(-border, (int) (height / 6)));
  519. pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  520. } else {
  521. pointList.addFirst(new Point(0, 0));
  522. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  523. }
  524. repaint();
  525. }
  526. /**
  527. * converts the number to fit the canvas.
  528. *
  529. * @param d
  530. * the number to convert
  531. * @return the converted number
  532. */
  533. public double convertToCanvasY(float d) {
  534. return (height - (d * (height / maximum)));
  535. }
  536. /**
  537. * converts the number to fit the value.
  538. *
  539. * @param d
  540. * the number to convert
  541. * @return the converted number
  542. */
  543. public float convertToValueY(double d) {
  544. return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
  545. }
  546. /**
  547. * Visualize the HolonElement on the Graph.
  548. *
  549. * @param selectedElement
  550. * which should be visualized
  551. */
  552. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  553. arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
  554. tempElements = selectedElement;
  555. pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  556. isSwitch = false;
  557. isElement = true;
  558. maximum = getMaximum(selectedElement.get(selectedElement.size() - 1));
  559. // First time clicked on the Element
  560. if (pointList.isEmpty()) {
  561. pointList.addFirst(new Point(0, 0));
  562. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  563. }
  564. repaint();
  565. }
  566. /**
  567. * Visualize the Switch on the Graph.
  568. *
  569. * @param s
  570. * which should be visualized
  571. */
  572. public void repaintWithNewSwitch(HolonSwitch s) {
  573. arrayOfBooleans = s.getActiveAt();
  574. pointList = s.getGraphPoints();
  575. isSwitch = true;
  576. isElement = false;
  577. // First time clicked on the Element
  578. if (pointList.isEmpty()) {
  579. pointList.addFirst(new Point(-border, (int) (height / 6)));
  580. pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  581. }
  582. repaint();
  583. }
  584. /**
  585. * Build a Curve for the Graph.
  586. *
  587. * @param p1
  588. * startpoint
  589. * @param p2
  590. * endpoint
  591. *
  592. * @return the CubicCurve2D for the Graph
  593. */
  594. public CubicCurve2D buildCurve(Point p1, Point p2) {
  595. x1 = (int) p1.getX();
  596. y1 = (int) p1.getY();
  597. x2 = (int) p2.getX();
  598. y2 = (int) p2.getY();
  599. // calculate the controllpoints
  600. ctrlx1 = x1 + (x2 - x1) / 2;
  601. ctrlx2 = x2 - (x2 - x1) / 2;
  602. if (y1 < y2) {
  603. ctrly1 = y1 + (y2 - y1) / 10;
  604. ctrly2 = y2 - (y2 - y1) / 10;
  605. } else {
  606. ctrly1 = y1 - (y1 - y2) / 10;
  607. ctrly2 = y2 + (y1 - y2) / 10;
  608. }
  609. // set the curve
  610. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  611. x2 * scaleX, y2 * scaleY);
  612. return c;
  613. }
  614. /**
  615. * Fills the Arrays with booleans.
  616. */
  617. public void fillArrayofBooleans() {
  618. for (int i = 0; i < arrayOfBooleans.length; i++) {
  619. int t = (int) getYValueAt((int) (i * width / (model.getIterations() - 1)));
  620. if (t <= height / 2) {
  621. arrayOfBooleans[i] = true;
  622. } else {
  623. arrayOfBooleans[i] = false;
  624. }
  625. }
  626. }
  627. /**
  628. * Fills the Arrays of each HolonElement.
  629. */
  630. @SuppressWarnings("unchecked")
  631. public void fillArrayofValue() {
  632. for (HolonElement he : tempElements) {
  633. maximum = getMaximum(he);
  634. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  635. for (int i = 0; i < arrayOfFloats.length; i++) {
  636. he.getAvailableEnergyPerElementAt()[i] = convertToValueY(getYValueAt2((int) (i * width / (model.getIterations() - 1))));
  637. }
  638. arrayOfFloats = he.getAvailableEnergyPerElementAt();
  639. }
  640. }
  641. /**
  642. * Get the Y Value at the x Coordination.
  643. *
  644. * @param xVal
  645. * the x value for the y value
  646. * @return y, the value at x
  647. */
  648. public float getYValueAt(int xVal) {
  649. for (int i = 0; i < pointList.size() - 1; i++) {
  650. // get the Points
  651. if (xVal <= pointList.get(i + 1).getX()) {
  652. // Curve erstellen
  653. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  654. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  655. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  656. return getIntersectionPoint(l1, l2);
  657. }
  658. }
  659. return 0;
  660. }
  661. /**
  662. * Get y value at the x Coordination via curves.
  663. *
  664. * @param xVal
  665. * the x value for the y value
  666. * @return y value at x
  667. */
  668. public float getYValueAt2(int xVal) {
  669. for (int i = 0; i < pointList.size() - 1; i++) {
  670. // get the Points
  671. if (xVal >= pointList.get(i).getX()) {
  672. // Curve erstellen
  673. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  674. c.subdivide(cl, cr);
  675. // Teil der Kurve aussuchen
  676. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  677. c = cl;
  678. // Kurve Links von "unten"
  679. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  680. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  681. if (c.contains(xVal * scaleX, j * scaleY)) {
  682. return (float) (j);
  683. }
  684. }
  685. } else {// Kurve Links von "oben"
  686. for (float j = 0; j < height; j += 0.1f) {
  687. if (c.contains(xVal * scaleX, j * scaleY)) {
  688. return (float) (j);
  689. }
  690. }
  691. }
  692. } else {
  693. c = cr;
  694. // Kurve Links von "unten"
  695. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  696. for (float j = 0; j < height; j += 0.1f) {
  697. if (c.contains(xVal * scaleX, j * scaleY)) {
  698. return (float) (j);
  699. }
  700. }
  701. } else {// Kurve Links von "oben"
  702. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  703. if (c.contains(xVal * scaleX, j * scaleY)) {
  704. return (float) (j);
  705. }
  706. }
  707. }
  708. }
  709. }
  710. }
  711. // else
  712. return getYValueAt(xVal);
  713. }
  714. /**
  715. * Get the Intersection Point of 2 Lines.
  716. *
  717. * @param l1
  718. * the first Line
  719. * @param l2
  720. * the second Line
  721. *
  722. * @return The Intersection Point
  723. */
  724. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  725. if (!l1.intersectsLine(l2)) {
  726. return 0;// null;
  727. }
  728. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  729. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  730. double det = sx * ry - sy * rx;
  731. if (det == 0) {
  732. return 0;// null;
  733. } else {
  734. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  735. if (z < 0 || z > 1) {
  736. return 0;// new Point(0, 0); // intersection at end point!
  737. }
  738. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  739. // (py + z * ry));
  740. }
  741. } // end intersection line-line
  742. public void update(ArrayList<AbstractCpsObject> obj) {
  743. ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
  744. AbstractCpsObject u = null;
  745. queue.addAll(obj);
  746. while (!queue.isEmpty()) {
  747. u = queue.pop();
  748. repaintGraph(u);
  749. }
  750. empty();
  751. if (u instanceof CpsUpperNode)
  752. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  753. queue.add(adjacent);
  754. }
  755. }
  756. void repaintGraph(AbstractCpsObject u) {
  757. ArrayList<HolonElement> list = new ArrayList<>();
  758. if (u instanceof HolonObject) {
  759. for (HolonElement ele : ((HolonObject) u).getElements()) {
  760. list.add(ele);
  761. repaintWithNewElement(list);
  762. fillArrayofValue();
  763. list.remove(0);
  764. }
  765. } else if (u instanceof HolonSwitch) {
  766. repaintWithNewSwitch((HolonSwitch) u);
  767. fillArrayofBooleans();
  768. }
  769. }
  770. float getMaximum(HolonElement ele) {
  771. if (ele.isFlexible()) {
  772. return ele.getFlexibleEnergyAvailablePerElement();
  773. } else {
  774. return ele.getEnergyPerElement();
  775. }
  776. }
  777. }