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