MyCanvas.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. private int cx;
  34. private int cy;
  35. boolean dragging = false;
  36. boolean dropDelete = false;
  37. private CpsObject tempCps = null;
  38. private Rectangle selectRect = new Rectangle();
  39. // PopUpMenu
  40. private JPopupMenu popmenu = new JPopupMenu();
  41. private JMenuItem itemDelete = new JMenuItem("Delete Object");
  42. private JToolTip objectTT = new JToolTip();
  43. public MyCanvas(final Model model, Control control) {
  44. this.add(objectTT);
  45. this.controller = control;
  46. this.model = model;
  47. popmenu.add(itemDelete);
  48. itemDelete.setEnabled(false);
  49. itemDelete.addActionListener(new ActionListener() {
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. model.getObjectsOnCanvas().remove(tempCps);
  53. tempCps = null;
  54. selectRect.setRect(0, 0, 0, 0);
  55. repaint();
  56. }
  57. });
  58. this.addMouseListener(this);
  59. this.addMouseMotionListener(this);
  60. }
  61. /**
  62. * Paints all Components on the Canvas
  63. *
  64. * @param Graphics
  65. *
  66. */
  67. public void paintComponent(Graphics g) {
  68. super.paintComponent(g);
  69. //Selection
  70. if(selectRect != null){
  71. g.setColor(new Color(100, 255, 100));
  72. g.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight());
  73. }
  74. //Edges
  75. //g.drawLine(x1, y1, x2, y2);
  76. //Objects
  77. for (CpsObject cps : model.getObjectsOnCanvas()) {
  78. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  79. g.drawImage(img, cps.getPos().x, cps.getPos().y, GlobalVariables.SCALE, GlobalVariables.SCALE, null);
  80. }
  81. }
  82. @Override
  83. public void mouseClicked(MouseEvent e) {
  84. // TODO Auto-generated method stub
  85. }
  86. @Override
  87. public void mouseEntered(MouseEvent e) {
  88. // TODO Auto-generated method stub
  89. }
  90. @Override
  91. public void mouseExited(MouseEvent e) {
  92. // TODO Auto-generated method stub
  93. }
  94. @Override
  95. public void mousePressed(MouseEvent e) {
  96. // TODO Auto-generated method stub
  97. x = e.getX();
  98. y = e.getY();
  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. boolean on = false;
  151. for (CpsObject cps : model.getObjectsOnCanvas()) {
  152. cx = cps.getPos().x;
  153. cy = cps.getPos().y;
  154. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  155. objectTT.setTipText(cps.getName());
  156. objectTT.setLocation(cx, cy);
  157. on = true;
  158. }
  159. }
  160. if(!on){
  161. objectTT.setLocation(-200, -200);
  162. objectTT.setTipText("");
  163. }
  164. }
  165. /**
  166. * Sets the Highlighting of the Selected Object
  167. */
  168. private void objectSelectionHighlighting() {
  169. if(tempCps != null){
  170. selectRect.setBounds(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20), GlobalVariables.SCALE+GlobalVariables.SCALE/10, GlobalVariables.SCALE+GlobalVariables.SCALE/10);
  171. controller.setSelectedObjectID(tempCps.getID());
  172. System.out.println("Select");
  173. }else {
  174. controller.setSelectedObjectID(0);
  175. selectRect.setRect(0, 0, 0, 0);
  176. System.out.println("Unselect");
  177. }
  178. }
  179. }