UnitGraph.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Point;
  14. import javax.swing.JPanel;
  15. import ui.controller.Control;
  16. import ui.model.Model;
  17. import java.awt.BorderLayout;
  18. import javax.swing.JButton;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.FlowLayout;
  22. import javax.swing.SwingConstants;
  23. import classes.HolonElement;
  24. import javax.swing.JLabel;
  25. import java.awt.Cursor;
  26. class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  27. private static final long serialVersionUID = 1L;
  28. private Control controller;
  29. private Model model;
  30. private Graphics2D g2;
  31. private CubicCurve2D c = new CubicCurve2D.Double();
  32. private Point[] pointList;
  33. private HolonElement temp = null;
  34. private boolean pointDrag = false;
  35. private int tempP;
  36. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  37. private final JButton resetButton = new JButton("Reset Graph");
  38. private final JLabel lblName = new JLabel("Name");
  39. public UnitGraph(final Model model, Control control) {
  40. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  41. this.controller = control;
  42. this.model = model;
  43. this.pointList = new Point[model.getIterations()];
  44. for (int i = 0; i < pointList.length; i++) {
  45. pointList[i] = new Point(0, 0);
  46. }
  47. this.addMouseListener(this);
  48. this.addMouseMotionListener(this);
  49. this.addComponentListener(this);
  50. lblName.setText("None");
  51. resetButton.setToolTipText("Resets the Graph");
  52. resetButton.setBackground(Color.WHITE);
  53. resetButton.addActionListener(new ActionListener() {
  54. public void actionPerformed(ActionEvent e) {
  55. reset();
  56. }
  57. });
  58. setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  59. add(lblName);
  60. add(resetButton);
  61. }
  62. /*
  63. * Resets the Graph
  64. */
  65. public void reset() {
  66. for (int i = 0; i < pointList.length; i++) {
  67. pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
  68. }
  69. repaint();
  70. }
  71. /**
  72. * Paints all Components on the Canvas
  73. *
  74. * @param Graphics
  75. *
  76. */
  77. public void paintComponent(Graphics g) {
  78. super.paintComponent(g);
  79. g2 = (Graphics2D) g;
  80. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  81. g2.setRenderingHints(rh);
  82. g2.setStroke(new BasicStroke(1));
  83. g2.setColor(new Color(240, 240, 240));
  84. for (int i = 0; i < pointList.length; i++) {
  85. g2.drawLine((int) pointList[i].getX(), 0, (int) pointList[i].getX(), this.getHeight());
  86. }
  87. g2.setColor(Color.BLACK);
  88. g2.setStroke(new BasicStroke(2));
  89. for (int i = 0; i < pointList.length - 1; i++) {
  90. x1 = (int) pointList[i].getX();
  91. y1 = (int) pointList[i].getY();
  92. x2 = (int) pointList[i + 1].getX();
  93. y2 = (int) pointList[i + 1].getY();
  94. ctrlx1 = (int) pointList[i].getX() + ((int) pointList[i + 1].getX() - (int) pointList[i].getX()) / 2;
  95. ctrlx2 = (int) pointList[i + 1].getX() - ((int) pointList[i + 1].getX() - (int) pointList[i].getX()) / 2;
  96. if (y1 < y2) {
  97. ctrly1 = (int) pointList[i].getY() + ((int) pointList[i + 1].getY() - (int) pointList[i].getY()) / 10;
  98. ctrly2 = (int) pointList[i + 1].getY()
  99. - ((int) pointList[i + 1].getY() - (int) pointList[i].getY()) / 10;
  100. } else {
  101. ctrly1 = (int) pointList[i].getY() - ((int) pointList[i].getY() - (int) pointList[i + 1].getY()) / 10;
  102. ctrly2 = (int) pointList[i + 1].getY()
  103. + ((int) pointList[i].getY() - (int) pointList[i + 1].getY()) / 10;
  104. }
  105. c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
  106. // draw the curve
  107. g2.draw(c);
  108. }
  109. }
  110. @Override
  111. public void mouseDragged(MouseEvent e) {
  112. if (temp != null) {
  113. if (pointDrag && e.getY() >= this.getHeight() / 3 && tempP != -1) {
  114. pointList[tempP].setLocation(pointList[tempP].getX(), e.getY());
  115. if (tempP != 0 && pointList[tempP - 1].getY() < pointList[tempP].getY()
  116. - (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
  117. pointList[tempP - 1].setLocation(pointList[tempP - 1].getX(),
  118. pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
  119. }
  120. if (tempP != model.getIterations() - 1 && pointList[tempP + 1].getY() < pointList[tempP].getY()
  121. - (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
  122. pointList[tempP + 1].setLocation(pointList[tempP + 1].getX(),
  123. pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
  124. }
  125. } else if (tempP != -1) {
  126. pointList[tempP].setLocation(pointList[tempP].getX(), this.getHeight() / 3);
  127. }
  128. repaint();
  129. }
  130. }
  131. @Override
  132. public void mouseMoved(MouseEvent e) {
  133. // TODO Auto-generated method stub
  134. }
  135. @Override
  136. public void mouseClicked(MouseEvent e) {
  137. // TODO Auto-generated method stub
  138. }
  139. @Override
  140. public void mouseEntered(MouseEvent e) {
  141. // TODO Auto-generated method stub
  142. }
  143. @Override
  144. public void mouseExited(MouseEvent e) {
  145. // TODO Auto-generated method stub
  146. }
  147. @Override
  148. public void mousePressed(MouseEvent e) {
  149. for (int i = 0; i < pointList.length; i++) {
  150. if (e.getX() - this.getWidth() / model.getIterations() / 2 <= pointList[i].getX()
  151. && e.getX() + this.getWidth() / model.getIterations() / 2 >= pointList[i].getX()
  152. && e.getY() - 10 < pointList[i].getY() && e.getY() + 10 > pointList[i].getY()) {
  153. tempP = i;
  154. pointDrag = true;
  155. }
  156. }
  157. }
  158. @Override
  159. public void mouseReleased(MouseEvent e) {
  160. if (pointDrag) {
  161. pointDrag = false;
  162. tempP = -1;
  163. }
  164. }
  165. public void repaintWithNewElement(HolonElement ele) {
  166. reset();
  167. float[] arrayOfValue = ele.getEnergyAt();
  168. lblName.setText(ele.getEleName());
  169. temp = ele;
  170. // TODO
  171. }
  172. // resize listener
  173. public void componentResized(ComponentEvent e) {
  174. if (pointList[0].getY() != 0) {
  175. reset();
  176. } else {
  177. for (int i = 0; i < pointList.length; i++) {
  178. pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
  179. }
  180. }
  181. repaint();
  182. }
  183. @Override
  184. public void componentHidden(ComponentEvent e) {
  185. }
  186. @Override
  187. public void componentMoved(ComponentEvent e) {
  188. }
  189. @Override
  190. public void componentShown(ComponentEvent e) {
  191. }
  192. }