MyCanvas.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. if(selectRect != null){
  68. g.setColor(new Color(100, 255, 100));
  69. g.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight());
  70. }
  71. for (CpsObject cps : model.getObjectsOnCanvas()) {
  72. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  73. g.drawImage(img, cps.getPos().x, cps.getPos().y, GlobalVariables.SCALE, GlobalVariables.SCALE, null);
  74. }
  75. }
  76. @Override
  77. public void mouseClicked(MouseEvent e) {
  78. // TODO Auto-generated method stub
  79. }
  80. @Override
  81. public void mouseEntered(MouseEvent e) {
  82. // TODO Auto-generated method stub
  83. }
  84. @Override
  85. public void mouseExited(MouseEvent e) {
  86. // TODO Auto-generated method stub
  87. }
  88. @Override
  89. public void mousePressed(MouseEvent e) {
  90. // TODO Auto-generated method stub
  91. x = e.getX();
  92. y = e.getY();
  93. int cx;
  94. int cy;
  95. tempCps = null;
  96. for (CpsObject cps : model.getObjectsOnCanvas()) {
  97. cx = cps.getPos().x;
  98. cy = cps.getPos().y;
  99. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  100. tempCps = cps;
  101. }
  102. }
  103. //Object Selection
  104. if(tempCps != null){
  105. selectRect.setBounds(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20), GlobalVariables.SCALE+GlobalVariables.SCALE/10, GlobalVariables.SCALE+GlobalVariables.SCALE/10);
  106. controller.setSelectedObjectID(tempCps.getID());
  107. System.out.println("Select");
  108. }else {
  109. controller.setSelectedObjectID(0);
  110. selectRect.setRect(0, 0, 0, 0);
  111. System.out.println("Unselect");
  112. }
  113. repaint();
  114. }
  115. @Override
  116. public void mouseReleased(MouseEvent e) {
  117. if (dragging) {
  118. x = e.getX();
  119. y = e.getY();
  120. dragging = false;
  121. tempCps.setPos(e.getX() - GlobalVariables.SCALE/2, e.getY() - GlobalVariables.SCALE/2);
  122. tempCps = null;
  123. repaint();
  124. }
  125. // Rechtsklick Liste
  126. if (e.getButton() == e.BUTTON3) {
  127. if (e.getButton() == e.BUTTON3 && tempCps != null) {
  128. itemDelete.setEnabled(true);
  129. }else {
  130. itemDelete.setEnabled(false);
  131. }
  132. popmenu.show(e.getComponent(), e.getX(), e.getY());
  133. }
  134. }
  135. @Override
  136. public void mouseDragged(MouseEvent e) {
  137. // TODO Auto-generated method stub
  138. try {
  139. tempCps.setPos(e.getX() - GlobalVariables.SCALE/2, e.getY() - GlobalVariables.SCALE/2);
  140. dragging = true;
  141. selectRect.setLocation(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20));
  142. objectTT.setTipText(tempCps.getName());
  143. objectTT.setLocation(tempCps.getPos().x, tempCps.getPos().y);
  144. repaint();
  145. } catch (Exception e2) {
  146. // TODO: handle exception
  147. }
  148. }
  149. @Override
  150. public void mouseMoved(MouseEvent e) {
  151. x = e.getX();
  152. y = e.getY();
  153. int cx;
  154. int cy;
  155. for (CpsObject cps : model.getObjectsOnCanvas()) {
  156. cx = cps.getPos().x;
  157. cy = cps.getPos().y;
  158. if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
  159. objectTT.setEnabled(true);
  160. objectTT.setTipText(cps.getName());
  161. objectTT.setLocation(cx, cy);
  162. }
  163. }
  164. }
  165. }