MyCanvas.java 5.5 KB

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