UnitGraph.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.util.LinkedList;
  14. import java.awt.Point;
  15. import javax.swing.JButton;
  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 int MAXIMUM = 1;
  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 double width = -1;
  32. private double height = -1;
  33. private HolonElement tempElement;
  34. private Model model;
  35. private Control controller;
  36. private boolean pointDrag = false;
  37. private boolean init = false;
  38. private Point tempP = null;
  39. private double x = 0, y = 0;
  40. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  41. public UnitGraph(final Model model, Control control) {
  42. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  43. this.controller = control;
  44. this.model = model;
  45. this.ITERATIONS = model.getIterations();
  46. this.pointList = new LinkedList<>();
  47. this.addMouseListener(this);
  48. this.addMouseMotionListener(this);
  49. this.addComponentListener(this);
  50. }
  51. /**
  52. * Paints all Components on the Canvas
  53. *
  54. * @param Graphics
  55. *
  56. */
  57. public void paintComponent(Graphics g) {
  58. super.paintComponent(g);
  59. g2 = (Graphics2D) g;
  60. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  61. g2.setRenderingHints(rh);
  62. g2.setStroke(new BasicStroke(1));
  63. // Draw the Vertical Lines
  64. g2.setColor(new Color(240, 240, 240));
  65. for (int i = 0; i < ITERATIONS; i++) {
  66. g2.drawLine((i) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (i) * this.getWidth() / (ITERATIONS - 1),
  67. this.getHeight());
  68. }
  69. g2.drawLine(0, MAXIMUM, this.getWidth(), MAXIMUM);
  70. // Draw the Lines
  71. g2.setColor(Color.BLACK);
  72. for (int i = 0; i < pointList.size() - 1; i++) {
  73. /*
  74. * g2.drawLine((int) (pointList.get(i).getX() * scaleX), (int)
  75. * (pointList.get(i).getY() * scaleY), (int) (pointList.get(i +
  76. * 1).getX() * scaleX), (int) (pointList.get(i + 1).getY() *
  77. * scaleY));
  78. */
  79. x1 = (int) pointList.get(i).getX();
  80. y1 = (int) pointList.get(i).getY();
  81. x2 = (int) pointList.get(i + 1).getX();
  82. y2 = (int) pointList.get(i + 1).getY();
  83. ctrlx1 = (int) pointList.get(i).getX()
  84. + ((int) pointList.get(i + 1).getX() - (int) pointList.get(i).getX()) / 2;
  85. ctrlx2 = (int) pointList.get(i + 1).getX()
  86. - ((int) pointList.get(i + 1).getX() - (int) pointList.get(i).getX()) / 2;
  87. if (y1 < y2) {
  88. ctrly1 = (int) pointList.get(i).getY()
  89. + ((int) pointList.get(i + 1).getY() - (int) pointList.get(i).getY()) / 10;
  90. ctrly2 = (int) pointList.get(i + 1).getY()
  91. - ((int) pointList.get(i + 1).getY() - (int) pointList.get(i).getY()) / 10;
  92. } else {
  93. ctrly1 = (int) pointList.get(i).getY()
  94. - ((int) pointList.get(i).getY() - (int) pointList.get(i + 1).getY()) / 10;
  95. ctrly2 = (int) pointList.get(i + 1).getY()
  96. + ((int) pointList.get(i).getY() - (int) pointList.get(i + 1).getY()) / 10;
  97. }
  98. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  99. x2 * scaleX, y2 * scaleY);
  100. g2.draw(c);
  101. }
  102. // Draw the Points
  103. g2.setColor(Color.BLUE);
  104. for (int i = 0; i < pointList.size() - 0; i++) {
  105. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  106. (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  107. (int) recSize.getY());
  108. }
  109. g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1),
  110. this.getHeight());
  111. }
  112. @Override
  113. public void mouseDragged(MouseEvent e) {
  114. if (pointDrag && tempP != null) {
  115. // Out of Bounds verhindern
  116. int i = pointList.indexOf(tempP);
  117. x = e.getX() / scaleX;
  118. y = e.getY() / scaleY;
  119. // y
  120. if (e.getY() <= MAXIMUM) {
  121. y = MAXIMUM / scaleY;
  122. } else if (this.getHeight() <= e.getY()) {
  123. y = this.getHeight() / scaleY;
  124. }
  125. // x
  126. if (tempP.getX() == 0 || tempP.getX() == this.getWidth() / scaleX || pointList.get(i + 1).getX() <= x
  127. || pointList.get(i - 1).getX() >= x) {
  128. x = tempP.getX();
  129. }
  130. tempP.setLocation(x, y);
  131. }
  132. repaint();
  133. }
  134. @Override
  135. public void mouseMoved(MouseEvent e) {
  136. // TODO Auto-generated method stub
  137. }
  138. @Override
  139. public void mouseClicked(MouseEvent e) {
  140. // TODO Auto-generated method stub
  141. }
  142. @Override
  143. public void mouseEntered(MouseEvent e) {
  144. // TODO Auto-generated method stub
  145. }
  146. @Override
  147. public void mouseExited(MouseEvent e) {
  148. // TODO Auto-generated method stub
  149. }
  150. @Override
  151. public void mousePressed(MouseEvent e) {
  152. boolean added = false;
  153. boolean deletePoint = false;
  154. double x = e.getX() / scaleX;
  155. double y = e.getY() / scaleY;
  156. // Click on Point
  157. tempP = null;
  158. for (Point p : pointList) {
  159. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  160. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  161. if (e.getButton() == MouseEvent.BUTTON3) {
  162. tempP = p;
  163. deletePoint = true;
  164. } else {
  165. pointDrag = true;
  166. tempP = p;
  167. }
  168. }
  169. }
  170. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  171. && e.getX() != this.getWidth() / scaleX) {
  172. for (int i = 0; i < pointList.size(); i++) {
  173. if (x < pointList.get(i).getX() && !added) {
  174. if (e.getY() <= MAXIMUM) {
  175. pointList.add(i, new Point((int) (x), (int) (MAXIMUM / scaleY)));
  176. } else {
  177. pointList.add(i, new Point((int) (x), (int) y));
  178. }
  179. added = true;
  180. pointDrag = true;
  181. tempP = pointList.get(i);
  182. }
  183. }
  184. }
  185. if (deletePoint && tempP.getX() != 0
  186. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  187. pointList.remove(tempP);
  188. }
  189. repaint();
  190. }
  191. @Override
  192. public void mouseReleased(MouseEvent e) {
  193. if (pointDrag) {
  194. pointDrag = false;
  195. tempP = null;
  196. }
  197. }
  198. public void componentResized(ComponentEvent e) {
  199. if (init) {
  200. MAXIMUM = (int) convertToCanvasY(MAXIMUM);
  201. init = false;
  202. // for scale
  203. if (width == -1 && height == -1) {
  204. width = this.getWidth();
  205. height = this.getHeight();
  206. }
  207. if (pointList.isEmpty()) {
  208. pointList.addFirst(new Point(0, MAXIMUM));
  209. pointList.addLast(new Point(this.getWidth(), MAXIMUM));
  210. }
  211. }
  212. scaleX = this.getWidth() / width;
  213. scaleY = this.getHeight() / height;
  214. repaint();
  215. }
  216. @Override
  217. public void componentHidden(ComponentEvent e) {
  218. }
  219. @Override
  220. public void componentMoved(ComponentEvent e) {
  221. }
  222. @Override
  223. public void componentShown(ComponentEvent e) {
  224. }
  225. /*
  226. * Resets the Graph
  227. */
  228. public void reset() {
  229. pointList.removeAll(pointList);
  230. pointList.addFirst(new Point(0, (int) (MAXIMUM / scaleY)));
  231. pointList.addLast(new Point((int) (this.getWidth() / scaleX), (int) (MAXIMUM / scaleY)));
  232. repaint();
  233. }
  234. /**
  235. * converts the number to fit the canvas
  236. *
  237. * @param double
  238. * d, the number to convert
  239. * @return the convertet number
  240. */
  241. public double convertToCanvasY(int d) {
  242. if ((this.getHeight() - (((((double) this.getHeight() * 3) / 4) / MAXIMUM) * d)) <= 0) {
  243. return 1;
  244. } else {
  245. return (this.getHeight() - (((((double) this.getHeight())) / MAXIMUM) * d));
  246. }
  247. }
  248. /**
  249. * Viusalise the HolonElement on the Graph
  250. *
  251. * @param HolonElement
  252. * ele, which should be visualized
  253. */
  254. public void repaintWithNewElement(HolonElement ele) {
  255. float[] arrayOfValue = ele.getEnergyAt();
  256. tempElement = ele;
  257. pointList = ele.getGraphPoints();
  258. init = true;
  259. componentResized(null);
  260. repaint();
  261. }
  262. }