UnitGraph.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. package ui.view;
  2. import classes.*;
  3. import ui.controller.Control;
  4. import ui.controller.SingletonControl;
  5. import ui.model.Model;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.awt.geom.CubicCurve2D;
  10. import java.awt.geom.GeneralPath;
  11. import java.awt.geom.Line2D;
  12. import java.util.ArrayDeque;
  13. import java.util.ArrayList;
  14. import java.util.LinkedList;
  15. /**
  16. * This Class represents a Graph where the User can model the behavior of
  17. * elements and switches over time.
  18. *
  19. * @author Gruppe14
  20. */
  21. public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  22. private static final long serialVersionUID = 1L;
  23. public static final int STANDARD_GRAPH_ACCURACY = 100;
  24. private GeneralPath graphCurve = new GeneralPath();
  25. private float maximum = 0;
  26. // Information shown when a Point is Dragged
  27. private String dragInformation = "";
  28. // Points
  29. private Point recSize = new Point(8, 8); // Point Size
  30. private Graphics2D g2;
  31. private CubicCurve2D c = new CubicCurve2D.Double();
  32. private CubicCurve2D cr = new CubicCurve2D.Double();
  33. private CubicCurve2D cl = new CubicCurve2D.Double();
  34. private LinkedList<Point> pointList;
  35. // Scale for the Graph
  36. private double scaleX;
  37. private double scaleY;
  38. //private float[] arrayOfFloats = null;
  39. //private boolean[] arrayOfBooleans = null;
  40. private double width = -1;
  41. private double height = -1;
  42. private boolean isElement = false;
  43. private boolean isSwitch = false;
  44. private ArrayList<HolonElement> tempElements = new ArrayList<>();
  45. private Model model;
  46. private Control controller;
  47. private Line2D.Double line = null;
  48. private boolean pointDrag = false;
  49. private boolean init = true;
  50. private Point tempP = null;
  51. private double x = 0, y = 0;
  52. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  53. private int border = 4;
  54. private int textWidth = 0;
  55. private IGraphedElement current;
  56. /**
  57. * Constructor.
  58. *
  59. * @param model the Model
  60. * @param control the Controller
  61. */
  62. public UnitGraph(final Model model, Control control) {
  63. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  64. this.controller = control;
  65. this.model = model;
  66. this.pointList = new LinkedList<>();
  67. this.setBackground(Color.WHITE);
  68. this.addMouseListener(this);
  69. this.addMouseMotionListener(this);
  70. this.addComponentListener(this);
  71. }
  72. /**
  73. * Paints all Components on the Canvas.
  74. *
  75. * @param g Graphics
  76. */
  77. public void paintComponent(Graphics g) {
  78. super.paintComponent(g);
  79. g2 = (Graphics2D) g;
  80. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  81. g2.setRenderingHints(rh);
  82. g2.setStroke(new BasicStroke(0));
  83. graphCurve.reset();
  84. border = (int) recSize.getX() >> 1;
  85. // Draw the Vertical Lines
  86. g2.setColor(Color.BLACK);
  87. for (int i = border; i < this.getWidth() - border; i += border * 2) {
  88. g2.drawLine(i, border, i, this.getHeight() - border);
  89. }
  90. g2.drawLine(this.getWidth() - border, border, this.getWidth() - border, this.getHeight() - border);
  91. for (int i = border; i < this.getHeight() - border; i += border * 2) {
  92. g2.drawLine(border, i, this.getWidth() - border, i);
  93. }
  94. g2.drawLine(border, this.getHeight() - border, this.getWidth() - border, this.getHeight() - border);
  95. int effectiveX;
  96. if(current!=null)effectiveX=getEffectiveIndex(model, current, model.getCurIteration());
  97. else effectiveX=0;
  98. if (isElement) {
  99. // fill array with values from the pointList in a HolonElement
  100. fillArrayofValue();
  101. if (current != null) {
  102. // Draw the Lines
  103. g2.setStroke(new BasicStroke(2));
  104. g2.setColor(Color.BLACK);
  105. for (int i = 0; i < pointList.size() - 1; i++) {
  106. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  107. c.setCurve((x1 * scaleX) + border, (y1 * scaleY) + border, (ctrlx1 * scaleX) + border,
  108. (ctrly1 * scaleY) + border, (ctrlx2 * scaleX) + border, (ctrly2 * scaleY) + border,
  109. (x2 * scaleX) + border, (y2 * scaleY) + border);
  110. graphCurve.append(c, true);
  111. }
  112. g2.draw(graphCurve);
  113. // Draw the Points
  114. g2.setColor(Color.BLUE);
  115. for (Point aPointList : pointList) {
  116. g2.fillOval((int) (aPointList.getX() * scaleX - recSize.getX() / 2) + border,
  117. (int) (aPointList.getY() * scaleY - recSize.getY() / 2) + border,
  118. (int) recSize.getX(), (int) recSize.getY());
  119. }
  120. // Iteration Value
  121. //TODO: added function getGraphIterations see if it works
  122. textWidth = g.getFontMetrics().stringWidth("" + ((HolonElement)current).getAvailableEnergyAt(model.getCurIteration())/*arrayOfFloats[effectiveX]*/) + 2;
  123. if (textWidth
  124. + (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1) + 2
  125. + border <= this.getWidth()) {
  126. g2.drawString("" + ((HolonElement)current).getAvailableEnergyAt(model.getCurIteration()),
  127. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  128. + 2 + border,
  129. this.getHeight() - 10);
  130. } else {
  131. g2.drawString("" + ((HolonElement)current).getAvailableEnergyAt(model.getCurIteration()),
  132. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  133. + border - textWidth,
  134. this.getHeight() - 10);
  135. }
  136. }
  137. // drag Information
  138. if (tempP != null && pointDrag) {
  139. dragInformation = "" + convertToValueY(getYValueAt((int) tempP.getX()));
  140. textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX()))) + 2;
  141. if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
  142. g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) + 10 + border,
  143. (int) (tempP.getY() * scaleY) + 10);
  144. } else {
  145. g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) - textWidth,
  146. (int) (tempP.getY() * scaleY) + 10);
  147. }
  148. }
  149. /*
  150. * // Actual Iteration Point Visualization g2.setColor(Color.RED);
  151. * if (arrayOfFloats != null) { for (int i = 0; i <
  152. * arrayOfFloats.length; i++) { g2.fillOval((int) (i * width /
  153. * (model.getIterations() - 1) * scaleX - recSize.getX() /
  154. * 2)+border, (int) (convertToCanvasY((int) arrayOfFloats[i]) *
  155. * scaleY - recSize.getY() / 2)+border, (int) recSize.getX(), (int)
  156. * recSize.getY()); } }
  157. */
  158. } else if (isSwitch) {
  159. if (/*arrayOfBooleans*/current != null) {//Technically this test should be unnecessary
  160. // array fillen
  161. fillArrayofBooleans();
  162. // Draw the Lines
  163. g2.setStroke(new BasicStroke(2));
  164. g2.setColor(Color.BLACK);
  165. for (int i = 0; i < pointList.size() - 1; i++) {
  166. // Left out of bounce
  167. if ((i == 1 || i == 2) && pointList.get(i).getX() < 0) {
  168. line = new Line2D.Double(border, pointList.get(i).getY() * scaleY, border,
  169. pointList.get(i + 1).getY() * scaleY);
  170. }
  171. // Right out of bounce
  172. else if (i == pointList.size() - 4 && pointList.get(pointList.size() - 3).getX() > width) {
  173. line = new Line2D.Double(pointList.get(i).getX() * scaleX + border,
  174. pointList.get(i).getY() * scaleY, this.getWidth() - border,
  175. pointList.get(i + 1).getY() * scaleY);
  176. } else if (i == pointList.size() - 3 && pointList.get(pointList.size() - 3).getX() > width) {
  177. line = new Line2D.Double(this.getWidth() - border, pointList.get(i).getY() * scaleY,
  178. this.getWidth() - border, pointList.get(i + 1).getY() * scaleY);
  179. } else if (i == pointList.size() - 2 && pointList.get(pointList.size() - 2).getX() > width) {
  180. line = new Line2D.Double(this.getWidth() - border, pointList.get(i).getY() * scaleY,
  181. pointList.get(i + 1).getX() * scaleX + border, pointList.get(i + 1).getY() * scaleY);
  182. } else {
  183. line = new Line2D.Double(pointList.get(i).getX() * scaleX + border,
  184. pointList.get(i).getY() * scaleY, pointList.get(i + 1).getX() * scaleX + border,
  185. pointList.get(i + 1).getY() * scaleY);
  186. }
  187. graphCurve.append(line, true);
  188. }
  189. g2.draw(graphCurve);
  190. /*
  191. * // Draw the Points g2.setColor(Color.BLUE); for (int i = 0; i
  192. * < pointList.size() - 0; i++) { g2.fillOval((int)
  193. * (pointList.get(i).getX() * scaleX - recSize.getX() / 2) +
  194. * border, (int) (pointList.get(i).getY() * scaleY -
  195. * recSize.getY() / 2), (int) recSize.getX(), (int)
  196. * recSize.getY()); }
  197. */
  198. // Iteration Value
  199. g2.setColor(Color.BLUE);
  200. textWidth = g.getFontMetrics().stringWidth("" + ((HolonSwitch)current).getState(model.getCurIteration())/*arrayOfBooleans[effectiveX]*/) + 2;
  201. if (textWidth
  202. + (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1) + 2
  203. + border <= this.getWidth()) {
  204. g2.drawString("" + ((HolonSwitch)current).getState(model.getCurIteration()),
  205. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  206. + 2 + border,
  207. this.getHeight() - 10);
  208. } else {
  209. g2.drawString("" + ((HolonSwitch)current).getState(model.getCurIteration()),
  210. (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  211. + border - textWidth,
  212. this.getHeight() - 10);
  213. }
  214. }
  215. // When the switch graph is dragged
  216. if (tempP != null && pointDrag)
  217. {
  218. try {
  219. int i;
  220. for (i = 0; (i * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
  221. + border < getMousePosition().getX()); i++) {
  222. }
  223. dragInformation = "" + i;
  224. textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX())))
  225. + 2;
  226. if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
  227. g2.drawString(dragInformation, (int) (getMousePosition().getX()) + 10 + border,
  228. (int) (getMousePosition().getY() * scaleY) + 10);
  229. } else {
  230. g2.drawString(dragInformation, (int) (getMousePosition().getX()) - textWidth,
  231. (int) (getMousePosition().getY() * scaleY) + 10);
  232. }
  233. } catch (Exception e) {
  234. }
  235. }
  236. }
  237. // Iteration Line TODO: repeat
  238. g2.setColor(Color.BLUE);
  239. g2.setStroke(new BasicStroke(1));
  240. g2.drawLine(border + (effectiveX) * (this.getWidth() - border * 2) / /*(model.getIterations() - 1)*/100-1,
  241. 0, border + (effectiveX) * (this.getWidth() - border * 2) / /*(model.getIterations() - 1)*/100-1,
  242. this.getHeight());
  243. // algorithmus
  244. controller.calculateStateForTimeStep(model.getCurIteration());
  245. }
  246. @Override
  247. public void mouseDragged(MouseEvent e) {
  248. if (isElement) {
  249. elementDragged(e);
  250. } else if (isSwitch) {
  251. switchDragged(e);
  252. }
  253. }
  254. /**
  255. * When a Point of a Holon Element is dragged.
  256. *
  257. * @param e MouseEvent
  258. */
  259. public void elementDragged(MouseEvent e) {
  260. if (pointDrag && tempP != null) {
  261. // Out of Bounds verhindern
  262. int i = pointList.indexOf(tempP);
  263. x = (e.getX() - border) / scaleX;
  264. y = (e.getY() - border) / scaleY;
  265. // y
  266. if (e.getY() <= border) {
  267. y = 0;
  268. } else if (this.getHeight() - border <= e.getY()) {
  269. y = (this.getHeight() - border * 2) / scaleY;
  270. }
  271. // x
  272. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x + 2
  273. || pointList.get(i - 1).getX() > x - 2 || pointList.getFirst().getX() > x - 2
  274. || pointList.getLast().getX() < x + 2) {
  275. x = tempP.getX();
  276. }
  277. tempP.setLocation(x, y);
  278. repaint();
  279. }
  280. }
  281. /**
  282. * When a Point of a switch is dragged.
  283. *
  284. * @param e 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 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) {//TODO: test
  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 MouseEvent
  385. */
  386. public void switchPressed(MouseEvent e) {
  387. boolean added = false;
  388. boolean deletePoint = false;
  389. double x = (e.getX() - border) / scaleX;
  390. e.getY();
  391. // Halbe Iterations Distanz
  392. double dist = (width / (model.getIterations() - 1)) / 2;
  393. // Click on Point
  394. tempP = null;
  395. if (pointList != null) {
  396. for (Point p : pointList) {
  397. if (x >= p.getX() - dist * 2 && x <= p.getX() + dist * 2) {
  398. if (e.getButton() == MouseEvent.BUTTON3) {
  399. tempP = p;
  400. deletePoint = true;
  401. } else {
  402. pointDrag = true;
  403. tempP = p;
  404. }
  405. }
  406. }
  407. // New Point
  408. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
  409. for (int i = 0; i < pointList.size() && !added; i++) {
  410. if (x < pointList.get(i).getX() - dist) {
  411. // double p1, p2 um location der points zu bestimmen
  412. double p1 = pointList.get(i - 1).getX();
  413. double p2 = pointList.get(i).getX();
  414. // Punkte hinzufügen, je nachdem ob true oder false
  415. 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 - height / 6)));
  418. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height / 6)));
  419. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height / 6)));
  420. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height - height / 6)));
  421. added = true;
  422. } else if (pointList.get(i - 1).getY() == (int) (height / 6)
  423. && pointList.get(i).getY() == (int) (height / 6)) {
  424. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height / 6)));
  425. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height - height / 6)));
  426. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height - height / 6)));
  427. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) (height / 6)));
  428. added = true;
  429. }
  430. }
  431. }
  432. }
  433. // Delete a Point
  434. if (deletePoint && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
  435. int i = pointList.indexOf(tempP);
  436. // If Right, else if Left
  437. if (tempP.getY() == (int) (height / 6) && i < pointList.size() - 1 && i > 0) {
  438. pointList.remove(i);
  439. pointList.remove(i - 1);
  440. pointList.remove(i - 2);
  441. pointList.remove(i - 3);
  442. } else if (tempP.getY() == (int) (height - height / 6)) {
  443. pointList.remove(i + 2);
  444. pointList.remove(i + 1);
  445. pointList.remove(i);
  446. pointList.remove(i - 1);
  447. }
  448. }
  449. repaint();
  450. }
  451. }
  452. @Override
  453. public void mouseReleased(MouseEvent e) {
  454. if (pointDrag) {
  455. pointDrag = false;
  456. tempP = null;
  457. }
  458. /**
  459. * reset the dragInformation.
  460. */
  461. dragInformation = "";
  462. repaint();
  463. }
  464. /**
  465. * When the Component is Resized.
  466. *
  467. * @param e ComponentEvent
  468. */
  469. public void componentResized(ComponentEvent e) {
  470. // Wenn ein anderes Element genommen wird
  471. if (init) {
  472. init = false;
  473. // for scale on the first initialisation
  474. if (width == -1 && height == -1) {
  475. width = this.getWidth() - (border * 2);
  476. height = this.getHeight() - (border * 2);
  477. }
  478. // Scale
  479. scaleX = (this.getWidth() - (border * 2)) / width;
  480. scaleY = (this.getHeight() - (border * 2)) / height;
  481. // set the scroll graph invisible
  482. this.getParent().getParent().setVisible(false);
  483. }
  484. // Scale
  485. scaleX = (this.getWidth() - (border * 2)) / width;
  486. scaleY = (this.getHeight() - (border * 2)) / height;
  487. repaint();
  488. }
  489. @Override
  490. public void componentHidden(ComponentEvent e) {
  491. }
  492. @Override
  493. public void componentMoved(ComponentEvent e) {
  494. }
  495. @Override
  496. public void componentShown(ComponentEvent e) {
  497. }
  498. /**
  499. * Empty the Graph.
  500. */
  501. public void empty() {
  502. pointList = null;
  503. tempElements = null;
  504. current = null;
  505. isSwitch = false;
  506. isElement = false;
  507. repaint();
  508. }
  509. /**
  510. * Resets the Points for the Element.
  511. */
  512. public void reset() {
  513. pointList.removeAll(pointList);
  514. if (isSwitch) {
  515. pointList.addFirst(new Point(-border, (int) (height / 6)));
  516. pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  517. } else {
  518. pointList.addFirst(new Point(0, 0));
  519. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  520. }
  521. repaint();
  522. }
  523. /**
  524. * converts the number to fit the canvas.
  525. *
  526. * @param d the number to convert
  527. * @return the converted number
  528. */
  529. public double convertToCanvasY(float d) {
  530. return (height - (d * (height / maximum)));
  531. }
  532. /**
  533. * converts the number to fit the value.
  534. *
  535. * @param d the number to convert
  536. * @return the converted number
  537. */
  538. public float convertToValueY(double d) {
  539. return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
  540. }
  541. /**
  542. * Visualize the HolonElement on the Graph.
  543. *
  544. * @param selectedElement which should be visualized
  545. */
  546. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  547. //arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
  548. current = selectedElement.get(selectedElement.size()-1);
  549. tempElements=selectedElement;
  550. pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  551. isSwitch = false;
  552. isElement = true;
  553. maximum = getMaximum(selectedElement.get(selectedElement.size() - 1));
  554. // First time clicked on the Element
  555. if (pointList.isEmpty()) {
  556. pointList.addFirst(new Point(0, 0));
  557. pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  558. }
  559. repaint();
  560. }
  561. /**
  562. * Visualize the Switch on the Graph.
  563. *
  564. * @param s which should be visualized
  565. */
  566. public void repaintWithNewSwitch(HolonSwitch s) {
  567. current=s;
  568. //arrayOfBooleans = s.getValueArray();
  569. pointList = s.getGraphPoints();
  570. isSwitch = true;
  571. isElement = false;
  572. // First time clicked on the Element
  573. if (pointList.isEmpty()) {
  574. pointList.addFirst(new Point(-border, (int) (height / 6)));
  575. pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  576. }
  577. repaint();
  578. }
  579. /**
  580. * Build a Curve for the Graph.
  581. *
  582. * @param p1 startpoint
  583. * @param p2 endpoint
  584. * @return the CubicCurve2D for the Graph
  585. */
  586. public CubicCurve2D buildCurve(Point p1, Point p2) {
  587. x1 = (int) p1.getX();
  588. y1 = (int) p1.getY();
  589. x2 = (int) p2.getX();
  590. y2 = (int) p2.getY();
  591. // calculate the controllpoints
  592. ctrlx1 = x1 + (x2 - x1) / 2;
  593. ctrlx2 = x2 - (x2 - x1) / 2;
  594. if (y1 < y2) {
  595. ctrly1 = y1 + (y2 - y1) / 10;
  596. ctrly2 = y2 - (y2 - y1) / 10;
  597. } else {
  598. ctrly1 = y1 - (y1 - y2) / 10;
  599. ctrly2 = y2 + (y1 - y2) / 10;
  600. }
  601. // set the curve
  602. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  603. x2 * scaleX, y2 * scaleY);
  604. return c;
  605. }
  606. /**
  607. * Fills the Arrays with booleans.
  608. */
  609. public void fillArrayofBooleans() {
  610. for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {
  611. int t = (int) getYValueAt((int) (i * width / (STANDARD_GRAPH_ACCURACY - 1)));
  612. if (t <= height / 2) {
  613. ((HolonSwitch)current).setActiveAt(i, true);
  614. } else {
  615. ((HolonSwitch)current).setActiveAt(i, false);
  616. }
  617. }
  618. }
  619. /**
  620. * Fills the Arrays of each HolonElement.
  621. */
  622. @SuppressWarnings("unchecked")
  623. public void fillArrayofValue() {
  624. for (HolonElement he : tempElements) {
  625. maximum = getMaximum(he);
  626. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  627. for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {//!!!!!
  628. he.setAvailableEnergyPerElementAt(i, convertToValueY(getYValueAt2((int) (i * width / (100 - 1)))));
  629. //he.getAvailableEnergyPerElementAt()[i] = convertToValueY(getYValueAt2((int) (i * width / (100 - 1))));
  630. }
  631. //arrayOfFloats = he.getAvailableEnergyPerElementAt();
  632. }
  633. }
  634. /**
  635. * Get the Y Value at the x Coordination.
  636. *
  637. * @param xVal the x value for the y value
  638. * @return y, the value at x
  639. */
  640. public float getYValueAt(int xVal) {
  641. for (int i = 0; i < pointList.size() - 1; i++) {
  642. // get the Points
  643. if (xVal <= pointList.get(i + 1).getX()) {
  644. // Curve erstellen
  645. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  646. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  647. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  648. return getIntersectionPoint(l1, l2);
  649. }
  650. }
  651. return 0;
  652. }
  653. /**
  654. * Get y value at the x Coordination via curves.
  655. *
  656. * @param xVal the x value for the y value
  657. * @return y value at x
  658. */
  659. public float getYValueAt2(int xVal) {
  660. for (int i = 0; i < pointList.size() - 1; i++) {
  661. // get the Points
  662. if (xVal >= pointList.get(i).getX()) {
  663. // Curve erstellen
  664. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  665. c.subdivide(cl, cr);
  666. // Teil der Kurve aussuchen
  667. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  668. c = cl;
  669. // Kurve Links von "unten"
  670. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  671. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  672. if (c.contains(xVal * scaleX, j * scaleY)) {
  673. return (float) (j);
  674. }
  675. }
  676. } else {// Kurve Links von "oben"
  677. for (float j = 0; j < height; j += 0.1f) {
  678. if (c.contains(xVal * scaleX, j * scaleY)) {
  679. return (float) (j);
  680. }
  681. }
  682. }
  683. } else {
  684. c = cr;
  685. // Kurve Links von "unten"
  686. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  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. } else {// Kurve Links von "oben"
  693. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  694. if (c.contains(xVal * scaleX, j * scaleY)) {
  695. return (float) (j);
  696. }
  697. }
  698. }
  699. }
  700. }
  701. }
  702. // else
  703. return getYValueAt(xVal);
  704. }
  705. /**
  706. * Get the Intersection Point of 2 Lines.
  707. *
  708. * @param l1 the first Line
  709. * @param l2 the second Line
  710. * @return The Intersection Point
  711. */
  712. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  713. if (!l1.intersectsLine(l2)) {
  714. return 0;// null;
  715. }
  716. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  717. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  718. double det = sx * ry - sy * rx;
  719. if (det == 0) {
  720. return 0;// null;
  721. } else {
  722. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  723. if (z < 0 || z > 1) {
  724. return 0;// new Point(0, 0); // intersection at end point!
  725. }
  726. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  727. // (py + z * ry));
  728. }
  729. } // end intersection line-line
  730. public void update(ArrayList<AbstractCpsObject> obj) {
  731. ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
  732. AbstractCpsObject u = null;
  733. queue.addAll(obj);
  734. while (!queue.isEmpty()) {
  735. u = queue.pop();
  736. repaintGraph(u);
  737. }
  738. empty();
  739. if (u instanceof CpsUpperNode)
  740. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  741. queue.add(adjacent);
  742. }
  743. }
  744. void repaintGraph(AbstractCpsObject u) {
  745. ArrayList<HolonElement> list = new ArrayList<>();
  746. if (u instanceof HolonObject) {
  747. for (HolonElement ele : ((HolonObject) u).getElements()) {
  748. list.add(ele);
  749. repaintWithNewElement(list);
  750. fillArrayofValue();
  751. list.remove(0);
  752. }
  753. } else if (u instanceof HolonSwitch) {
  754. repaintWithNewSwitch((HolonSwitch) u);
  755. fillArrayofBooleans();
  756. }
  757. }
  758. float getMaximum(HolonElement ele) {
  759. if (ele.isFlexible()) {
  760. return ele.getFlexibleEnergyAvailablePerElement();
  761. } else {
  762. return ele.getEnergyPerElement();
  763. }
  764. }
  765. /**
  766. * sets the localPeriod of the Current Graph
  767. * @param localPeriod
  768. */
  769. public void setLocalPeriod(int localPeriod){
  770. for(IGraphedElement e:tempElements)e.setLocalPeriod(localPeriod);
  771. }
  772. /**
  773. * gets the LocalPeriod of the CurrentGraph
  774. * @return localPeriod of the current Element or Switch
  775. */
  776. public int getLocalPeriod(){
  777. if(current!=null)return current.getLocalPeriod();
  778. else return model.getGraphIterations();//TODO: maybe rename
  779. }
  780. public boolean isStretching(){
  781. return current.isStretching();
  782. }
  783. public void setStretching(boolean b){
  784. for(IGraphedElement e:tempElements)e.setStretching(b);
  785. }
  786. static int lv=0;
  787. public static int getEffectiveIndex(Model m, IGraphedElement e, int timeStep){
  788. if(e.isStretching())return timeStep*100/(m==null?STANDARD_GRAPH_ACCURACY:m.getIterations());
  789. else return timeStep%e.getLocalPeriod()*100/e.getLocalPeriod();
  790. }
  791. public static int getEffectiveIndex(IGraphedElement e, int timeStep){
  792. return getEffectiveIndex(SingletonControl.getInstance().getControl()==null?null:SingletonControl.getInstance().getControl().getModel(),e,timeStep);
  793. }
  794. public static int getEffectiveIndex(IGraphedElement e){
  795. return getEffectiveIndex(e,SingletonControl.getInstance().getControl().getModel().getCurIteration());
  796. }
  797. }