UnitGraph.java 32 KB

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