UnitGraph.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 java.awt.Cursor;
  22. class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  23. private static final long serialVersionUID = 1L;
  24. private float MAXIMUM = 0;
  25. private Point recSize = new Point(8, 8); // Point Size
  26. private Graphics2D g2;
  27. private CubicCurve2D c = new CubicCurve2D.Double();
  28. private CubicCurve2D cr = new CubicCurve2D.Double();
  29. private CubicCurve2D cl = new CubicCurve2D.Double();
  30. private LinkedList<Point> pointList;
  31. private double scaleX;
  32. private double scaleY;
  33. private float[] arrayOfValue = null;
  34. private double width = -1;
  35. private double height = -1;
  36. private HolonElement tempElement;
  37. private Model model;
  38. private Control controller;
  39. GeneralPath graphCurve = new GeneralPath();
  40. private boolean pointDrag = false;
  41. private boolean init = false;
  42. private Point tempP = null, p1 = null, p2 = null;
  43. private double x = 0, y = 0;
  44. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  45. public UnitGraph(final Model model, Control control) {
  46. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  47. this.controller = control;
  48. this.model = model;
  49. this.pointList = new LinkedList<>();
  50. this.addMouseListener(this);
  51. this.addMouseMotionListener(this);
  52. this.addComponentListener(this);
  53. }
  54. /**
  55. * Paints all Components on the Canvas
  56. *
  57. * @param Graphics
  58. *
  59. */
  60. public void paintComponent(Graphics g) {
  61. super.paintComponent(g);
  62. g2 = (Graphics2D) g;
  63. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  64. g2.setRenderingHints(rh);
  65. g2.setStroke(new BasicStroke(1));
  66. graphCurve.reset();
  67. // Draw the Vertical Lines
  68. g2.setColor(new Color(240, 240, 240));
  69. for (int i = 0; i < model.getIterations(); i++) {
  70. g2.drawLine((i) * this.getWidth() / (model.getIterations() - 1), 0,
  71. (i) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
  72. }
  73. for (int i = 0; i < model.getIterations(); i++) {
  74. g2.drawLine(0, (i) * this.getHeight() / (model.getIterations() - 1), this.getWidth(),
  75. (i) * this.getHeight() / (model.getIterations() - 1));
  76. }
  77. if (arrayOfValue != null) {
  78. //array fillen
  79. fillArrayofValue();
  80. // Draw the Lines
  81. g2.setColor(Color.BLACK);
  82. for (int i = 0; i < pointList.size() - 1; i++) {
  83. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  84. graphCurve.append(c, true);
  85. }
  86. g2.draw(graphCurve);
  87. // Draw the Points
  88. g2.setColor(Color.BLUE);
  89. for (int i = 0; i < pointList.size() - 0; i++) {
  90. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  91. (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  92. (int) recSize.getY());
  93. }
  94. }
  95. // Iteration Line
  96. g2.setColor(Color.BLUE);
  97. g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
  98. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
  99. // Iteration Value
  100. if (arrayOfValue != null) {
  101. if (model.getCurIteration() > model.getIterations() / 2) {
  102. g2.drawString("" + arrayOfValue[model.getCurIteration()],
  103. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) - 30,
  104. this.getHeight() / 2);
  105. } else {
  106. g2.drawString("" + arrayOfValue[model.getCurIteration()],
  107. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  108. this.getHeight() / 2);
  109. }
  110. }
  111. // Actual Iteration Point Visualization
  112. g2.setColor(Color.RED);
  113. if (arrayOfValue != null) {
  114. for (int i = 0; i < arrayOfValue.length; i++) {
  115. g2.fillOval((int) (i * width / (model.getIterations() - 1) * scaleX - recSize.getX() / 2),
  116. (int) (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() / 2),
  117. (int) recSize.getX(), (int) recSize.getY());
  118. }
  119. }
  120. }
  121. @Override
  122. public void mouseDragged(MouseEvent e) {
  123. if (pointDrag && tempP != null) {
  124. // Out of Bounds verhindern
  125. int i = pointList.indexOf(tempP);
  126. x = e.getX() / scaleX;
  127. y = e.getY() / scaleY;
  128. // y
  129. if (e.getY() <= 0) {
  130. y = 0 / scaleY;
  131. } else if (this.getHeight() <= e.getY()) {
  132. y = this.getHeight() / scaleY;
  133. }
  134. // x
  135. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
  136. || pointList.get(i - 1).getX() >= x) {
  137. x = tempP.getX();
  138. }
  139. tempP.setLocation(x, y);
  140. repaint();
  141. }
  142. }
  143. @Override
  144. public void mouseMoved(MouseEvent e) {
  145. // TODO Auto-generated method stub
  146. }
  147. @Override
  148. public void mouseClicked(MouseEvent e) {
  149. // TODO Auto-generated method stub
  150. }
  151. @Override
  152. public void mouseEntered(MouseEvent e) {
  153. // TODO Auto-generated method stub
  154. }
  155. @Override
  156. public void mouseExited(MouseEvent e) {
  157. // TODO Auto-generated method stub
  158. }
  159. @Override
  160. public void mousePressed(MouseEvent e) {
  161. boolean added = false;
  162. boolean deletePoint = false;
  163. double x = e.getX() / scaleX;
  164. double y = e.getY() / scaleY;
  165. // Click on Point
  166. tempP = null;
  167. if (pointList != null) {
  168. for (Point p : pointList) {
  169. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  170. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  171. if (e.getButton() == MouseEvent.BUTTON3) {
  172. tempP = p;
  173. deletePoint = true;
  174. } else {
  175. pointDrag = true;
  176. tempP = p;
  177. }
  178. }
  179. }
  180. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  181. && e.getX() != this.getWidth() / scaleX) {
  182. for (int i = 0; i < pointList.size(); i++) {
  183. if (x < pointList.get(i).getX() && !added) {
  184. if (e.getY() <= 0) {
  185. pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
  186. } else {
  187. pointList.add(i, new Point((int) (x), (int) y));
  188. }
  189. added = true;
  190. pointDrag = true;
  191. tempP = pointList.get(i);
  192. }
  193. }
  194. }
  195. if (deletePoint && tempP.getX() != 0
  196. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  197. pointList.remove(tempP);
  198. }
  199. repaint();
  200. }
  201. }
  202. @Override
  203. public void mouseReleased(MouseEvent e) {
  204. if (pointDrag) {
  205. pointDrag = false;
  206. tempP = null;
  207. }
  208. }
  209. public void componentResized(ComponentEvent e) {
  210. if (init) {
  211. MAXIMUM = tempElement.getEnergy();
  212. init = false;
  213. // for scale
  214. if (width == -1 && height == -1) {
  215. width = this.getWidth();
  216. height = this.getHeight();
  217. }
  218. scaleX = this.getWidth() / width;
  219. scaleY = this.getHeight() / height;
  220. if (pointList.isEmpty()) {
  221. pointList.addFirst(new Point(0, 0));
  222. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  223. }
  224. }
  225. scaleX = this.getWidth() / width;
  226. scaleY = this.getHeight() / height;
  227. repaint();
  228. }
  229. @Override
  230. public void componentHidden(ComponentEvent e) {
  231. }
  232. @Override
  233. public void componentMoved(ComponentEvent e) {
  234. }
  235. @Override
  236. public void componentShown(ComponentEvent e) {
  237. }
  238. /*
  239. * Emptys the Graph
  240. */
  241. public void empty() {
  242. pointList = null;
  243. tempElement = null;
  244. arrayOfValue = null;
  245. repaint();
  246. }
  247. /*
  248. * Resets the Points for the Element
  249. */
  250. public void reset() {
  251. pointList.removeAll(pointList);
  252. pointList.addFirst(new Point(0, 0));
  253. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  254. repaint();
  255. }
  256. /**
  257. * converts the number to fit the canvas
  258. *
  259. * @param double
  260. * d, the number to convert
  261. * @return the converted number
  262. */
  263. public double convertToCanvasY(float d) {
  264. return (height - (d * (height / MAXIMUM)));
  265. }
  266. /**
  267. * converts the number to fit the value
  268. *
  269. * @param double
  270. * d, the number to convert
  271. * @return the converted number
  272. */
  273. public float convertToValueY(double d) {
  274. return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
  275. }
  276. /**
  277. * Visualize the HolonElement on the Graph
  278. *
  279. * @param HolonElement
  280. * ele, which should be visualized
  281. */
  282. public void repaintWithNewElement(HolonElement ele) {
  283. arrayOfValue = ele.getEnergyAt();
  284. tempElement = ele;
  285. pointList = ele.getGraphPoints();
  286. init = true;
  287. componentResized(null);
  288. repaint();
  289. }
  290. /**
  291. * Build a Curve for the Graph
  292. *
  293. * @param Point,Point
  294. * ,startpoint p1 and endpoint p2
  295. *
  296. * @return CubicCurve2D, c, the CubicCurve2D for the Graph
  297. */
  298. public CubicCurve2D buildCurve(Point p1, Point p2) {
  299. x1 = (int) p1.getX();
  300. y1 = (int) p1.getY();
  301. x2 = (int) p2.getX();
  302. y2 = (int) p2.getY();
  303. ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
  304. ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
  305. if (y1 < y2) {
  306. ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
  307. ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
  308. } else {
  309. ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
  310. ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
  311. }
  312. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  313. x2 * scaleX, y2 * scaleY);
  314. return c;
  315. }
  316. public void fillArrayofValue() {
  317. for (int i = 0; i < arrayOfValue.length; i++) {
  318. arrayOfValue[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
  319. }
  320. }
  321. /**
  322. *
  323. * @param xVal,
  324. * the x value for the y value
  325. * @return y, the value at x
  326. */
  327. public float getYValueAt(int xVal) {
  328. for (int i = 0; i < pointList.size() - 1; i++) {
  329. // get the Points
  330. if (xVal <= pointList.get(i + 1).getX()) {
  331. // Curve erstellen
  332. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  333. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  334. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  335. return (float) getIntersectionPoint(l1, l2).getY();
  336. }
  337. }
  338. return 0;
  339. }
  340. /**
  341. *
  342. * @param xVal,
  343. * the x value for the y value
  344. * @return y, the value at x
  345. */
  346. public float getYValueAt_2(int xVal) {
  347. for (int i = 0; i < pointList.size() - 1; i++) {
  348. // get the Points
  349. if (xVal >= pointList.get(i).getX()) {
  350. // Curve erstellen
  351. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  352. c.subdivide(cl, cr);
  353. // Teil der Kurve aussuchen
  354. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  355. c = cl;
  356. // Kurve Links von "unten"
  357. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  358. for (int j = (int) (height - 1); j >= 0; j--) {
  359. if (c.contains(xVal * scaleX, j * scaleY)) {
  360. return (float) (j);
  361. }
  362. }
  363. } else {// Kurve Links von "oben"
  364. for (int j = 0; j < height; j++) {
  365. if (c.contains(xVal * scaleX, j * scaleY)) {
  366. return (float) (j);
  367. }
  368. }
  369. }
  370. } else {
  371. c = cr;
  372. // Kurve Links von "unten"
  373. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  374. for (int j = 0; j < height; j++) {
  375. if (c.contains(xVal * scaleX, j * scaleY)) {
  376. return (float) (j);
  377. }
  378. }
  379. } else {// Kurve Links von "oben"
  380. for (int j = (int) (height - 1); j >= 0; j--) {
  381. if (c.contains(xVal * scaleX, j * scaleY)) {
  382. return (float) (j);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. return getYValueAt(xVal);
  390. }
  391. public Point getIntersectionPoint(Line2D l1, Line2D l2) {
  392. if (!l1.intersectsLine(l2)) {
  393. return null;
  394. }
  395. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  396. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  397. double det = sx * ry - sy * rx;
  398. if (det == 0) {
  399. return null;
  400. } else {
  401. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  402. if (z < 0 || z > 1) {
  403. return new Point(0, 0); // intersection at end point!
  404. }
  405. return new Point((int) (px + z * rx), (int) (py + z * ry));
  406. }
  407. } // end intersection line-line
  408. }