UnitGraph.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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(2));
  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, pointList.get(i+1).getX()*scaleX,pointList.get(i+1).getY()*scaleY);
  126. graphCurve.append(line, true);
  127. }
  128. g2.draw(graphCurve);
  129. // Draw the Points
  130. g2.setColor(Color.BLUE);
  131. for (int i = 0; i < pointList.size() - 0; i++) {
  132. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  133. (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  134. (int) recSize.getY());
  135. }
  136. // Iteration Value
  137. if (arrayOfFloats != null) {
  138. g2.drawString("" + arrayOfFloats[model.getCurIteration()],
  139. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  140. this.getHeight() - 10);
  141. }
  142. }
  143. }
  144. // Iteration Line
  145. g2.setColor(Color.BLUE);
  146. g2.setStroke(new BasicStroke(1));
  147. g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
  148. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
  149. }
  150. @Override
  151. public void mouseDragged(MouseEvent e) {
  152. if (isElement) {
  153. elementDragged(e);
  154. } else if (isSwitch) {
  155. switchDragged(e);
  156. }
  157. }
  158. /**
  159. * Wenn ein Punkt bei einem HolonElement gedragged wird
  160. *
  161. * @param e
  162. */
  163. public void elementDragged(MouseEvent e) {
  164. if (pointDrag && tempP != null) {
  165. // Out of Bounds verhindern
  166. int i = pointList.indexOf(tempP);
  167. x = e.getX() / scaleX;
  168. y = e.getY() / scaleY;
  169. // y
  170. if (e.getY() <= 0) {
  171. y = 0 / scaleY;
  172. } else if (this.getHeight() <= e.getY()) {
  173. y = this.getHeight() / scaleY;
  174. }
  175. // x
  176. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
  177. || pointList.get(i - 1).getX() >= x) {
  178. x = tempP.getX();
  179. }
  180. tempP.setLocation(x, y);
  181. repaint();
  182. }
  183. }
  184. /**
  185. * Wenn ein Punkt bei einem CpsSwitch gedragged wird
  186. *
  187. * @param e
  188. */
  189. public void switchDragged(MouseEvent e) {
  190. // TODO Switch dragged zeugs kommt hier hin
  191. }
  192. @Override
  193. public void mouseMoved(MouseEvent e) {
  194. }
  195. @Override
  196. public void mouseClicked(MouseEvent e) {
  197. }
  198. @Override
  199. public void mouseEntered(MouseEvent e) {
  200. }
  201. @Override
  202. public void mouseExited(MouseEvent e) {
  203. }
  204. @Override
  205. public void mousePressed(MouseEvent e) {
  206. if (isElement) {
  207. elementPressed(e);
  208. } else if (isSwitch) {
  209. switchPressed(e);
  210. }
  211. }
  212. /**
  213. * Wenn ein Punkt von einem Element gedrueckt wird
  214. *
  215. * @param e
  216. */
  217. public void elementPressed(MouseEvent e) {
  218. boolean added = false;
  219. boolean deletePoint = false;
  220. double x = e.getX() / scaleX;
  221. double y = e.getY() / scaleY;
  222. // Click on Point
  223. tempP = null;
  224. if (pointList != null) {
  225. for (Point p : pointList) {
  226. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  227. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  228. if (e.getButton() == MouseEvent.BUTTON3) {
  229. tempP = p;
  230. deletePoint = true;
  231. } else {
  232. pointDrag = true;
  233. tempP = p;
  234. }
  235. }
  236. }
  237. // New Point
  238. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  239. && e.getX() != this.getWidth() / scaleX) {
  240. for (int i = 0; i < pointList.size(); i++) {
  241. if (x < pointList.get(i).getX() && !added) {
  242. if (e.getY() <= 0) {
  243. pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
  244. } else {
  245. pointList.add(i, new Point((int) (x), (int) y));
  246. }
  247. added = true;
  248. pointDrag = true;
  249. tempP = pointList.get(i);
  250. }
  251. }
  252. }
  253. // Delete a Point
  254. if (deletePoint && tempP.getX() != 0
  255. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  256. pointList.remove(tempP);
  257. }
  258. repaint();
  259. }
  260. }
  261. /**
  262. * Wenn ein Punkt von einem Switch gedr�ck wird
  263. *
  264. * @param e
  265. */
  266. public void switchPressed(MouseEvent e) {
  267. // TODO Siwtch pressed zeugs hier hin
  268. }
  269. @Override
  270. public void mouseReleased(MouseEvent e) {
  271. if (pointDrag) {
  272. pointDrag = false;
  273. tempP = null;
  274. }
  275. }
  276. public void componentResized(ComponentEvent e) {
  277. // Wenn ein anderes Element genommen wird
  278. if (init) {
  279. init = false;
  280. // for scale on the first initialisation
  281. if (width == -1 && height == -1) {
  282. width = this.getWidth();
  283. height = this.getHeight();
  284. }
  285. scaleX = this.getWidth() / width;
  286. scaleY = this.getHeight() / height;
  287. }
  288. // Scale
  289. scaleX = this.getWidth() / width;
  290. scaleY = this.getHeight() / height;
  291. repaint();
  292. }
  293. @Override
  294. public void componentHidden(ComponentEvent e) {
  295. }
  296. @Override
  297. public void componentMoved(ComponentEvent e) {
  298. }
  299. @Override
  300. public void componentShown(ComponentEvent e) {
  301. }
  302. /*
  303. * Emptys the Graph
  304. */
  305. public void empty() {
  306. pointList = null;
  307. tempElement = null;
  308. tempSwitch = null;
  309. arrayOfFloats = null;
  310. arrayOfBooleans = null;
  311. isSwitch = false;
  312. isElement = false;
  313. repaint();
  314. }
  315. /*
  316. * Resets the Points for the Element
  317. */
  318. public void reset() {
  319. pointList.removeAll(pointList);
  320. pointList.addFirst(new Point(0, 0));
  321. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  322. repaint();
  323. }
  324. /**
  325. * converts the number to fit the canvas
  326. *
  327. * @param double
  328. * d, the number to convert
  329. * @return the converted number
  330. */
  331. public double convertToCanvasY(float d) {
  332. return (height - (d * (height / MAXIMUM)));
  333. }
  334. /**
  335. * converts the number to fit the value
  336. *
  337. * @param double
  338. * d, the number to convert
  339. * @return the converted number
  340. */
  341. public float convertToValueY(double d) {
  342. return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
  343. }
  344. /**
  345. * Visualize the HolonElement on the Graph
  346. *
  347. * @param HolonElement
  348. * ele, which should be visualized
  349. */
  350. public void repaintWithNewElement(HolonElement ele) {
  351. arrayOfFloats = ele.getEnergyAt();
  352. tempElement = ele;
  353. pointList = ele.getGraphPoints();
  354. isSwitch = false;
  355. isElement = true;
  356. MAXIMUM = tempElement.getEnergy();
  357. // First time clicked on the Element
  358. if (pointList.isEmpty()) {
  359. pointList.addFirst(new Point(0, 0));
  360. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  361. }
  362. repaint();
  363. }
  364. /**
  365. * Visualize the HolonElement on the Graph
  366. *
  367. * @param HolonElement
  368. * ele, which should be visualized
  369. */
  370. public void repaintWithNewSwitch(HolonSwitch s) {
  371. arrayOfBooleans = s.getActiveAt();
  372. tempSwitch = s;
  373. pointList = s.getGraphPoints();
  374. isSwitch = true;
  375. isElement = false;
  376. // First time clicked on the Element
  377. if (pointList.isEmpty()) {
  378. pointList.addFirst(new Point(0, (int) (this.getHeight()/scaleY)));
  379. pointList.addLast(new Point((int) (this.getWidth() / scaleX), (int) (this.getHeight()/scaleY)));
  380. }
  381. repaint();
  382. }
  383. /**
  384. * Build a Curve for the Graph
  385. *
  386. * @param Point,Point
  387. * ,startpoint p1 and endpoint p2
  388. *
  389. * @return CubicCurve2D, c, the CubicCurve2D for the Graph
  390. */
  391. public CubicCurve2D buildCurve(Point p1, Point p2) {
  392. x1 = (int) p1.getX();
  393. y1 = (int) p1.getY();
  394. x2 = (int) p2.getX();
  395. y2 = (int) p2.getY();
  396. // calculate the controllpoints
  397. ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
  398. ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
  399. if (y1 < y2) {
  400. ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
  401. ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
  402. } else {
  403. ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
  404. ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
  405. }
  406. // set the curve
  407. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  408. x2 * scaleX, y2 * scaleY);
  409. return c;
  410. }
  411. /**
  412. * Fills the Arrays with booleans
  413. */
  414. public void fillArrayofBooleans() {
  415. for (int i = 0; i < arrayOfBooleans.length; i++) {
  416. arrayOfBooleans[i] = true;
  417. }
  418. }
  419. /**
  420. * Fills the Arrays of each HolonElement
  421. */
  422. public void fillArrayofValue() {
  423. for (int i = 0; i < arrayOfFloats.length; i++) {
  424. arrayOfFloats[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
  425. }
  426. }
  427. /**
  428. *
  429. * @param xVal,
  430. * the x value for the y value
  431. * @return y, the value at x
  432. */
  433. public float getYValueAt(int xVal) {
  434. for (int i = 0; i < pointList.size() - 1; i++) {
  435. // get the Points
  436. if (xVal <= pointList.get(i + 1).getX()) {
  437. // Curve erstellen
  438. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  439. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  440. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  441. return getIntersectionPoint(l1, l2);
  442. }
  443. }
  444. return 0;
  445. }
  446. /**
  447. *
  448. * @param xVal,
  449. * the x value for the y value
  450. * @return y, the value at x
  451. */
  452. public float getYValueAt_2(int xVal) {
  453. for (int i = 0; i < pointList.size() - 1; i++) {
  454. // get the Points
  455. if (xVal >= pointList.get(i).getX()) {
  456. // Curve erstellen
  457. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  458. c.subdivide(cl, cr);
  459. // Teil der Kurve aussuchen
  460. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  461. c = cl;
  462. // Kurve Links von "unten"
  463. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  464. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  465. if (c.contains(xVal * scaleX, j * scaleY)) {
  466. return (float) (j);
  467. }
  468. }
  469. } else {// Kurve Links von "oben"
  470. for (float j = 0; j < height; j += 0.1f) {
  471. if (c.contains(xVal * scaleX, j * scaleY)) {
  472. return (float) (j);
  473. }
  474. }
  475. }
  476. } else {
  477. c = cr;
  478. // Kurve Links von "unten"
  479. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  480. for (float j = 0; j < height; j += 0.1f) {
  481. if (c.contains(xVal * scaleX, j * scaleY)) {
  482. return (float) (j);
  483. }
  484. }
  485. } else {// Kurve Links von "oben"
  486. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  487. if (c.contains(xVal * scaleX, j * scaleY)) {
  488. return (float) (j);
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. return getYValueAt(xVal);
  496. }
  497. /**
  498. *
  499. * @param l1,
  500. * the first Line
  501. * @param l2,
  502. * the second Line
  503. *
  504. * @return The Intersection Point
  505. */
  506. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  507. if (!l1.intersectsLine(l2)) {
  508. return 0;// null;
  509. }
  510. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  511. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  512. double det = sx * ry - sy * rx;
  513. if (det == 0) {
  514. return 0;// null;
  515. } else {
  516. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  517. if (z < 0 || z > 1) {
  518. return 0;// new Point(0, 0); // intersection at end point!
  519. }
  520. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  521. // (py + z * ry));
  522. }
  523. } // end intersection line-line
  524. }