UnitGraph.java 11 KB

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