MyCanvas.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 java.util.Timer;
  22. import java.util.TimerTask;
  23. import javax.swing.AbstractButton;
  24. import javax.swing.ImageIcon;
  25. import javax.swing.JComponent;
  26. import javax.swing.JLabel;
  27. import javax.swing.JMenuItem;
  28. import javax.swing.JPanel;
  29. import javax.swing.JPopupMenu;
  30. import javax.swing.JToolTip;
  31. import classes.CpsObject;
  32. import classes.HolonElement;
  33. import classes.HolonObject;
  34. import classes.HolonSwitch;
  35. import classes.HolonTransformer;
  36. import ui.controller.Control;
  37. import ui.model.*;
  38. class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  39. private Image img = null; // Contains the image to draw on MyCanvas
  40. private int x = 0;
  41. private int y = 0;
  42. // edge Object Start Point
  43. private Model model;
  44. private final Control controller;
  45. Graphics2D g2; // For Painting
  46. private int cx;
  47. private int cy;
  48. private boolean dragging = false; //for dragging
  49. private boolean drawEdge = false; //for drawing edges
  50. private boolean click = false; // for double click
  51. private CpsObject tempCps = null;
  52. private Rectangle selectRect = new Rectangle();
  53. // PopUpMenu
  54. private JPopupMenu popmenu = new JPopupMenu();
  55. private JMenuItem itemDelete = new JMenuItem("Delete Object");
  56. private JToolTip objectTT = new JToolTip();
  57. public MyCanvas(final Model model, Control control) {
  58. this.add(objectTT);
  59. this.controller = control;
  60. this.model = model;
  61. popmenu.add(itemDelete);
  62. itemDelete.setEnabled(false);
  63. itemDelete.addActionListener(new ActionListener() {
  64. @Override
  65. public void actionPerformed(ActionEvent e) {
  66. model.getObjectsOnCanvas().remove(tempCps);
  67. for (CpsObject cps : model.getObjectsOnCanvas()) {
  68. cps.getConnectedTo().remove(tempCps);
  69. }
  70. tempCps = null;
  71. selectRect.setRect(0, 0, 0, 0);
  72. repaint();
  73. }
  74. });
  75. this.addMouseListener(this);
  76. this.addMouseMotionListener(this);
  77. }
  78. /**
  79. * Paints all Components on the Canvas
  80. *
  81. * @param Graphics
  82. *
  83. */
  84. public void paintComponent(Graphics g) {
  85. super.paintComponent(g);
  86. g2 = (Graphics2D) g;
  87. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, 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(),
  93. (int) selectRect.getHeight());
  94. }
  95. // drawEdges
  96. g2.setColor(Color.BLACK);
  97. if (drawEdge)
  98. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  99. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  100. for (CpsObject cps : model.getObjectsOnCanvas()) {
  101. for (CpsObject con : cps.getConnectedTo()) {
  102. if (con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
  103. g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
  104. cps.getPosition().y + controller.getScaleDiv2(),
  105. con.getPosition().x + controller.getScaleDiv2(),
  106. con.getPosition().y + controller.getScaleDiv2());
  107. }
  108. }
  109. // Highlighted Edge
  110. g2.setColor(Color.GREEN);
  111. for (CpsObject cps : model.getObjectsOnCanvas()) {
  112. for (CpsObject con : cps.getConnectedTo()) {
  113. if (con.getID() == model.getSelectedObjectID())
  114. g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
  115. cps.getPosition().y + controller.getScaleDiv2(),
  116. con.getPosition().x + controller.getScaleDiv2(),
  117. con.getPosition().y + controller.getScaleDiv2());
  118. }
  119. }
  120. // Objects
  121. for (CpsObject cps : model.getObjectsOnCanvas()) {
  122. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  123. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  124. null);
  125. }
  126. }
  127. @Override
  128. public void mouseClicked(MouseEvent e) {
  129. if (doubleClick() && tempCps != null && tempCps.getClass() == HolonSwitch.class) {
  130. System.out.println("trans double click");
  131. if (tempCps.getImage().compareTo("/Images/switch-on.png") == 0) {
  132. tempCps.setImage("/Images/switch-off.png");
  133. System.out.println("off");
  134. } else {
  135. tempCps.setImage("/Images/switch-on.png");
  136. System.out.println("on");
  137. }
  138. }
  139. repaint();
  140. }
  141. @Override
  142. public void mouseEntered(MouseEvent e) {
  143. // TODO Auto-generated method stub
  144. }
  145. @Override
  146. public void mouseExited(MouseEvent e) {
  147. // TODO Auto-generated method stub
  148. }
  149. @Override
  150. public void mousePressed(MouseEvent e) {
  151. // TODO Auto-generated method stub
  152. x = e.getX();
  153. y = e.getY();
  154. tempCps = null;
  155. // Object Selection
  156. for (CpsObject cps : model.getObjectsOnCanvas()) {
  157. cx = cps.getPosition().x;
  158. cy = cps.getPosition().y;
  159. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  160. tempCps = cps;
  161. if (e.isControlDown())
  162. drawEdge = true;
  163. }
  164. }
  165. // Object Selection Highlighting (selectRect)
  166. objectSelectionHighlighting();
  167. repaint();
  168. }
  169. @Override
  170. public void mouseReleased(MouseEvent e) {
  171. if (drawEdge) {
  172. drawEdge = false;
  173. drawDeleteEdge();
  174. }
  175. if (dragging) {
  176. x = e.getX();
  177. y = e.getY();
  178. dragging = false;
  179. tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
  180. tempCps = null;
  181. }
  182. // Rechtsklick Liste
  183. if (e.getButton() == e.BUTTON3) {
  184. if (e.getButton() == e.BUTTON3 && tempCps != null) {
  185. itemDelete.setEnabled(true);
  186. } else {
  187. itemDelete.setEnabled(false);
  188. }
  189. popmenu.show(e.getComponent(), e.getX(), e.getY());
  190. }
  191. repaint();
  192. }
  193. @Override
  194. public void mouseDragged(MouseEvent e) {
  195. // TODO Auto-generated method stub
  196. if (drawEdge) {
  197. x = e.getX();
  198. y = e.getY();
  199. repaint();
  200. } else {
  201. try {
  202. tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
  203. dragging = true;
  204. selectRect.setLocation(tempCps.getPosition().x - (controller.getScale() / 20),
  205. tempCps.getPosition().y - (controller.getScale() / 20));
  206. objectTT.setTipText(tempCps.getName());
  207. objectTT.setLocation(tempCps.getPosition().x, tempCps.getPosition().y+controller.getScale());
  208. repaint();
  209. } catch (Exception e2) {
  210. // TODO: handle exception
  211. }
  212. }
  213. }
  214. @Override
  215. public void mouseMoved(MouseEvent e) {
  216. x = e.getX();
  217. y = e.getY();
  218. boolean on = false;
  219. for (CpsObject cps : model.getObjectsOnCanvas()) {
  220. cx = cps.getPosition().x;
  221. cy = cps.getPosition().y;
  222. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  223. objectTT.setTipText(cps.getName());
  224. objectTT.setLocation(cx, cy+controller.getScale());
  225. on = true;
  226. }
  227. }
  228. if (!on) {
  229. objectTT.setLocation(-200, -200);
  230. objectTT.setTipText("");
  231. }
  232. }
  233. /**
  234. * Sets the Highlighting of the Selected Object
  235. */
  236. private void objectSelectionHighlighting() {
  237. if (tempCps != null) {
  238. selectRect.setBounds(tempCps.getPosition().x - (controller.getScale() / 20),
  239. tempCps.getPosition().y - (controller.getScale() / 20),
  240. controller.getScale() + controller.getScale() / 10,
  241. controller.getScale() + controller.getScale() / 10);
  242. controller.setSelectedObjectID(tempCps.getID());
  243. } else {
  244. controller.setSelectedObjectID(0);
  245. selectRect.setRect(0, 0, 0, 0);
  246. }
  247. }
  248. /**
  249. * Draws or Deletes an Edge
  250. */
  251. private void drawDeleteEdge() {
  252. for (CpsObject cps : model.getObjectsOnCanvas()) {
  253. cx = cps.getPosition().x;
  254. cy = cps.getPosition().y;
  255. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  256. if (!cps.getConnectedTo().contains(tempCps)) {
  257. cps.AddConnection(tempCps);
  258. tempCps.AddConnection(cps);
  259. } else {
  260. cps.getConnectedTo().remove(tempCps);
  261. tempCps.getConnectedTo().remove(cps);
  262. }
  263. }
  264. }
  265. }
  266. private boolean doubleClick(){
  267. if (click) {
  268. click = false;
  269. return true;
  270. } else {
  271. click = true;
  272. Timer t = new Timer("doubleclickTimer", false);
  273. t.schedule(new TimerTask() {
  274. @Override
  275. public void run() {
  276. click = false;
  277. }
  278. }, 500);
  279. }
  280. return false;
  281. }
  282. }