MyCanvas.java 6.9 KB

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