UnitGraph.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. package ui.view;
  2. import classes.*;
  3. import classes.comparator.UnitGraphPointComperator;
  4. import interfaces.GraphEditable;
  5. import interfaces.GraphEditable.Graphtype;
  6. import interfaces.IGraphedElement;
  7. import sun.reflect.generics.reflectiveObjects.NotImplementedException;
  8. import ui.controller.Control;
  9. import ui.controller.SingletonControl;
  10. import ui.model.Model;
  11. import javax.swing.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.awt.geom.CubicCurve2D;
  15. import java.awt.geom.GeneralPath;
  16. import java.awt.geom.Line2D;
  17. import java.awt.geom.Path2D;
  18. import java.awt.geom.Point2D;
  19. import java.util.ArrayDeque;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.LinkedList;
  23. import java.util.ListIterator;
  24. /**
  25. * This Class represents a Graph where the User can model the behavior of
  26. * elements and switches over time.
  27. *
  28. * @author Gruppe14
  29. */
  30. public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  31. private static final long serialVersionUID = 1L;
  32. public static final int STANDARD_GRAPH_ACCURACY = 100;
  33. private GeneralPath graphCurve = new GeneralPath();
  34. private float maximum = 0;
  35. // Information shown when a Point is Dragged
  36. private String dragInformation = "";
  37. // Points
  38. private Point recSize = new Point(8, 8); // Point Size
  39. private Graphics2D g2;
  40. private CubicCurve2D c = new CubicCurve2D.Double();
  41. private CubicCurve2D cr = new CubicCurve2D.Double();
  42. private CubicCurve2D cl = new CubicCurve2D.Double();
  43. private LinkedList<Point> pointList;
  44. // Scale for the Graph
  45. private double scaleX;
  46. private double scaleY;
  47. private double width = -1;
  48. private double height = -1;
  49. private boolean isElement = false;
  50. private boolean isSwitch = false;
  51. private ArrayList<HolonElement> tempElements = new ArrayList<>();
  52. private Model model;
  53. private Control controller;
  54. private Line2D.Double line = null;
  55. private boolean pointDrag = false;
  56. private boolean init = true;
  57. private Point tempP = null;
  58. private double x = 0, y = 0;
  59. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  60. private int textWidth = 0;
  61. private IGraphedElement current;
  62. //NEW ERA
  63. // Normal Settings
  64. private int border = 4;
  65. private int clickThreshholdSquared = 25;
  66. // Display Settings
  67. /**
  68. * The size of a dot in the graph.
  69. * It should be at least 1.
  70. * */
  71. int dotSize = 8;
  72. /** The Color of a dot in the graph. */
  73. Color dotColor = Color.blue;
  74. Color editDotColor = new Color(255, 119, 0);
  75. //Intern Variables
  76. //TODO: JavaDoc
  77. private LinkedList<UnitGraphPoint> actualGraphPoints = new LinkedList<UnitGraphPoint>();
  78. private Graphtype actualGraphType;
  79. private GraphEditable actualElement;
  80. Position currentPosition; //outDated
  81. Position editPosition;
  82. boolean released = false;
  83. private int widthWithBorder, heightWithBorder;
  84. /**
  85. * Constructor.
  86. *
  87. * @param model the Model
  88. * @param control the Controller
  89. */
  90. public UnitGraph(final Model model, Control control) {
  91. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  92. this.controller = control;
  93. this.model = model;
  94. this.pointList = new LinkedList<>();
  95. this.setBackground(Color.WHITE);
  96. this.addMouseListener(this);
  97. this.addMouseMotionListener(this);
  98. this.addComponentListener(this);
  99. }
  100. /**
  101. * Paints all Components on the Canvas.
  102. *
  103. * @param g Graphics
  104. */
  105. public void paintComponent(Graphics g) {
  106. super.paintComponent(g);
  107. //System.out.println("paint");
  108. Graphics2D g2D = (Graphics2D) g;
  109. g2D.setColor(Color.BLACK);
  110. int höhe = this.getHeight();
  111. int breite = this.getWidth();
  112. g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  113. g2D.setStroke(new BasicStroke(2));
  114. // g2D.drawLine(0, 0,breite, höhe);
  115. //printDebug();
  116. printDebugRepresentive();
  117. drawUnitGraph(g2D);
  118. g2D.setColor(dotColor);
  119. if(released)
  120. {
  121. drawUnitGraphPointsReleased(g2D);
  122. }else
  123. {
  124. drawUnitGraphPoints(g2D);
  125. }
  126. //generate Path --> maybe als Methode auslagern
  127. //Good Source for basic understanding for Bezier Curves
  128. //http://www.theappguruz.com/blog/bezier-curve-in-games
  129. }
  130. //TODO -> New Section
  131. private Path2D.Double initBezier(Position start) {
  132. //Good Source for basic understanding for Bezier Curves
  133. //http://www.theappguruz.com/blog/bezier-curve-in-games
  134. Path2D.Double path = new Path2D.Double();
  135. path.moveTo(start.x, start.y);
  136. return path;
  137. }
  138. private void curveTo(Path2D.Double path, Position actual, Position target) {
  139. double mitte = (actual.x + target.x)* 0.5;
  140. path.curveTo(mitte, actual.y, mitte, target.y, target.x, target.y);
  141. }
  142. private void drawDot(Graphics2D g, Position p)
  143. {
  144. g.fillOval(p.x -dotSize/2, p.y-dotSize/2, dotSize, dotSize);
  145. }
  146. private void drawUnitGraph(Graphics2D g) {
  147. switch(actualGraphType) {
  148. case boolGraph:
  149. drawBoolGraph(g);
  150. break;
  151. case doubleGraph:
  152. if(released)
  153. drawDoubleGraphReleased(g);
  154. else
  155. drawDoubleGraph(g);
  156. break;
  157. default:
  158. throw new UnsupportedOperationException();
  159. }
  160. }
  161. private void drawUnitGraphPoints(Graphics2D g) {
  162. g.setColor(Color.blue);
  163. for(UnitGraphPoint p : actualGraphPoints){
  164. drawDot(g, p.displayedPosition);
  165. }
  166. }
  167. private void drawUnitGraphPointsReleased(Graphics2D g) {
  168. ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
  169. g.setColor(Color.blue);
  170. while (iter2.hasNext())
  171. {
  172. Position tempPosition = iter2.next().displayedPosition;
  173. if(tempPosition.x > editPosition.x)
  174. {
  175. iter2.previous();
  176. break;
  177. }
  178. drawDot(g, tempPosition);
  179. }
  180. g.setColor(editDotColor);
  181. drawDot(g, editPosition);
  182. g.setColor(Color.blue);
  183. while (iter2.hasNext())
  184. {
  185. drawDot(g, iter2.next().displayedPosition);
  186. }
  187. }
  188. private void drawBoolGraph(Graphics2D g) {
  189. throw new NotImplementedException();
  190. }
  191. private void drawDoubleGraph(Graphics2D g) {
  192. if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
  193. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  194. Position actual = iter.next().displayedPosition;
  195. Path2D.Double path = this.initBezier(actual);
  196. while (iter.hasNext())
  197. {
  198. Position target = iter.next().displayedPosition;
  199. this.curveTo(path, actual, target);
  200. actual = target;
  201. }
  202. g.draw(path);
  203. }
  204. private void drawDoubleGraphReleased(Graphics2D g) {
  205. if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
  206. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  207. Position actual = iter.next().displayedPosition;
  208. Path2D.Double path = this.initBezier(actual);
  209. while (iter.hasNext())
  210. {
  211. Position target = iter.next().displayedPosition;
  212. if(target.x > editPosition.x)
  213. {
  214. iter.previous();
  215. break;
  216. }
  217. this.curveTo(path, actual, target);
  218. actual = target;
  219. }
  220. Position old = actual;
  221. Path2D.Double editPath = this.initBezier(actual);
  222. actual = iter.next().displayedPosition;
  223. //here actual is new Position;
  224. this.curveTo(editPath, old, editPosition);
  225. this.curveTo(editPath, editPosition, actual);
  226. path.moveTo(actual.x, actual.y);
  227. // g.setColor(Color.blue);
  228. while (iter.hasNext())
  229. {
  230. Position target = iter.next().displayedPosition;
  231. this.curveTo(path, actual, target);
  232. actual = target;
  233. }
  234. g.draw(path);
  235. g.setColor(editDotColor);
  236. g.draw(editPath);
  237. }
  238. private void updateRepresentativePositions()
  239. {
  240. for(UnitGraphPoint p : actualGraphPoints) {
  241. p.calcDisplayedPosition(border, widthWithBorder, heightWithBorder);
  242. }
  243. }
  244. private void overrideUnitGraph(LinkedList<Point2D.Double> stateCurve) {
  245. actualGraphPoints.clear();
  246. for(Point2D.Double p: stateCurve){
  247. actualGraphPoints.add(new UnitGraphPoint(p));
  248. }
  249. updateRepresentativePositions();
  250. }
  251. private void calculateWidthHeight()
  252. {
  253. widthWithBorder = this.getWidth() - 2 * border;
  254. heightWithBorder = this.getHeight() - 2 * border;
  255. }
  256. public void initNewElement(GraphEditable element)
  257. {
  258. overrideUnitGraph(element.getStateGraph());
  259. actualGraphType = element.getGraphType();
  260. actualElement = element;
  261. repaint();
  262. }
  263. private void printDebugRepresentive(){
  264. if(this.actualGraphPoints.isEmpty()) return;
  265. System.out.print("{");
  266. for(UnitGraphPoint p: actualGraphPoints){
  267. System.out.print(p.displayedPosition);
  268. }
  269. System.out.println("}");
  270. }
  271. private void detectPointUnderCurserAndRemove(MouseEvent mEvent) {
  272. //get mouse Position
  273. Position mPosition = new Position(mEvent.getPoint());
  274. ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
  275. while (iter2.hasNext())
  276. {
  277. if(mPosition.squareDistance(iter2.next().displayedPosition) < clickThreshholdSquared)
  278. {
  279. iter2.remove();
  280. break;
  281. }
  282. }
  283. }
  284. private void updateEditPointPosition(Position newPosition) {
  285. //make it in the bounds of the UnitGraph no Point out of the Border
  286. currentPosition = setInBounds(newPosition);
  287. this.editPosition = currentPosition;
  288. repaint();
  289. }
  290. private Position setInBounds(Position p) {
  291. p.clampX(border, border + widthWithBorder);
  292. p.clampY(border, border + heightWithBorder);
  293. return p;
  294. }
  295. private void insertNewGraphPoint(Position pos)
  296. {
  297. System.out.println("insertNewGraphPoint");
  298. setInBounds(pos);
  299. ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
  300. while (iter2.hasNext())
  301. {
  302. Position tempPosition = iter2.next().displayedPosition;
  303. if(pos.x <= tempPosition.x)
  304. {
  305. //previous to go back a position to make the new point before the the Position with greater X
  306. iter2.previous();
  307. iter2.add(generateUnitGraphPoint(pos));
  308. break;
  309. }
  310. }
  311. }
  312. private UnitGraphPoint generateUnitGraphPoint(Position pos) {
  313. UnitGraphPoint temp = new UnitGraphPoint((double)(pos.x - border)/(double)widthWithBorder,1 - (double) (pos.y - border)/(double)heightWithBorder,true);
  314. temp.displayedPosition = pos;
  315. return temp;
  316. }
  317. @Override
  318. public void mouseDragged(MouseEvent e) {
  319. System.out.println("MouseDragged");
  320. updateEditPointPosition(new Position(e.getPoint()));
  321. }
  322. @Override
  323. public void mouseMoved(MouseEvent e) {
  324. }
  325. @Override
  326. public void mouseClicked(MouseEvent e) {
  327. }
  328. @Override
  329. public void mouseEntered(MouseEvent e) {
  330. }
  331. @Override
  332. public void mouseExited(MouseEvent e) {
  333. }
  334. @Override
  335. public void mousePressed(MouseEvent e) {
  336. System.out.println("mousePressed");
  337. detectPointUnderCurserAndRemove(e);
  338. updateEditPointPosition(new Position(e.getPoint()));
  339. released = true;
  340. repaint();
  341. }
  342. @Override
  343. public void mouseReleased(MouseEvent e) {
  344. System.out.println("mouseReleased");
  345. this.insertNewGraphPoint(editPosition);
  346. released = false;
  347. repaint();
  348. }
  349. /**
  350. * When the Component is Resized.
  351. *
  352. * @param e ComponentEvent
  353. */
  354. public void componentResized(ComponentEvent e) {
  355. System.out.println("componentResized");
  356. calculateWidthHeight();
  357. updateRepresentativePositions();
  358. // Wenn ein anderes Element genommen wird
  359. /*
  360. if (init) {
  361. init = false;
  362. // for scale on the first initialisation
  363. if (width == -1 && height == -1) {
  364. width = this.getWidth() - (border * 2);
  365. height = this.getHeight() - (border * 2);
  366. }
  367. // Scale
  368. scaleX = (this.getWidth() - (border * 2)) / width;
  369. scaleY = (this.getHeight() - (border * 2)) / height;
  370. // set the scroll graph invisible
  371. this.getParent().getParent().setVisible(false);
  372. }
  373. // Scale
  374. scaleX = (this.getWidth() - (border * 2)) / width;
  375. scaleY = (this.getHeight() - (border * 2)) / height;
  376. */
  377. repaint();
  378. }
  379. @Override
  380. public void componentHidden(ComponentEvent e) {
  381. }
  382. @Override
  383. public void componentMoved(ComponentEvent e) {
  384. }
  385. @Override
  386. public void componentShown(ComponentEvent e) {
  387. }
  388. /**
  389. * Empty the Graph.
  390. */
  391. public void empty() {
  392. System.out.println("empty");
  393. pointList = null;
  394. tempElements = null;
  395. current = null;
  396. isSwitch = false;
  397. isElement = false;
  398. repaint();
  399. }
  400. /**
  401. * Resets the Points for the Element.
  402. */
  403. public void reset() {
  404. System.out.println("reset");
  405. // pointList.removeAll(pointList);
  406. // if (isSwitch) {
  407. // pointList.addFirst(new Point(-border, (int) (height / 6)));
  408. // pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  409. // } else {
  410. // pointList.addFirst(new Point(0, 0));
  411. // pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  412. // }
  413. // repaint();
  414. }
  415. /**
  416. * converts the number to fit the canvas.
  417. *
  418. * @param d the number to convert
  419. * @return the converted number
  420. */
  421. public double convertToCanvasY(float d) {
  422. System.out.println("convertToCanvasY");
  423. return (height - (d * (height / maximum)));
  424. }
  425. /**
  426. * converts the number to fit the value.
  427. *
  428. * @param d the number to convert
  429. * @return the converted number
  430. */
  431. public float convertToValueY(double d) {
  432. System.out.println("convertToValueY");
  433. return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
  434. }
  435. /**
  436. * Visualize the HolonElement on the Graph.
  437. *
  438. * @param selectedElement which should be visualized
  439. */
  440. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  441. System.out.println("repaintWithNewElement");
  442. // //maybe linkedlist better?
  443. // //arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
  444. // current = selectedElement.get(selectedElement.size()-1);
  445. // tempElements=selectedElement;
  446. // pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  447. // isSwitch = false;
  448. // isElement = true;
  449. // maximum = getMaximum(selectedElement.get(selectedElement.size() - 1));
  450. // // First time clicked on the Element
  451. // if (pointList.isEmpty()) {
  452. // pointList.addFirst(new Point(0, 0));
  453. // pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
  454. // }
  455. // repaint();
  456. }
  457. /**
  458. * Visualize the Switch on the Graph.
  459. *
  460. * @param s which should be visualized
  461. */
  462. public void repaintWithNewSwitch(HolonSwitch s) {
  463. System.out.println("repaintWithNewSwitch");
  464. // //arrayOfBooleans = s.getValueArray();
  465. // current=s;
  466. // pointList = s.getGraphPoints();
  467. // isSwitch = true;
  468. // isElement = false;
  469. // // First time clicked on the Element
  470. // if (pointList.isEmpty()) {
  471. // pointList.addFirst(new Point(-border, (int) (height / 6)));
  472. // pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
  473. // }
  474. // repaint();
  475. }
  476. /**
  477. * Build a Curve for the Graph.
  478. *
  479. * @param p1 startpoint
  480. * @param p2 endpoint
  481. * @return the CubicCurve2D for the Graph
  482. */
  483. public CubicCurve2D buildCurve(Point p1, Point p2) {
  484. System.out.println("buildCurve");
  485. x1 = (int) p1.getX();
  486. y1 = (int) p1.getY();
  487. x2 = (int) p2.getX();
  488. y2 = (int) p2.getY();
  489. // calculate the controllpoints
  490. ctrlx1 = x1 + (x2 - x1) / 2;
  491. ctrlx2 = x2 - (x2 - x1) / 2;
  492. if (y1 < y2) {
  493. ctrly1 = y1 + (y2 - y1) / 10;
  494. ctrly2 = y2 - (y2 - y1) / 10;
  495. } else {
  496. ctrly1 = y1 - (y1 - y2) / 10;
  497. ctrly2 = y2 + (y1 - y2) / 10;
  498. }
  499. // set the curve
  500. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  501. x2 * scaleX, y2 * scaleY);
  502. return c;
  503. }
  504. /**
  505. * Fills the Arrays with booleans.
  506. */
  507. public void fillArrayofBooleans() {
  508. System.out.println("fillArrayofBooleans");
  509. for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {
  510. int t = (int) getYValueAt((int) (i * width / (STANDARD_GRAPH_ACCURACY - 1)));
  511. if (t <= height / 2) {
  512. ((HolonSwitch)current).setActiveAt(i, true);
  513. } else {
  514. ((HolonSwitch)current).setActiveAt(i, false);
  515. }
  516. }
  517. }
  518. /**
  519. * Fills the Arrays of each HolonElement.
  520. */
  521. @SuppressWarnings("unchecked")
  522. public void generateSampleCurves() {
  523. System.out.println("generateSampleCurves");
  524. for (HolonElement he : tempElements) {
  525. maximum = getMaximum(he);
  526. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  527. //foreach(Point p: pointList)
  528. System.out.println("------------");
  529. System.out.println("pointList[");
  530. for(Point p: pointList)
  531. {
  532. System.out.println(p);
  533. }
  534. System.out.println("]");
  535. for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {//!!!!!
  536. he.setAvailableEnergyPerElementAt(i, convertToValueY(getYValueAt2((int) (i * width / (100 - 1)))));
  537. }
  538. System.out.println("TestgraphPoints:");
  539. he.testFunctiongetAvailableEnergyAt(0);
  540. }
  541. }
  542. /**
  543. * Convert the graph widget point to a pointlist from a holon element
  544. * @param unitgraph
  545. */
  546. public LinkedList<Point> convertUnitGraphToHolonElemntPointList(LinkedList<Point> unitgraph)
  547. {
  548. LinkedList<Point> graphpoints = new LinkedList<Point>();
  549. if(width == 0 || height == 0) return graphpoints;
  550. for(Point p: unitgraph)
  551. {
  552. //CalcX
  553. int x = (int )((p.getX() / width)* 100.0);
  554. //CalcY
  555. int y = (int )((1.0-(p.getY() / height))* 100.0); //1.0- because 0 is at the top, width is at the bottom
  556. //AddPoint
  557. graphpoints.add(new Point(x, y));
  558. }
  559. return graphpoints;
  560. }
  561. public LinkedList<Point> convertHolonElementPointListToUnitGraph(LinkedList<Point> graphPoints)
  562. {
  563. LinkedList<Point> unitgraph = new LinkedList<Point>();
  564. for(Point p: graphPoints)
  565. {
  566. //CalcX
  567. int x = (int )((p.getX() / 100.0)* width);
  568. //CalcY
  569. int y = (int )((1.0-(p.getY() / 100.0))* height); //1.0- because 0 is at the top, width is at the bottom
  570. //AddPoint
  571. unitgraph.add(new Point(x, y));
  572. }
  573. return unitgraph;
  574. }
  575. /**
  576. * Get the Y Value at the x Coordination.
  577. *
  578. * @param xVal the x value for the y value
  579. * @return y, the value at x
  580. */
  581. public float getYValueAt(int xVal) {
  582. System.out.println("getYValueAt");
  583. for (int i = 0; i < pointList.size() - 1; i++) {
  584. // get the Points
  585. if (xVal <= pointList.get(i + 1).getX()) {
  586. // Curve erstellen
  587. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  588. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  589. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  590. return getIntersectionPoint(l1, l2);
  591. }
  592. }
  593. return 0;
  594. }
  595. /**
  596. * Get y value at the x Coordination via curves.
  597. *
  598. * @param xVal the x value for the y value
  599. * @return y value at x
  600. */
  601. public float getYValueAt2(int xVal) {
  602. System.out.println("getYValueAt2");
  603. for (int i = 0; i < pointList.size() - 1; i++) {
  604. // get the Points
  605. if (xVal >= pointList.get(i).getX()) {
  606. // Curve erstellen
  607. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  608. c.subdivide(cl, cr);
  609. // Teil der Kurve aussuchen
  610. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  611. c = cl;
  612. // Kurve Links von "unten"
  613. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  614. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  615. if (c.contains(xVal * scaleX, j * scaleY)) {
  616. return (float) (j);
  617. }
  618. }
  619. } else {// Kurve Links von "oben"
  620. for (float j = 0; j < height; j += 0.1f) {
  621. if (c.contains(xVal * scaleX, j * scaleY)) {
  622. return (float) (j);
  623. }
  624. }
  625. }
  626. } else {
  627. c = cr;
  628. // Kurve Links von "unten"
  629. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  630. for (float j = 0; j < height; j += 0.1f) {
  631. if (c.contains(xVal * scaleX, j * scaleY)) {
  632. return (float) (j);
  633. }
  634. }
  635. } else {// Kurve Links von "oben"
  636. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  637. if (c.contains(xVal * scaleX, j * scaleY)) {
  638. return (float) (j);
  639. }
  640. }
  641. }
  642. }
  643. }
  644. }
  645. // else
  646. return getYValueAt(xVal);
  647. }
  648. /**
  649. * Get the Intersection Point of 2 Lines.
  650. *
  651. * @param l1 the first Line
  652. * @param l2 the second Line
  653. * @return The Intersection Point
  654. */
  655. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  656. System.out.println("getIntersectionPoint");
  657. if (!l1.intersectsLine(l2)) {
  658. return 0;// null;
  659. }
  660. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  661. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  662. double det = sx * ry - sy * rx;
  663. if (det == 0) {
  664. return 0;// null;
  665. } else {
  666. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  667. if (z < 0 || z > 1) {
  668. return 0;// new Point(0, 0); // intersection at end point!
  669. }
  670. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  671. // (py + z * ry));
  672. }
  673. } // end intersection line-line
  674. public void update(ArrayList<AbstractCpsObject> obj) {
  675. System.out.println("update");
  676. ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
  677. AbstractCpsObject u = null;
  678. queue.addAll(obj);
  679. while (!queue.isEmpty()) {
  680. u = queue.pop();
  681. repaintGraph(u);
  682. }
  683. empty();
  684. if (u instanceof CpsUpperNode)
  685. for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
  686. queue.add(adjacent);
  687. }
  688. }
  689. void repaintGraph(AbstractCpsObject u) {
  690. System.out.println("repaintGraph");
  691. ArrayList<HolonElement> list = new ArrayList<>();
  692. if (u instanceof HolonObject) {
  693. for (HolonElement ele : ((HolonObject) u).getElements()) {
  694. list.add(ele);
  695. repaintWithNewElement(list);
  696. generateSampleCurves();
  697. list.remove(0);
  698. }
  699. } else if (u instanceof HolonSwitch) {
  700. repaintWithNewSwitch((HolonSwitch) u);
  701. fillArrayofBooleans();
  702. }
  703. }
  704. float getMaximum(HolonElement ele) {
  705. System.out.println("getMaximum");
  706. if (ele.isFlexible()) {
  707. return ele.getFlexibleEnergyAvailablePerElement();
  708. } else {
  709. return ele.getEnergyPerElement();
  710. }
  711. }
  712. /**
  713. * sets the localPeriod of the Current Graph
  714. * @param localPeriod
  715. */
  716. public void setLocalPeriod(int localPeriod){
  717. System.out.println("setLocalPeriod");
  718. if(isElement)for(IGraphedElement e:tempElements)e.setLocalPeriod(localPeriod);
  719. else if(isSwitch)current.setLocalPeriod(localPeriod);
  720. }
  721. /**
  722. * gets the LocalPeriod of the CurrentGraph
  723. * @return localPeriod of the current Element or Switch
  724. */
  725. public int getLocalPeriod(){
  726. System.out.println("getLocalPeriod");
  727. if(current!=null)return current.getLocalPeriod();
  728. else return model.getGraphIterations();//TODO: maybe rename
  729. }
  730. public boolean isStretching(){
  731. System.out.println("isStretching");
  732. return current.isStretching();
  733. }
  734. public void setStretching(boolean b){
  735. System.out.println("setStretching");
  736. if(isElement)for(IGraphedElement e:tempElements)e.setStretching(b);
  737. else if(isSwitch)current.setStretching(b);
  738. }
  739. }