UnitGraph.java 14 KB

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