UnitGraph.java 26 KB

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