UnitGraph.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. package ui.view;
  2. import classes.*;
  3. import classes.comparator.UnitGraphPointComperator;
  4. import interfaces.GraphEditable;
  5. import interfaces.GraphEditable.Graphtype;
  6. import interfaces.LocalMode;
  7. import ui.controller.Control;
  8. import ui.controller.SingletonControl;
  9. import ui.model.Model;
  10. import javax.swing.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.awt.geom.Path2D;
  14. import java.awt.geom.Point2D;
  15. import java.util.ArrayList;
  16. import java.util.LinkedList;
  17. import java.util.ListIterator;
  18. /**
  19. * This Class represents a Graph where the User can model the behavior of
  20. * elements and switches over time.
  21. *
  22. * @author Tom Troppmann
  23. */
  24. public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  25. private static final long serialVersionUID = 1L;
  26. // Normal Settings
  27. private int border = 4;
  28. private int clickThreshholdSquared = 25;
  29. // Display Settings
  30. /**
  31. * The size of a dot in the graph.
  32. * It should be at least 1.
  33. * */
  34. int dotSize = 8;
  35. /** The Color of a dot in the graph. */
  36. Color dotColor = Color.blue;
  37. Color editDotColor = new Color(255, 119, 0);
  38. //Intern Variables
  39. //TODO: JavaDoc
  40. private LinkedList<UnitGraphPoint> actualGraphPoints = new LinkedList<UnitGraphPoint>();
  41. private Graphtype actualGraphType;
  42. private GraphEditable actualElement;
  43. Position editPosition;
  44. boolean editMode = false;
  45. private enum pointType {Normal, StartPoint, EndPoint};
  46. pointType editPoint = pointType.Normal;
  47. //Maybe Needed
  48. private Model model;
  49. private Control controller;
  50. private int widthWithBorder, heightWithBorder;
  51. /**
  52. * Constructor.
  53. *
  54. * @param model the Model
  55. * @param control the Controller
  56. */
  57. public UnitGraph(final Model model, Control control) {
  58. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  59. this.controller = control;
  60. this.model = model;
  61. this.setBackground(Color.WHITE);
  62. this.addMouseListener(this);
  63. this.addMouseMotionListener(this);
  64. this.addComponentListener(this);
  65. }
  66. /**
  67. * When the UnitGraph should represent a new GraphEditable Element.
  68. * Its Updates the Graph and give access to the Element.
  69. * @param element
  70. */
  71. public void initNewElement(GraphEditable element)
  72. {
  73. overrideUnitGraph(element.getStateGraph());
  74. actualGraphType = element.getGraphType();
  75. actualElement = element;
  76. repaint();
  77. }
  78. /**
  79. * Paints the Graph, the Grid, the actual Line fro the currentIteration
  80. * @param g Graphics
  81. */
  82. public void paintComponent(Graphics g) {
  83. super.paintComponent(g);
  84. Graphics2D g2D = (Graphics2D) g;
  85. drawGrid(g2D);
  86. g2D.setColor(Color.BLACK);
  87. g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  88. g2D.setStroke(new BasicStroke(2));
  89. drawUnitGraph(g2D);
  90. g2D.setColor(dotColor);
  91. if(editMode)
  92. {
  93. drawUnitGraphPointsReleased(g2D);
  94. }else
  95. {
  96. drawUnitGraphPoints(g2D);
  97. }
  98. g2D.setColor(dotColor);
  99. g2D.setStroke(new BasicStroke(1));
  100. drawCurrentIterartionLine(g2D);
  101. }
  102. // Draw Methods only to let the User see the changes. Nothing its saved here or changed.
  103. /**
  104. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  105. * <p>
  106. * This Methods draws the UnitGraph whether its a boolGraph or a doubleGraph.
  107. * @param g to draw.
  108. */
  109. private void drawUnitGraph(Graphics2D g) {
  110. switch(actualGraphType) {
  111. case boolGraph:
  112. if(editMode)
  113. drawBoolGraphInEditMode(g);
  114. else
  115. drawBoolGraph(g);
  116. break;
  117. case doubleGraph:
  118. if(editMode)
  119. drawDoubleGraphInEditMode(g);
  120. else
  121. drawDoubleGraph(g);
  122. break;
  123. default:
  124. throw new UnsupportedOperationException();
  125. }
  126. }
  127. /**
  128. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  129. * <p>
  130. * This Methods draws the UnitGraphPoints of the UnitGraph.
  131. * @param g to draw.
  132. */
  133. private void drawUnitGraphPoints(Graphics2D g) {
  134. g.setColor(dotColor);
  135. for(UnitGraphPoint p : actualGraphPoints){
  136. drawDot(g, p.displayedPosition);
  137. }
  138. }
  139. /**
  140. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  141. * <p>
  142. * This Methods draws the UnitGraphPoints of the UnitGraph when its in EditMode.
  143. * @param g to draw.
  144. */
  145. private void drawUnitGraphPointsReleased(Graphics2D g) {
  146. drawUnitGraphPoints(g);
  147. g.setColor(editDotColor);
  148. drawDot(g, editPosition);
  149. }
  150. /**
  151. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  152. * <p>
  153. * This Methods draws the Grid on the Canvas.
  154. * @param g2D to draw.
  155. */
  156. private void drawGrid(Graphics2D g2D) {
  157. g2D.setStroke(new BasicStroke(1));
  158. g2D.setColor(Color.lightGray);
  159. int amountOfLines = 10;
  160. int width = widthWithBorder + 2 * border;
  161. int height = heightWithBorder;
  162. for(int i = 0; i<=amountOfLines; i++)
  163. {
  164. int linehieght = (int) (((double)i/ (double) amountOfLines) * (double) height) + border;
  165. g2D.drawLine(0, linehieght, width, linehieght);
  166. }
  167. }
  168. /**
  169. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  170. * <p>
  171. * This Method draws the CurrentIterationLine.
  172. * @param g2D to draw.
  173. */
  174. private void drawCurrentIterartionLine(Graphics2D g)
  175. {
  176. int cur = model.getCurIteration();
  177. int max = model.getIterations();
  178. double where;
  179. if(!this.isUsingLocalPeriod()) {
  180. where = ((double) cur)/((double) max);
  181. }
  182. else
  183. {
  184. int lPeriod = this.getLocalPeriod();
  185. where = ((double) cur%lPeriod)/((double) lPeriod);
  186. }
  187. Position oben = new Position(border + (int)(where * widthWithBorder), 0);
  188. Position unten = new Position(border + (int)(where * widthWithBorder), 2 * border + heightWithBorder);
  189. drawLine(g,oben,unten);
  190. }
  191. /**
  192. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  193. * <p>
  194. * This Method draws a line between two Positions on the Canvas.
  195. * @param g2D to draw.
  196. * @param start the Position of one end of the line to draw.
  197. * @param end the other Ends Position of the Line to draw.
  198. */
  199. private void drawLine(Graphics2D g, Position start, Position end)
  200. {
  201. Path2D.Double path = new Path2D.Double();
  202. path.moveTo(start.x, start.y);
  203. path.lineTo(end.x, end.y);
  204. g.draw(path);
  205. }
  206. /**
  207. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  208. * <p>
  209. * Initialize a Cubic BezierCurve.
  210. * @param start The Position to start the Curve.
  211. */
  212. private Path2D.Double initBezier(Position start) {
  213. //Good Source for basic understanding for Bezier Curves
  214. //http://www.theappguruz.com/blog/bezier-curve-in-games
  215. Path2D.Double path = new Path2D.Double();
  216. path.moveTo(start.x, start.y);
  217. return path;
  218. }
  219. /**
  220. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  221. * <p>
  222. * Calculate the Path of a the Cubic BezierCurve with the special controlPoints to make the wanted behavior.
  223. * @param path the path of the Bezier.
  224. * @param actaul the actual Position of the Path.
  225. * @param target the end Position of the Curve.
  226. */
  227. private void curveTo(Path2D.Double path, Position actual, Position target) {
  228. double mitte = (actual.x + target.x)* 0.5;
  229. path.curveTo(mitte, actual.y, mitte, target.y, target.x, target.y);
  230. }
  231. /**
  232. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  233. * <p>
  234. * Draws a Dot at a Position.
  235. * @param g to draw.
  236. * @param p the position of the Dot.
  237. */
  238. private void drawDot(Graphics2D g, Position p)
  239. {
  240. g.fillOval(p.x -dotSize/2, p.y-dotSize/2, dotSize, dotSize);
  241. }
  242. /**
  243. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  244. * <p>
  245. * This Method draws the UnitGraph as BoolGraph.
  246. * @param g2D to draw.
  247. */
  248. private void drawBoolGraph(Graphics2D g) {
  249. if(actualGraphPoints.size() <= 1) return;
  250. LinkedList<Position> cornerPoints = new LinkedList<Position>();
  251. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  252. Position actual = actualGraphPoints.getFirst().displayedPosition;
  253. Path2D.Double path = new Path2D.Double();
  254. path.moveTo(actual.x, actual.y);
  255. while (iter.hasNext())
  256. {
  257. Position target = iter.next().displayedPosition;
  258. //BooleanConnection
  259. path.lineTo(target.x, actual.y); //line to corner
  260. cornerPoints.add(new Position(target.x, actual.y)); //save corner
  261. path.lineTo(target.x, target.y); //line to next Point
  262. actual = target;
  263. }
  264. g.draw(path);
  265. //Draw the Points on the Corner that dont exist in Data but should be visual
  266. g.setColor(dotColor);
  267. for(Position p: cornerPoints)
  268. {
  269. drawDot(g, p);
  270. }
  271. }
  272. /**
  273. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  274. * <p>
  275. * This Method draws the UnitGraph as BoolGraph in EditMode.
  276. * @param g2D to draw.
  277. */
  278. private void drawBoolGraphInEditMode(Graphics2D g) {
  279. LinkedList<Position> before = new LinkedList<Position>();
  280. LinkedList<Position> after = new LinkedList<Position>();
  281. for(UnitGraphPoint p: actualGraphPoints)
  282. {
  283. if(p.displayedPosition.x < editPosition.x)
  284. before.add(p.displayedPosition);
  285. else
  286. after.add(p.displayedPosition);
  287. }
  288. g.setColor(Color.BLACK);
  289. drawBoolGraphFromList(g, before);
  290. g.setColor(Color.BLACK);
  291. drawBoolGraphFromList(g, after);
  292. //EditGraph
  293. LinkedList<Position> middle = new LinkedList<Position>();
  294. if(!before.isEmpty()) middle.add(before.getLast());
  295. middle.add(editPosition);
  296. if(!after.isEmpty()) middle.add(after.getFirst());
  297. g.setColor(editDotColor);
  298. drawBoolGraphFromList(g, middle);
  299. drawSnappingHint(g);
  300. }
  301. /**
  302. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  303. * <p>
  304. * This Method draws a red Hint to signal the User the snapping of the hovered Point under the Cursor in EditMode.
  305. * @param g2D to draw.
  306. */
  307. private void drawSnappingHint(Graphics2D g)
  308. {
  309. //ColorHint
  310. g.setColor(Color.RED);
  311. //Threshhold Line
  312. final float dash1[] = {10.0f};
  313. final BasicStroke dashed =new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
  314. g.setStroke(dashed);
  315. int halfheight = border + heightWithBorder / 2;
  316. g.drawLine(0, halfheight , widthWithBorder + 2 * border, halfheight);
  317. //Threshhold Text
  318. g.drawString("Snapping Threshold", 10, halfheight - 2);
  319. }
  320. /**
  321. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  322. * <p>
  323. * This Method draws a partial Graph from a Position List as BoolGraph.
  324. * @param g2D to draw.
  325. * @param list the PositionList to draw a BoolGraph
  326. */
  327. private void drawBoolGraphFromList(Graphics2D g, LinkedList<Position> list) {
  328. if(list.size() <= 1) return;
  329. ListIterator<Position> iter = list.listIterator();
  330. LinkedList<Position> cornerPoints = new LinkedList<Position>();
  331. Position actual = list.getFirst();
  332. Path2D.Double path = new Path2D.Double();
  333. path.moveTo(actual.x, actual.y);
  334. while (iter.hasNext())
  335. {
  336. Position target = iter.next();
  337. //BooleanConnection
  338. path.lineTo(target.x, actual.y); //line to corner
  339. cornerPoints.add(new Position(target.x, actual.y)); //save corner
  340. path.lineTo(target.x, target.y); //line to next Point
  341. actual = target;
  342. }
  343. g.draw(path);
  344. g.setColor(dotColor);
  345. for(Position p: cornerPoints)
  346. {
  347. drawDot(g, p);
  348. }
  349. }
  350. /**
  351. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  352. * <p>
  353. * This Method draws the UnitGraph as DoubleGraph.
  354. * @param g2D to draw.
  355. */
  356. private void drawDoubleGraph(Graphics2D g) {
  357. if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
  358. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  359. Position actual = iter.next().displayedPosition;
  360. Path2D.Double path = this.initBezier(actual);
  361. while (iter.hasNext())
  362. {
  363. Position target = iter.next().displayedPosition;
  364. this.curveTo(path, actual, target);
  365. actual = target;
  366. }
  367. g.draw(path);
  368. }
  369. /**
  370. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  371. * <p>
  372. * This Method draws the UnitGraph as DoubleGraph in EditMode.
  373. * @param g2D to draw.
  374. */
  375. private void drawDoubleGraphInEditMode(Graphics2D g) {
  376. LinkedList<Position> before = new LinkedList<Position>();
  377. LinkedList<Position> after = new LinkedList<Position>();
  378. for(UnitGraphPoint p: actualGraphPoints)
  379. {
  380. if(p.displayedPosition.x < editPosition.x)
  381. before.add(p.displayedPosition);
  382. else
  383. after.add(p.displayedPosition);
  384. }
  385. drawUnitGraphFromList(g, before);
  386. drawUnitGraphFromList(g, after);
  387. //EditGraph
  388. LinkedList<Position> middle = new LinkedList<Position>();
  389. if(!before.isEmpty()) middle.add(before.getLast());
  390. middle.add(editPosition);
  391. if(!after.isEmpty()) middle.add(after.getFirst());
  392. g.setColor(editDotColor);
  393. drawUnitGraphFromList(g, middle);
  394. }
  395. /**
  396. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  397. * <p>
  398. * This Method draws a partial Graph from a Position List as DoubleGraph.
  399. * @param g2D to draw.
  400. * @param list the PositionList to draw a DoubleGraph
  401. */
  402. private void drawUnitGraphFromList(Graphics2D g, LinkedList<Position> list) {
  403. if(list.size() <= 1) return;
  404. ListIterator<Position> iter = list.listIterator();
  405. Position actual = list.getFirst();
  406. Path2D.Double path = this.initBezier(actual);
  407. while (iter.hasNext())
  408. {
  409. Position target = iter.next();
  410. curveTo(path, actual, target);
  411. actual = target;
  412. }
  413. g.draw(path);
  414. }
  415. //Under the hood functions to calculate and function the
  416. /**
  417. * A unitgraphpoint have a x and y position to store the data of a graph point.
  418. * Also it have a displayposition to store the Position of the GraphPoints on the Canvas.
  419. * e.g. when the canvas has 500 width and 200 height a GraphPoint with the X=0.5 and Y=1.0 should have a displayposition of (250,3) when border is 3.
  420. */
  421. private void updateRepresentativePositions()
  422. {
  423. for(UnitGraphPoint p : actualGraphPoints) {
  424. p.calcDisplayedPosition(border, widthWithBorder, heightWithBorder);
  425. }
  426. }
  427. /**
  428. * Takes a List of GraphPoints and convert it to the actual UnitGraphPoints with displayposition in the {@link #actualGraphPoints}
  429. * @param stateCurve the list of GraphPoint
  430. */
  431. private void overrideUnitGraph(LinkedList<Point2D.Double> stateCurve) {
  432. actualGraphPoints.clear();
  433. for(Point2D.Double p: stateCurve){
  434. actualGraphPoints.add(new UnitGraphPoint(p));
  435. }
  436. updateRepresentativePositions();
  437. }
  438. /**
  439. * When the PanelSize Change the width and height to calculate the drawings have to be adjusted.
  440. */
  441. private void calculateWidthHeight()
  442. {
  443. widthWithBorder = this.getWidth() - 2 * border;
  444. heightWithBorder = this.getHeight() - 2 * border;
  445. }
  446. /**
  447. * Save the actualGraphPoint List to the GraphEditable Element.
  448. */
  449. private void saveGraph() {
  450. LinkedList<Point2D.Double> actual = actualElement.getStateGraph();
  451. actual.clear();
  452. for(UnitGraphPoint p: actualGraphPoints)
  453. {
  454. actual.add(p.getPoint());
  455. }
  456. actualElement.sampleGraph();
  457. }
  458. /**
  459. * Remove a UnitGraphPoint from the UnitGraphPoint list ({@link #actualGraphPoints} when its near a given Position.
  460. * @param mPosition
  461. */
  462. private void removePointNearPosition(Position mPosition) {
  463. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  464. while (iter.hasNext())
  465. {
  466. if(near(mPosition,iter.next().displayedPosition))
  467. {
  468. iter.remove();
  469. break;
  470. }
  471. }
  472. }
  473. /**
  474. * Determine if the Point is a StartPoint , EndPoint or a NormalPoint a.k.a. in between Points.
  475. * @param mPosition The Position to check.
  476. */
  477. private void detectStartEndPoint(Position mPosition)
  478. {
  479. UnitGraphPoint first = actualGraphPoints.getFirst();
  480. UnitGraphPoint last = actualGraphPoints.getLast();
  481. if(near(mPosition, first.displayedPosition)) editPoint = pointType.StartPoint;
  482. else if(near(mPosition, last.displayedPosition)) editPoint = pointType.EndPoint;
  483. else editPoint = pointType.Normal;
  484. }
  485. /**
  486. * Determine if a Point is near the Cursor (depends on Mode what near means). To detect if it should grab the Point or create a new Point.
  487. * @param actual
  488. * @param target
  489. * @return
  490. */
  491. private boolean near(Position actual, Position target) {
  492. switch(actualGraphType)
  493. {
  494. case boolGraph: //Distance only with X
  495. int xDis = target.x - actual.x;
  496. return xDis * xDis < clickThreshholdSquared;
  497. case doubleGraph:
  498. return actual.squareDistance(target) < clickThreshholdSquared;
  499. default:
  500. return false;
  501. }
  502. }
  503. /**
  504. * When the Mouse Drag a Point it updates each time the position.
  505. * @param newPosition
  506. */
  507. private void updateEditPointPosition(Position newPosition) {
  508. //make it in the bounds of the UnitGraph no Point out of the Border
  509. Position currentPosition = setInBounds(newPosition);
  510. if(editPoint != pointType.Normal) attachToBorder(currentPosition);
  511. if(actualGraphType == Graphtype.boolGraph) snapBoolean(currentPosition);
  512. this.editPosition = currentPosition;
  513. }
  514. /**
  515. * No Point on the UnitGraph should exit the UnitGraph.
  516. * @param p the Position
  517. * @return the updated Position.
  518. */
  519. private Position setInBounds(Position p) {
  520. p.clampX(border, border + widthWithBorder);
  521. p.clampY(border, border + heightWithBorder);
  522. return p;
  523. }
  524. /**
  525. * For Switches the Point have to be Snap to the Top or the Bottem.
  526. * @param p the Position
  527. * @return the updated Position.
  528. */
  529. private Position snapBoolean(Position p)
  530. {
  531. if (p.y < border + heightWithBorder / 2) {
  532. p.y = border;
  533. } else {
  534. p.y = border + heightWithBorder;
  535. }
  536. return p;
  537. }
  538. /**
  539. * The First Point has to be at 0(LeftSide) and Last Point has to be at 1(RightSide).
  540. * @param p the Position
  541. * @return the updated Position.
  542. */
  543. private Position attachToBorder(Position p)
  544. {
  545. switch(editPoint)
  546. {
  547. case StartPoint:
  548. p.x = border;
  549. break;
  550. case EndPoint:
  551. p.x = border + widthWithBorder;
  552. break;
  553. default:
  554. break;
  555. }
  556. return p;
  557. }
  558. /**
  559. * Insert a Position in the UnitGraphList at the right order.
  560. * Its sorted based on the xValues.
  561. * @param pos The new UnitGraphPoints Position
  562. */
  563. private void insertNewGraphPoint(Position pos)
  564. {
  565. setInBounds(pos);
  566. ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
  567. while (iter2.hasNext())
  568. {
  569. Position tempPosition = iter2.next().displayedPosition;
  570. if(pos.x <= tempPosition.x)
  571. {
  572. //previous to go back a position to make the new point before the the Position with greater X
  573. iter2.previous();
  574. iter2.add(generateUnitGraphPoint(pos));
  575. break;
  576. }
  577. }
  578. if(!iter2.hasNext()) //if behind last point
  579. {
  580. iter2.add(generateUnitGraphPoint(pos));
  581. }
  582. }
  583. /**
  584. * Generate a UnitGraphPoint from a normal Position in the UnitGraph.
  585. * @param pos the normal pos with xValues from 0..Width and yValues from 0..Height
  586. * @return a UnitGraphPoint
  587. */
  588. private UnitGraphPoint generateUnitGraphPoint(Position pos) {
  589. UnitGraphPoint temp = new UnitGraphPoint((double) (pos.x - border) / (double) widthWithBorder,
  590. 1 - (double) (pos.y - border) / (double) heightWithBorder, true);
  591. temp.displayedPosition = pos;
  592. return temp;
  593. }
  594. /**
  595. * Update the Point Position
  596. */
  597. @Override
  598. public void mouseDragged(MouseEvent e) {
  599. updateEditPointPosition(new Position(e.getPoint()));
  600. repaint();
  601. }
  602. @Override
  603. public void mouseMoved(MouseEvent e) {
  604. }
  605. @Override
  606. public void mouseClicked(MouseEvent e) {
  607. }
  608. @Override
  609. public void mouseEntered(MouseEvent e) {
  610. }
  611. @Override
  612. public void mouseExited(MouseEvent e) {
  613. }
  614. /**
  615. * The First Step.
  616. * When LeftMouseButton its checks if a point is to grab under the cursor or create a new Point. Then enter EditMode.
  617. * When RightMouseButton its delete a point if its under the Curser.
  618. */
  619. @Override
  620. public void mousePressed(MouseEvent e) {
  621. Position mPosition = new Position(e.getPoint());
  622. if (e.getButton() == MouseEvent.BUTTON3) {
  623. // RightMouseButtonEvent
  624. detectStartEndPoint(mPosition);
  625. if (editPoint == pointType.Normal) {
  626. removePointNearPosition(mPosition);
  627. repaint();
  628. }
  629. editMode = false;
  630. } else if (e.getButton() == MouseEvent.BUTTON1) {
  631. // LeftMouseButtonEvent
  632. detectStartEndPoint(mPosition);
  633. removePointNearPosition(mPosition);
  634. updateEditPointPosition(mPosition);
  635. editMode = true;
  636. repaint();
  637. }
  638. }
  639. /**
  640. * The last step to save the Changes.
  641. * Its insert the Hovering Point and exit EditMode.
  642. */
  643. @Override
  644. public void mouseReleased(MouseEvent e) {
  645. if(editMode)
  646. {
  647. this.insertNewGraphPoint(editPosition);
  648. editMode = false;
  649. repaint();
  650. }
  651. saveGraph();
  652. }
  653. /**
  654. * When the Component is Resized.
  655. *
  656. * @param e ComponentEvent
  657. */
  658. public void componentResized(ComponentEvent e) {
  659. calculateWidthHeight();
  660. updateRepresentativePositions();
  661. repaint();
  662. }
  663. @Override
  664. public void componentHidden(ComponentEvent e) {
  665. }
  666. @Override
  667. public void componentMoved(ComponentEvent e) {
  668. }
  669. @Override
  670. public void componentShown(ComponentEvent e) {
  671. }
  672. /**
  673. * Resets the graph to normal.
  674. */
  675. public void reset() {
  676. if(this.actualElement != null) {
  677. actualElement.reset();
  678. overrideUnitGraph(actualElement.getStateGraph());
  679. repaint();
  680. }
  681. }
  682. //LocalMode access methods...
  683. //To access a element from the GUI for the LocalMode
  684. public void setUseLocalPeriod(boolean state) {
  685. if(this.actualElement != null) {
  686. ((LocalMode) actualElement).setUseLocalPeriod(state);
  687. }
  688. }
  689. public void setLocalPeriod(int localLength) {
  690. if(this.actualElement != null) {
  691. ((LocalMode) actualElement).setLocalPeriod(localLength);
  692. }
  693. }
  694. public int getLocalPeriod() {
  695. if(this.actualElement != null) {
  696. return ((LocalMode) actualElement).getLocalPeriod();
  697. }
  698. return -1;
  699. }
  700. public boolean isUsingLocalPeriod() {
  701. if(this.actualElement != null) {
  702. return ((LocalMode) actualElement).isUsingLocalPeriod();
  703. }
  704. return false;
  705. }
  706. /**
  707. * @deprecated
  708. * @param cps
  709. */
  710. public void repaintGraph(AbstractCanvasObject cps) {
  711. System.out.println("repaintGraph");
  712. }
  713. /**
  714. * @deprecated
  715. * @param obj
  716. */
  717. public void update(ArrayList<AbstractCanvasObject> obj) {
  718. }
  719. }