UnitGraph.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.Line2D;
  14. import java.util.LinkedList;
  15. import java.awt.Point;
  16. import javax.swing.JPanel;
  17. import classes.HolonElement;
  18. import ui.controller.Control;
  19. import ui.model.Model;
  20. import java.awt.Cursor;
  21. class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  22. private static final long serialVersionUID = 1L;
  23. private int ITERATIONS = 50;
  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 LinkedList<Point> pointList;
  29. private double scaleX;
  30. private double scaleY;
  31. private float[] arrayOfValue = null;
  32. private double width = -1;
  33. private double height = -1;
  34. private HolonElement tempElement;
  35. private Model model;
  36. private Control controller;
  37. private boolean pointDrag = false;
  38. private boolean init = false;
  39. private Point tempP = null;
  40. private double x = 0, y = 0;
  41. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  42. public UnitGraph(final Model model, Control control) {
  43. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  44. this.controller = control;
  45. this.model = model;
  46. this.ITERATIONS = model.getIterations();
  47. this.pointList = new LinkedList<>();
  48. this.addMouseListener(this);
  49. this.addMouseMotionListener(this);
  50. this.addComponentListener(this);
  51. }
  52. /**
  53. * Paints all Components on the Canvas
  54. *
  55. * @param Graphics
  56. *
  57. */
  58. public void paintComponent(Graphics g) {
  59. super.paintComponent(g);
  60. g2 = (Graphics2D) g;
  61. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  62. g2.setRenderingHints(rh);
  63. g2.setStroke(new BasicStroke(1));
  64. if (arrayOfValue != null) {
  65. for (int i = 0; i < arrayOfValue.length; i++) {
  66. arrayOfValue[i] = convertToValueY(getYValueAt((int) (i * width / (ITERATIONS - 1))));
  67. }
  68. }
  69. /*
  70. // Draw the Vertical Lines
  71. g2.setColor(new Color(240, 240, 240));
  72. for (int i = 0; i < ITERATIONS; i++) {
  73. g2.drawLine((i) * this.getWidth() / (ITERATIONS - 1), 0, (i) * this.getWidth() / (ITERATIONS - 1),
  74. this.getHeight());
  75. }
  76. for (int i = 0; i < ITERATIONS; i++) {
  77. g2.drawLine(0, (i) * this.getHeight() / (ITERATIONS - 1), this.getWidth(),
  78. (i) * this.getHeight() / (ITERATIONS - 1));
  79. }
  80. */
  81. // Draw the Lines
  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. g2.draw(c);
  86. }
  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. g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), 0,
  95. (model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), this.getHeight());
  96. //Actual Iteration Point Visualization
  97. /*
  98. g2.setColor(Color.RED);
  99. if (arrayOfValue != null) {
  100. for (int i = 0; i < arrayOfValue.length; i++) {
  101. g2.fillOval((int) (i * width / (ITERATIONS - 1) * scaleX - recSize.getX() / 2),
  102. (int) (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  103. (int) recSize.getY());
  104. }
  105. }
  106. */
  107. }
  108. @Override
  109. public void mouseDragged(MouseEvent e) {
  110. if (pointDrag && tempP != null) {
  111. // Out of Bounds verhindern
  112. int i = pointList.indexOf(tempP);
  113. x = e.getX() / scaleX;
  114. y = e.getY() / scaleY;
  115. // y
  116. if (e.getY() <= 0) {
  117. y = 0 / scaleY;
  118. } else if (this.getHeight() <= e.getY()) {
  119. y = this.getHeight() / scaleY;
  120. }
  121. // x
  122. if (tempP.getX() == 0 || tempP.getX() == this.getWidth() / scaleX || pointList.get(i + 1).getX() <= x
  123. || pointList.get(i - 1).getX() >= x) {
  124. x = tempP.getX();
  125. }
  126. tempP.setLocation(x, y);
  127. }
  128. repaint();
  129. }
  130. @Override
  131. public void mouseMoved(MouseEvent e) {
  132. // TODO Auto-generated method stub
  133. }
  134. @Override
  135. public void mouseClicked(MouseEvent e) {
  136. // TODO Auto-generated method stub
  137. }
  138. @Override
  139. public void mouseEntered(MouseEvent e) {
  140. // TODO Auto-generated method stub
  141. }
  142. @Override
  143. public void mouseExited(MouseEvent e) {
  144. // TODO Auto-generated method stub
  145. }
  146. @Override
  147. public void mousePressed(MouseEvent e) {
  148. boolean added = false;
  149. boolean deletePoint = false;
  150. double x = e.getX() / scaleX;
  151. double y = e.getY() / scaleY;
  152. // Click on Point
  153. tempP = null;
  154. for (Point p : pointList) {
  155. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  156. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  157. if (e.getButton() == MouseEvent.BUTTON3) {
  158. tempP = p;
  159. deletePoint = true;
  160. } else {
  161. pointDrag = true;
  162. tempP = p;
  163. }
  164. }
  165. }
  166. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  167. && e.getX() != this.getWidth() / scaleX) {
  168. for (int i = 0; i < pointList.size(); i++) {
  169. if (x < pointList.get(i).getX() && !added) {
  170. if (e.getY() <= 0) {
  171. pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
  172. } else {
  173. pointList.add(i, new Point((int) (x), (int) y));
  174. }
  175. added = true;
  176. pointDrag = true;
  177. tempP = pointList.get(i);
  178. }
  179. }
  180. }
  181. if (deletePoint && tempP.getX() != 0
  182. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  183. pointList.remove(tempP);
  184. }
  185. repaint();
  186. }
  187. @Override
  188. public void mouseReleased(MouseEvent e) {
  189. if (pointDrag) {
  190. pointDrag = false;
  191. tempP = null;
  192. }
  193. }
  194. public void componentResized(ComponentEvent e) {
  195. if (init) {
  196. MAXIMUM = tempElement.getEnergy();
  197. init = false;
  198. // for scale
  199. if (width == -1 && height == -1) {
  200. width = this.getWidth();
  201. height = this.getHeight();
  202. }
  203. scaleX = this.getWidth() / width;
  204. scaleY = this.getHeight() / height;
  205. if (pointList.isEmpty()) {
  206. pointList.addFirst(new Point(0, 0));
  207. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  208. }
  209. }
  210. scaleX = this.getWidth() / width;
  211. scaleY = this.getHeight() / height;
  212. repaint();
  213. }
  214. @Override
  215. public void componentHidden(ComponentEvent e) {
  216. }
  217. @Override
  218. public void componentMoved(ComponentEvent e) {
  219. }
  220. @Override
  221. public void componentShown(ComponentEvent e) {
  222. }
  223. /*
  224. * Resets the Graph
  225. */
  226. public void reset() {
  227. pointList.removeAll(pointList);
  228. pointList.addFirst(new Point(0,0));
  229. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  230. repaint();
  231. }
  232. /**
  233. * converts the number to fit the canvas
  234. *
  235. * @param double
  236. * d, the number to convert
  237. * @return the converted number
  238. */
  239. public double convertToCanvasY(float d) {
  240. System.out.println(""+(height + (d*(height/MAXIMUM))));
  241. return (height -(d*(height/MAXIMUM)));
  242. }
  243. /**
  244. * converts the number to fit the value
  245. *
  246. * @param double
  247. * d, the number to convert
  248. * @return the converted number
  249. */
  250. public float convertToValueY(double d){
  251. return (float)((height - ( height * (d/height)))/(height/MAXIMUM));
  252. }
  253. /**
  254. * Visualize the HolonElement on the Graph
  255. *
  256. * @param HolonElement
  257. * ele, which should be visualized
  258. */
  259. public void repaintWithNewElement(HolonElement ele) {
  260. arrayOfValue = ele.getEnergyAt();
  261. tempElement = ele;
  262. pointList = ele.getGraphPoints();
  263. init = true;
  264. componentResized(null);
  265. repaint();
  266. }
  267. /**
  268. * Build a Curve for the Graph
  269. *
  270. * @param Point,Point
  271. * ,startpoint p1 and endpoint p2
  272. *
  273. * @return CubicCurve2D, c, the CubicCurve2D for the Graph
  274. */
  275. public CubicCurve2D buildCurve(Point p1, Point p2) {
  276. x1 = (int) p1.getX();
  277. y1 = (int) p1.getY();
  278. x2 = (int) p2.getX();
  279. y2 = (int) p2.getY();
  280. ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
  281. ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
  282. if (y1 < y2) {
  283. ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
  284. ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
  285. } else {
  286. ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
  287. ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
  288. }
  289. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  290. x2 * scaleX, y2 * scaleY);
  291. return c;
  292. }
  293. /**
  294. *
  295. * @param xVal,
  296. * the x value for the y value
  297. * @return y, the value at x
  298. */
  299. public int getYValueAt(int xVal) {
  300. for (int i = 0; i < pointList.size() - 1; i++) {
  301. // get the Points
  302. if (xVal < pointList.get(i + 1).getX()) {
  303. // Curve erstellen
  304. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  305. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  306. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  307. return (int) getIntersectionPoint(l1, l2).getY();
  308. }
  309. }
  310. return 0;
  311. }
  312. public Point getIntersectionPoint(Line2D l1, Line2D l2) {
  313. if (!l1.intersectsLine(l2))
  314. return null;
  315. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  316. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  317. double det = sx * ry - sy * rx;
  318. if (det == 0) {
  319. return null;
  320. } else {
  321. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  322. if (z < 0 || z > 1)
  323. return new Point(0,0); // intersection at end point!
  324. return new Point((int) (px + z * rx), (int) (py + z * ry));
  325. }
  326. } // end intersection line-line
  327. }