UnitGraph.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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;
  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.setBackground(Color.WHITE);
  51. this.addMouseListener(this);
  52. this.addMouseMotionListener(this);
  53. this.addComponentListener(this);
  54. }
  55. /**
  56. * Paints all Components on the Canvas
  57. *
  58. * @param Graphics
  59. *
  60. */
  61. public void paintComponent(Graphics g) {
  62. super.paintComponent(g);
  63. g2 = (Graphics2D) g;
  64. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  65. g2.setRenderingHints(rh);
  66. g2.setStroke(new BasicStroke(0));
  67. graphCurve.reset();
  68. // Draw the Vertical Lines
  69. g2.setColor(Color.BLACK);
  70. for (int i = 0; i <= this.getWidth(); i += 10) {
  71. g2.drawLine(i, 0, i, this.getHeight());
  72. }
  73. for (int i = 0; i <= this.getHeight(); i += 5) {
  74. g2.drawLine(0, i, this.getWidth(), i);
  75. }
  76. if (arrayOfValue != null) {
  77. // array fillen
  78. fillArrayofValue();
  79. // Draw the Lines
  80. g2.setStroke(new BasicStroke(2));
  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.setStroke(new BasicStroke(1));
  98. g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
  99. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
  100. // Iteration Value
  101. if (arrayOfValue != null) {
  102. g2.drawString("" + arrayOfValue[model.getCurIteration()],
  103. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  104. this.getHeight() - 10);
  105. }
  106. /*
  107. * // Actual Iteration Point Visualization g2.setColor(Color.RED); if
  108. * (arrayOfValue != null) { for (int i = 0; i < arrayOfValue.length;
  109. * i++) { g2.fillOval((int) (i * width / (model.getIterations() - 1) *
  110. * scaleX - recSize.getX() / 2), (int) (convertToCanvasY((int)
  111. * arrayOfValue[i]) * scaleY - recSize.getY() / 2), (int)
  112. * recSize.getX(), (int) recSize.getY()); } }
  113. */
  114. }
  115. @Override
  116. public void mouseDragged(MouseEvent e) {
  117. if (pointDrag && tempP != null) {
  118. // Out of Bounds verhindern
  119. int i = pointList.indexOf(tempP);
  120. x = e.getX() / scaleX;
  121. y = e.getY() / scaleY;
  122. // y
  123. if (e.getY() <= 0) {
  124. y = 0 / scaleY;
  125. } else if (this.getHeight() <= e.getY()) {
  126. y = this.getHeight() / scaleY;
  127. }
  128. // x
  129. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
  130. || pointList.get(i - 1).getX() >= x) {
  131. x = tempP.getX();
  132. }
  133. tempP.setLocation(x, y);
  134. repaint();
  135. }
  136. }
  137. @Override
  138. public void mouseMoved(MouseEvent e) {
  139. // TODO Auto-generated method stub
  140. }
  141. @Override
  142. public void mouseClicked(MouseEvent e) {
  143. // TODO Auto-generated method stub
  144. }
  145. @Override
  146. public void mouseEntered(MouseEvent e) {
  147. // TODO Auto-generated method stub
  148. }
  149. @Override
  150. public void mouseExited(MouseEvent e) {
  151. // TODO Auto-generated method stub
  152. }
  153. @Override
  154. public void mousePressed(MouseEvent e) {
  155. boolean added = false;
  156. boolean deletePoint = false;
  157. double x = e.getX() / scaleX;
  158. double y = e.getY() / scaleY;
  159. // Click on Point
  160. tempP = null;
  161. if (pointList != null) {
  162. for (Point p : pointList) {
  163. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  164. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  165. if (e.getButton() == MouseEvent.BUTTON3) {
  166. tempP = p;
  167. deletePoint = true;
  168. } else {
  169. pointDrag = true;
  170. tempP = p;
  171. }
  172. }
  173. }
  174. // New Point
  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. // Delete a Point
  191. if (deletePoint && tempP.getX() != 0
  192. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  193. pointList.remove(tempP);
  194. }
  195. repaint();
  196. }
  197. }
  198. @Override
  199. public void mouseReleased(MouseEvent e) {
  200. if (pointDrag) {
  201. pointDrag = false;
  202. tempP = null;
  203. }
  204. }
  205. public void componentResized(ComponentEvent e) {
  206. // Wenn ein anderes Element genommen wird
  207. if (init) {
  208. MAXIMUM = tempElement.getEnergy();
  209. init = false;
  210. // for scale on the first initialisation
  211. if (width == -1 && height == -1) {
  212. width = this.getWidth();
  213. height = this.getHeight();
  214. }
  215. scaleX = this.getWidth() / width;
  216. scaleY = this.getHeight() / height;
  217. // First time clicked on the Element
  218. if (pointList.isEmpty()) {
  219. pointList.addFirst(new Point(0, 0));
  220. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  221. }
  222. }
  223. // Scale
  224. scaleX = this.getWidth() / width;
  225. scaleY = this.getHeight() / height;
  226. repaint();
  227. }
  228. @Override
  229. public void componentHidden(ComponentEvent e) {
  230. }
  231. @Override
  232. public void componentMoved(ComponentEvent e) {
  233. }
  234. @Override
  235. public void componentShown(ComponentEvent e) {
  236. }
  237. /*
  238. * Emptys the Graph
  239. */
  240. public void empty() {
  241. pointList = null;
  242. tempElement = null;
  243. arrayOfValue = null;
  244. repaint();
  245. }
  246. /*
  247. * Resets the Points for the Element
  248. */
  249. public void reset() {
  250. pointList.removeAll(pointList);
  251. pointList.addFirst(new Point(0, 0));
  252. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  253. repaint();
  254. }
  255. /**
  256. * converts the number to fit the canvas
  257. *
  258. * @param double
  259. * d, the number to convert
  260. * @return the converted number
  261. */
  262. public double convertToCanvasY(float d) {
  263. return (height - (d * (height / MAXIMUM)));
  264. }
  265. /**
  266. * converts the number to fit the value
  267. *
  268. * @param double
  269. * d, the number to convert
  270. * @return the converted number
  271. */
  272. public float convertToValueY(double d) {
  273. return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
  274. }
  275. /**
  276. * Visualize the HolonElement on the Graph
  277. *
  278. * @param HolonElement
  279. * ele, which should be visualized
  280. */
  281. public void repaintWithNewElement(HolonElement ele) {
  282. arrayOfValue = ele.getEnergyAt();
  283. tempElement = ele;
  284. pointList = ele.getGraphPoints();
  285. init = true;
  286. componentResized(null);
  287. repaint();
  288. }
  289. /**
  290. * Build a Curve for the Graph
  291. *
  292. * @param Point,Point
  293. * ,startpoint p1 and endpoint p2
  294. *
  295. * @return CubicCurve2D, c, the CubicCurve2D for the Graph
  296. */
  297. public CubicCurve2D buildCurve(Point p1, Point p2) {
  298. x1 = (int) p1.getX();
  299. y1 = (int) p1.getY();
  300. x2 = (int) p2.getX();
  301. y2 = (int) p2.getY();
  302. // calculate the controllpoints
  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. // set the curve
  313. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  314. x2 * scaleX, y2 * scaleY);
  315. return c;
  316. }
  317. /**
  318. * Fills the Arrays of each HolonElement
  319. */
  320. public void fillArrayofValue() {
  321. for (int i = 0; i < arrayOfValue.length; i++) {
  322. arrayOfValue[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
  323. }
  324. }
  325. /**
  326. *
  327. * @param xVal,
  328. * the x value for the y value
  329. * @return y, the value at x
  330. */
  331. public float getYValueAt(int xVal) {
  332. for (int i = 0; i < pointList.size() - 1; i++) {
  333. // get the Points
  334. if (xVal <= pointList.get(i + 1).getX()) {
  335. // Curve erstellen
  336. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  337. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  338. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  339. return getIntersectionPoint(l1, l2);
  340. }
  341. }
  342. return 0;
  343. }
  344. /**
  345. *
  346. * @param xVal,
  347. * the x value for the y value
  348. * @return y, the value at x
  349. */
  350. public float getYValueAt_2(int xVal) {
  351. for (int i = 0; i < pointList.size() - 1; i++) {
  352. // get the Points
  353. if (xVal >= pointList.get(i).getX()) {
  354. // Curve erstellen
  355. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  356. c.subdivide(cl, cr);
  357. // Teil der Kurve aussuchen
  358. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  359. c = cl;
  360. // Kurve Links von "unten"
  361. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  362. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  363. if (c.contains(xVal * scaleX, j * scaleY)) {
  364. return (float) (j);
  365. }
  366. }
  367. } else {// Kurve Links von "oben"
  368. for (float j = 0; j < height; j += 0.1f) {
  369. if (c.contains(xVal * scaleX, j * scaleY)) {
  370. return (float) (j);
  371. }
  372. }
  373. }
  374. } else {
  375. c = cr;
  376. // Kurve Links von "unten"
  377. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  378. for (float j = 0; j < height; j += 0.1f) {
  379. if (c.contains(xVal * scaleX, j * scaleY)) {
  380. return (float) (j);
  381. }
  382. }
  383. } else {// Kurve Links von "oben"
  384. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  385. if (c.contains(xVal * scaleX, j * scaleY)) {
  386. return (float) (j);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. return getYValueAt(xVal);
  394. }
  395. /**
  396. *
  397. * @param l1,
  398. * the first Line
  399. * @param l2,
  400. * the second Line
  401. *
  402. * @return The Intersection Point
  403. */
  404. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  405. if (!l1.intersectsLine(l2)) {
  406. return 0;// null;
  407. }
  408. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  409. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  410. double det = sx * ry - sy * rx;
  411. if (det == 0) {
  412. return 0;// null;
  413. } else {
  414. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  415. if (z < 0 || z > 1) {
  416. return 0;// new Point(0, 0); // intersection at end point!
  417. }
  418. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  419. // (py + z * ry));
  420. }
  421. } // end intersection line-line
  422. }