UnitGraph.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.event.ComponentEvent;
  8. import java.awt.event.ComponentListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.event.MouseMotionListener;
  12. import java.awt.geom.CubicCurve2D;
  13. import java.awt.geom.GeneralPath;
  14. import java.awt.geom.Line2D;
  15. import java.util.LinkedList;
  16. import java.awt.Point;
  17. import javax.swing.JPanel;
  18. import classes.HolonElement;
  19. import ui.controller.Control;
  20. import ui.model.Model;
  21. import classes.HolonSwitch;
  22. import java.awt.Cursor;
  23. class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  24. private static final long serialVersionUID = 1L;
  25. private float MAXIMUM = 0;
  26. private Point recSize = new Point(8, 8); // Point Size
  27. private Graphics2D g2;
  28. private CubicCurve2D c = new CubicCurve2D.Double();
  29. private CubicCurve2D cr = new CubicCurve2D.Double();
  30. private CubicCurve2D cl = new CubicCurve2D.Double();
  31. private LinkedList<Point> pointList;
  32. private double scaleX;
  33. private double scaleY;
  34. private float[] arrayOfFloats = null;
  35. private boolean[] arrayOfBooleans = null;
  36. private double width = -1;
  37. private double height = -1;
  38. private boolean isElement = false;
  39. private boolean isSwitch = false;
  40. private HolonElement tempElement;
  41. private HolonSwitch tempSwitch;
  42. private Model model;
  43. private Control controller;
  44. private Line2D.Double line = null;
  45. GeneralPath graphCurve = new GeneralPath();
  46. private boolean pointDrag = false;
  47. private boolean init = true;
  48. private Point tempP = null;
  49. private double x = 0, y = 0;
  50. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  51. public UnitGraph(final Model model, Control control) {
  52. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  53. this.controller = control;
  54. this.model = model;
  55. this.pointList = new LinkedList<>();
  56. this.setBackground(Color.WHITE);
  57. this.addMouseListener(this);
  58. this.addMouseMotionListener(this);
  59. this.addComponentListener(this);
  60. }
  61. /**
  62. * Paints all Components on the Canvas
  63. *
  64. * @param Graphics
  65. *
  66. */
  67. public void paintComponent(Graphics g) {
  68. super.paintComponent(g);
  69. g2 = (Graphics2D) g;
  70. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  71. g2.setRenderingHints(rh);
  72. g2.setStroke(new BasicStroke(0));
  73. graphCurve.reset();
  74. // Draw the Vertical Lines
  75. g2.setColor(Color.BLACK);
  76. for (int i = 0; i <= this.getWidth(); i += 10) {
  77. g2.drawLine(i, 0, i, this.getHeight());
  78. }
  79. for (int i = 0; i <= this.getHeight(); i += 5) {
  80. g2.drawLine(0, i, this.getWidth(), i);
  81. }
  82. if (isElement) {
  83. if (arrayOfFloats != null) {
  84. // array fillen
  85. fillArrayofValue();
  86. // Draw the Lines
  87. g2.setStroke(new BasicStroke(2));
  88. g2.setColor(Color.BLACK);
  89. for (int i = 0; i < pointList.size() - 1; i++) {
  90. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  91. graphCurve.append(c, true);
  92. }
  93. g2.draw(graphCurve);
  94. // Draw the Points
  95. g2.setColor(Color.BLUE);
  96. for (int i = 0; i < pointList.size() - 0; i++) {
  97. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  98. (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  99. (int) recSize.getY());
  100. }
  101. // Iteration Value
  102. if (arrayOfFloats != null) {
  103. g2.drawString("" + arrayOfFloats[model.getCurIteration()],
  104. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  105. this.getHeight() - 10);
  106. }
  107. }
  108. /*
  109. * // Actual Iteration Point Visualization g2.setColor(Color.RED);
  110. * if (arrayOfValue != null) { for (int i = 0; i <
  111. * arrayOfValue.length; i++) { g2.fillOval((int) (i * width /
  112. * (model.getIterations() - 1) * scaleX - recSize.getX() / 2), (int)
  113. * (convertToCanvasY((int) arrayOfValue[i]) * scaleY -
  114. * recSize.getY() / 2), (int) recSize.getX(), (int) recSize.getY());
  115. * } }
  116. */
  117. } else if (isSwitch) {
  118. if (arrayOfBooleans != null) {
  119. // array fillen
  120. fillArrayofBooleans();
  121. // Draw the Lines
  122. g2.setStroke(new BasicStroke(1));
  123. g2.setColor(Color.BLACK);
  124. for (int i = 0; i < pointList.size() - 1; i++) {
  125. line = new Line2D.Double(pointList.get(i).getX() * scaleX, pointList.get(i).getY() * scaleY,
  126. pointList.get(i + 1).getX() * scaleX, pointList.get(i + 1).getY() * scaleY);
  127. graphCurve.append(line, true);
  128. }
  129. g2.draw(graphCurve);
  130. /*
  131. // Draw the Points
  132. g2.setColor(Color.BLUE);
  133. for (int i = 0; i < pointList.size() - 0; i++) {
  134. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  135. (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  136. (int) recSize.getY());
  137. }
  138. */
  139. // Iteration Value
  140. if (arrayOfBooleans != null) {
  141. g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
  142. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  143. this.getHeight() - 10);
  144. }
  145. }
  146. }
  147. // Iteration Line
  148. g2.setColor(Color.BLUE);
  149. g2.setStroke(new BasicStroke(1));
  150. g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
  151. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
  152. }
  153. @Override
  154. public void mouseDragged(MouseEvent e) {
  155. if (isElement) {
  156. elementDragged(e);
  157. } else if (isSwitch) {
  158. switchDragged(e);
  159. }
  160. }
  161. /**
  162. * Wenn ein Punkt bei einem HolonElement gedragged wird
  163. *
  164. * @param e
  165. */
  166. public void elementDragged(MouseEvent e) {
  167. if (pointDrag && tempP != null) {
  168. // Out of Bounds verhindern
  169. int i = pointList.indexOf(tempP);
  170. x = e.getX() / scaleX;
  171. y = e.getY() / scaleY;
  172. // y
  173. if (e.getY() <= 0) {
  174. y = 0 / scaleY;
  175. } else if (this.getHeight() <= e.getY()) {
  176. y = this.getHeight() / scaleY;
  177. }
  178. // x
  179. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
  180. || pointList.get(i - 1).getX() >= x) {
  181. x = tempP.getX();
  182. }
  183. tempP.setLocation(x, y);
  184. repaint();
  185. }
  186. }
  187. /**
  188. * Wenn ein Punkt bei einem CpsSwitch gedragged wird
  189. *
  190. * @param e
  191. */
  192. public void switchDragged(MouseEvent e) {
  193. // TODO Switch dragged zeugs kommt hier hin
  194. }
  195. @Override
  196. public void mouseMoved(MouseEvent e) {
  197. }
  198. @Override
  199. public void mouseClicked(MouseEvent e) {
  200. }
  201. @Override
  202. public void mouseEntered(MouseEvent e) {
  203. }
  204. @Override
  205. public void mouseExited(MouseEvent e) {
  206. }
  207. @Override
  208. public void mousePressed(MouseEvent e) {
  209. if (isElement) {
  210. elementPressed(e);
  211. } else if (isSwitch) {
  212. switchPressed(e);
  213. }
  214. }
  215. /**
  216. * Wenn ein Punkt von einem Element gedrück wird
  217. *
  218. * @param e
  219. */
  220. public void elementPressed(MouseEvent e) {
  221. boolean added = false;
  222. boolean deletePoint = false;
  223. double x = e.getX() / scaleX;
  224. double y = e.getY() / scaleY;
  225. // Click on Point
  226. tempP = null;
  227. if (pointList != null) {
  228. // look if a point was clicked
  229. for (Point p : pointList) {
  230. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  231. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  232. if (e.getButton() == MouseEvent.BUTTON3) {
  233. tempP = p;
  234. deletePoint = true;
  235. } else {
  236. pointDrag = true;
  237. tempP = p;
  238. }
  239. }
  240. }
  241. // New Point
  242. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  243. && e.getX() != this.getWidth() / scaleX) {
  244. for (int i = 0; i < pointList.size(); i++) {
  245. if (x < pointList.get(i).getX() && !added) {
  246. if (e.getY() <= 0) {
  247. pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
  248. } else {
  249. pointList.add(i, new Point((int) (x), (int) y));
  250. }
  251. added = true;
  252. pointDrag = true;
  253. tempP = pointList.get(i);
  254. }
  255. }
  256. }
  257. // Delete a Point
  258. if (deletePoint && tempP.getX() != 0
  259. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  260. pointList.remove(tempP);
  261. }
  262. repaint();
  263. }
  264. }
  265. /**
  266. * Wenn ein Punkt von einem Switch gedrück wird
  267. *
  268. * @param e
  269. */
  270. public void switchPressed(MouseEvent e) {
  271. boolean added = false;
  272. boolean deletePoint = false;
  273. double x = e.getX() / scaleX;
  274. double y = e.getY() / scaleY;
  275. // Click on Point
  276. tempP = null;
  277. if (pointList != null) {
  278. for (Point p : pointList) {
  279. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  280. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  281. if (e.getButton() == MouseEvent.BUTTON3) {
  282. tempP = p;
  283. deletePoint = true;
  284. } else {
  285. pointDrag = true;
  286. tempP = p;
  287. }
  288. }
  289. }
  290. // New Point
  291. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
  292. for (int i = 0; i < pointList.size(); i++) {
  293. if (x < pointList.get(i).getX() && !added) {
  294. double it = 0;
  295. double dist = (1 * this.getWidth() / (model.getIterations() - 1))/scaleX/2;
  296. //Iterationspuunkt in der Nähe vom Click holen
  297. for (int j = 0; (j < model.getIterations() && it < x); j++) {
  298. it = (j * this.getWidth() / (model.getIterations() - 1))/scaleX;
  299. }
  300. //Punkt hinzufügen, je nachdem ob es oberhalb oder unterhalb der hälfte ist
  301. if (y < height / 2 && (pointList.get(i - 1).getY() != 0 || pointList.get(i).getY() != 0)) {
  302. pointList.add(i, new Point((int) (it+dist), (int) height-1));
  303. pointList.add(i, new Point((int) (it+dist), 0));
  304. pointList.add(i, new Point((int) (it-dist), 0));
  305. pointList.add(i, new Point((int) (it-dist), (int) height-1));
  306. } else if (x >= height/2 && (pointList.get(i-1).getY() != height-1 || pointList.get(i ).getY() != height-1)) {
  307. pointList.add(i, new Point((int) (it+dist), 0));
  308. pointList.add(i, new Point((int) (it+dist), (int) height-1));
  309. pointList.add(i, new Point((int) (it-dist), (int) height-1));
  310. pointList.add(i, new Point((int) (it-dist), 0));
  311. }
  312. added = true;
  313. }
  314. }
  315. }
  316. repaint();
  317. }
  318. // TODO Siwtch pressed zeugs hier hin
  319. }
  320. @Override
  321. public void mouseReleased(MouseEvent e) {
  322. if (pointDrag) {
  323. pointDrag = false;
  324. tempP = null;
  325. }
  326. }
  327. public void componentResized(ComponentEvent e) {
  328. // Wenn ein anderes Element genommen wird
  329. if (init) {
  330. init = false;
  331. // for scale on the first initialisation
  332. if (width == -1 && height == -1) {
  333. width = this.getWidth();
  334. height = this.getHeight();
  335. }
  336. scaleX = this.getWidth() / width;
  337. scaleY = this.getHeight() / height;
  338. }
  339. // Scale
  340. scaleX = this.getWidth() / width;
  341. scaleY = this.getHeight() / height;
  342. repaint();
  343. }
  344. @Override
  345. public void componentHidden(ComponentEvent e) {
  346. }
  347. @Override
  348. public void componentMoved(ComponentEvent e) {
  349. }
  350. @Override
  351. public void componentShown(ComponentEvent e) {
  352. }
  353. /*
  354. * Emptys the Graph
  355. */
  356. public void empty() {
  357. pointList = null;
  358. tempElement = null;
  359. tempSwitch = null;
  360. arrayOfFloats = null;
  361. arrayOfBooleans = null;
  362. isSwitch = false;
  363. isElement = false;
  364. repaint();
  365. }
  366. /*
  367. * Resets the Points for the Element
  368. */
  369. public void reset() {
  370. pointList.removeAll(pointList);
  371. pointList.addFirst(new Point(0, 0));
  372. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  373. repaint();
  374. }
  375. /**
  376. * converts the number to fit the canvas
  377. *
  378. * @param double
  379. * d, the number to convert
  380. * @return the converted number
  381. */
  382. public double convertToCanvasY(float d) {
  383. return (height - (d * (height / MAXIMUM)));
  384. }
  385. /**
  386. * converts the number to fit the value
  387. *
  388. * @param double
  389. * d, the number to convert
  390. * @return the converted number
  391. */
  392. public float convertToValueY(double d) {
  393. return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
  394. }
  395. /**
  396. * Visualize the HolonElement on the Graph
  397. *
  398. * @param HolonElement
  399. * ele, which should be visualized
  400. */
  401. public void repaintWithNewElement(HolonElement ele) {
  402. arrayOfFloats = ele.getEnergyAt();
  403. tempElement = ele;
  404. pointList = ele.getGraphPoints();
  405. isSwitch = false;
  406. isElement = true;
  407. MAXIMUM = tempElement.getEnergy();
  408. // First time clicked on the Element
  409. if (pointList.isEmpty()) {
  410. pointList.addFirst(new Point(0, 0));
  411. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  412. }
  413. repaint();
  414. }
  415. /**
  416. * Visualize the HolonElement on the Graph
  417. *
  418. * @param HolonElement
  419. * ele, which should be visualized
  420. */
  421. public void repaintWithNewSwitch(HolonSwitch s) {
  422. arrayOfBooleans = s.getActiveAt();
  423. tempSwitch = s;
  424. pointList = s.getGraphPoints();
  425. isSwitch = true;
  426. isElement = false;
  427. // First time clicked on the Element
  428. if (pointList.isEmpty()) {
  429. pointList.addFirst(new Point(0, 0));
  430. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  431. }
  432. repaint();
  433. }
  434. /**
  435. * Build a Curve for the Graph
  436. *
  437. * @param Point,Point
  438. * ,startpoint p1 and endpoint p2
  439. *
  440. * @return CubicCurve2D, c, the CubicCurve2D for the Graph
  441. */
  442. public CubicCurve2D buildCurve(Point p1, Point p2) {
  443. x1 = (int) p1.getX();
  444. y1 = (int) p1.getY();
  445. x2 = (int) p2.getX();
  446. y2 = (int) p2.getY();
  447. // calculate the controllpoints
  448. ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
  449. ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
  450. if (y1 < y2) {
  451. ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
  452. ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
  453. } else {
  454. ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
  455. ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
  456. }
  457. // set the curve
  458. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  459. x2 * scaleX, y2 * scaleY);
  460. return c;
  461. }
  462. /**
  463. * Fills the Arrays with booleans
  464. */
  465. public void fillArrayofBooleans() {
  466. for (int i = 0; i < arrayOfBooleans.length; i++) {
  467. arrayOfBooleans[i] = true;
  468. }
  469. }
  470. /**
  471. * Fills the Arrays of each HolonElement
  472. */
  473. public void fillArrayofValue() {
  474. for (int i = 0; i < arrayOfFloats.length; i++) {
  475. arrayOfFloats[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
  476. }
  477. }
  478. /**
  479. *
  480. * @param xVal,
  481. * the x value for the y value
  482. * @return y, the value at x
  483. */
  484. public float getYValueAt(int xVal) {
  485. for (int i = 0; i < pointList.size() - 1; i++) {
  486. // get the Points
  487. if (xVal <= pointList.get(i + 1).getX()) {
  488. // Curve erstellen
  489. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  490. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  491. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  492. return getIntersectionPoint(l1, l2);
  493. }
  494. }
  495. return 0;
  496. }
  497. /**
  498. *
  499. * @param xVal,
  500. * the x value for the y value
  501. * @return y, the value at x
  502. */
  503. public float getYValueAt_2(int xVal) {
  504. for (int i = 0; i < pointList.size() - 1; i++) {
  505. // get the Points
  506. if (xVal >= pointList.get(i).getX()) {
  507. // Curve erstellen
  508. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  509. c.subdivide(cl, cr);
  510. // Teil der Kurve aussuchen
  511. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  512. c = cl;
  513. // Kurve Links von "unten"
  514. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  515. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  516. if (c.contains(xVal * scaleX, j * scaleY)) {
  517. return (float) (j);
  518. }
  519. }
  520. } else {// Kurve Links von "oben"
  521. for (float j = 0; j < height; j += 0.1f) {
  522. if (c.contains(xVal * scaleX, j * scaleY)) {
  523. return (float) (j);
  524. }
  525. }
  526. }
  527. } else {
  528. c = cr;
  529. // Kurve Links von "unten"
  530. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  531. for (float j = 0; j < height; j += 0.1f) {
  532. if (c.contains(xVal * scaleX, j * scaleY)) {
  533. return (float) (j);
  534. }
  535. }
  536. } else {// Kurve Links von "oben"
  537. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  538. if (c.contains(xVal * scaleX, j * scaleY)) {
  539. return (float) (j);
  540. }
  541. }
  542. }
  543. }
  544. }
  545. }
  546. return getYValueAt(xVal);
  547. }
  548. /**
  549. *
  550. * @param l1,
  551. * the first Line
  552. * @param l2,
  553. * the second Line
  554. *
  555. * @return The Intersection Point
  556. */
  557. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  558. if (!l1.intersectsLine(l2)) {
  559. return 0;// null;
  560. }
  561. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  562. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  563. double det = sx * ry - sy * rx;
  564. if (det == 0) {
  565. return 0;// null;
  566. } else {
  567. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  568. if (z < 0 || z > 1) {
  569. return 0;// new Point(0, 0); // intersection at end point!
  570. }
  571. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  572. // (py + z * ry));
  573. }
  574. } // end intersection line-line
  575. }