MyCanvas.java 7.7 KB

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