MyCanvas.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. g2.setStroke(new BasicStroke(GlobalVariables.SCALE/15));
  85. RenderingHints rh = new RenderingHints(
  86. RenderingHints.KEY_ANTIALIASING,
  87. RenderingHints.VALUE_ANTIALIAS_ON);
  88. g2.setRenderingHints(rh);
  89. //Selection
  90. if(selectRect != null){
  91. g2.setColor(Color.GREEN);
  92. g2.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight());
  93. }
  94. //drawEdges
  95. g2.setColor(Color.BLACK);
  96. if(drawEdge)g2.drawLine(tempCps.getPos().x+GlobalVariables.SCALE_DIVIDED2, tempCps.getPos().y+GlobalVariables.SCALE_DIVIDED2, x, y);
  97. for (CpsObject cps : model.getObjectsOnCanvas()) {
  98. for (CpsObject con : cps.getConnectedTo()) {
  99. if(con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
  100. 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);
  101. }
  102. }
  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. if(y+GlobalVariables.SCALE>this.getHeight()){
  161. this.setPreferredSize(new Dimension(this.getWidth(), y+GlobalVariables.SCALE));
  162. }
  163. if(x+GlobalVariables.SCALE>this.getHeight()){
  164. this.setPreferredSize(new Dimension(x+GlobalVariables.SCALE,this.getHeight() ));
  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. /**
  229. * Draws or Deletes an Edge
  230. */
  231. private void drawDeleteEdge() {
  232. for (CpsObject cps : model.getObjectsOnCanvas()) {
  233. cx = cps.getPos().x;
  234. cy = cps.getPos().y;
  235. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  236. if (!cps.getConnectedTo().contains(tempCps)) {
  237. cps.AddConnection(tempCps);
  238. tempCps.AddConnection(cps);
  239. } else {
  240. cps.getConnectedTo().remove(tempCps);
  241. tempCps.getConnectedTo().remove(cps);
  242. }
  243. }
  244. }
  245. }
  246. }