MyCanvas.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Image;
  9. import java.awt.Paint;
  10. import java.awt.Rectangle;
  11. import java.awt.RenderingHints;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.event.MouseListener;
  16. import java.awt.event.MouseMotionListener;
  17. import java.awt.geom.Line2D;
  18. import java.util.ArrayList;
  19. import java.util.LinkedList;
  20. import javax.swing.AbstractButton;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JComponent;
  23. import javax.swing.JLabel;
  24. import javax.swing.JMenuItem;
  25. import javax.swing.JPanel;
  26. import javax.swing.JPopupMenu;
  27. import javax.swing.JToolTip;
  28. import classes.CpsObject;
  29. import classes.GlobalVariables;
  30. import classes.HolonElement;
  31. import classes.HolonObject;
  32. import ui.controller.Control;
  33. import ui.model.*;
  34. class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  35. private Image img = null; // Contains the image to draw on MyCanvas
  36. private int x = 0;
  37. private int y = 0;
  38. //edge Object Start Point
  39. private Model model;
  40. private final Control controller;
  41. Graphics2D g2; //For Painting
  42. private int cx;
  43. private int cy;
  44. boolean dragging = false;
  45. boolean drawEdge = false;
  46. boolean dropDelete = false;
  47. private CpsObject tempCps = null;
  48. private Rectangle selectRect = new Rectangle();
  49. // PopUpMenu
  50. private JPopupMenu popmenu = new JPopupMenu();
  51. private JMenuItem itemDelete = new JMenuItem("Delete Object");
  52. private JToolTip objectTT = new JToolTip();
  53. public MyCanvas(final Model model, Control control) {
  54. this.add(objectTT);
  55. this.controller = control;
  56. this.model = model;
  57. popmenu.add(itemDelete);
  58. itemDelete.setEnabled(false);
  59. itemDelete.addActionListener(new ActionListener() {
  60. @Override
  61. public void actionPerformed(ActionEvent e) {
  62. model.getObjectsOnCanvas().remove(tempCps);
  63. for (CpsObject cps : model.getObjectsOnCanvas()) {
  64. cps.getConnectedTo().remove(tempCps);
  65. }
  66. tempCps = null;
  67. selectRect.setRect(0, 0, 0, 0);
  68. repaint();
  69. }
  70. });
  71. this.addMouseListener(this);
  72. this.addMouseMotionListener(this);
  73. }
  74. /**
  75. * Paints all Components on the Canvas
  76. *
  77. * @param Graphics
  78. *
  79. */
  80. public void paintComponent(Graphics g) {
  81. super.paintComponent(g);
  82. g2 = (Graphics2D) g;
  83. g2.setStroke(new BasicStroke(GlobalVariables.SCALE/15));
  84. RenderingHints rh = new RenderingHints(
  85. RenderingHints.KEY_ANTIALIASING,
  86. RenderingHints.VALUE_ANTIALIAS_ON);
  87. g2.setRenderingHints(rh);
  88. //Selection
  89. if(selectRect != null){
  90. g2.setColor(Color.GREEN);
  91. g2.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight());
  92. }
  93. //drawEdges
  94. g2.setColor(Color.BLACK);
  95. if(drawEdge)g2.drawLine(tempCps.getPos().x+GlobalVariables.SCALE_DIVIDED2, tempCps.getPos().y+GlobalVariables.SCALE_DIVIDED2, x, y);
  96. for (CpsObject cps : model.getObjectsOnCanvas()) {
  97. for (CpsObject con : cps.getConnectedTo()) {
  98. if(con.getID() != model.getSelectedObjectID())
  99. g2.drawLine(cps.getPos().x+GlobalVariables.SCALE_DIVIDED2, cps.getPos().y+GlobalVariables.SCALE_DIVIDED2, con.getPos().x+GlobalVariables.SCALE_DIVIDED2, con.getPos().y+GlobalVariables.SCALE_DIVIDED2);
  100. }
  101. }
  102. g2.setColor(Color.GREEN);
  103. for (CpsObject cps : model.getObjectsOnCanvas()) {
  104. for (CpsObject con : cps.getConnectedTo()) {
  105. if(con.getID() == model.getSelectedObjectID())
  106. g2.drawLine(cps.getPos().x+GlobalVariables.SCALE_DIVIDED2, cps.getPos().y+GlobalVariables.SCALE_DIVIDED2, con.getPos().x+GlobalVariables.SCALE_DIVIDED2, con.getPos().y+GlobalVariables.SCALE_DIVIDED2);
  107. }
  108. }
  109. //Objects
  110. for (CpsObject cps : model.getObjectsOnCanvas()) {
  111. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  112. g2.drawImage(img, cps.getPos().x, cps.getPos().y, GlobalVariables.SCALE, GlobalVariables.SCALE, null);
  113. }
  114. }
  115. @Override
  116. public void mouseClicked(MouseEvent e) {
  117. // TODO Auto-generated method stub
  118. }
  119. @Override
  120. public void mouseEntered(MouseEvent e) {
  121. // TODO Auto-generated method stub
  122. }
  123. @Override
  124. public void mouseExited(MouseEvent e) {
  125. // TODO Auto-generated method stub
  126. }
  127. @Override
  128. public void mousePressed(MouseEvent e) {
  129. // TODO Auto-generated method stub
  130. x = e.getX();
  131. y = e.getY();
  132. tempCps = null;
  133. //Object Selection
  134. for (CpsObject cps : model.getObjectsOnCanvas()) {
  135. cx = cps.getPos().x;
  136. cy = cps.getPos().y;
  137. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  138. tempCps = cps;
  139. if(e.isControlDown())drawEdge = true;
  140. }
  141. }
  142. //Object Selection Highlighting (selectRect)
  143. objectSelectionHighlighting();
  144. repaint();
  145. }
  146. @Override
  147. public void mouseReleased(MouseEvent e) {
  148. if(drawEdge){
  149. drawEdge = false;
  150. for (CpsObject cps : model.getObjectsOnCanvas()) {
  151. cx = cps.getPos().x;
  152. cy = cps.getPos().y;
  153. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  154. cps.AddConnection(tempCps);
  155. tempCps.AddConnection(cps);
  156. }
  157. }
  158. }
  159. if (dragging) {
  160. x = e.getX();
  161. y = e.getY();
  162. dragging = false;
  163. tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
  164. tempCps = null;
  165. }
  166. // Rechtsklick Liste
  167. if (e.getButton() == e.BUTTON3) {
  168. if (e.getButton() == e.BUTTON3 && tempCps != null) {
  169. itemDelete.setEnabled(true);
  170. }else {
  171. itemDelete.setEnabled(false);
  172. }
  173. popmenu.show(e.getComponent(), e.getX(), e.getY());
  174. }
  175. repaint();
  176. }
  177. @Override
  178. public void mouseDragged(MouseEvent e) {
  179. // TODO Auto-generated method stub
  180. if(drawEdge){
  181. x = e.getX();
  182. y = e.getY();
  183. repaint();
  184. }else{
  185. try {
  186. tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
  187. dragging = true;
  188. selectRect.setLocation(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20));
  189. objectTT.setTipText(tempCps.getName());
  190. objectTT.setLocation(tempCps.getPos().x, tempCps.getPos().y);
  191. repaint();
  192. } catch (Exception e2) {
  193. // TODO: handle exception
  194. }
  195. }
  196. }
  197. @Override
  198. public void mouseMoved(MouseEvent e) {
  199. x = e.getX();
  200. y = e.getY();
  201. boolean on = false;
  202. for (CpsObject cps : model.getObjectsOnCanvas()) {
  203. cx = cps.getPos().x;
  204. cy = cps.getPos().y;
  205. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  206. objectTT.setTipText(cps.getName());
  207. objectTT.setLocation(cx, cy);
  208. on = true;
  209. }
  210. }
  211. if(!on){
  212. objectTT.setLocation(-200, -200);
  213. objectTT.setTipText("");
  214. }
  215. }
  216. /**
  217. * Sets the Highlighting of the Selected Object
  218. */
  219. private void objectSelectionHighlighting() {
  220. if(tempCps != null){
  221. selectRect.setBounds(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20), GlobalVariables.SCALE+GlobalVariables.SCALE/10, GlobalVariables.SCALE+GlobalVariables.SCALE/10);
  222. controller.setSelectedObjectID(tempCps.getID());
  223. }else {
  224. controller.setSelectedObjectID(0);
  225. selectRect.setRect(0, 0, 0, 0);
  226. }
  227. }
  228. }