UnitGraph.java 13 KB

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