MyCanvas.java 9.2 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. //Remove the selected Object object
  67. model.getObjectsOnCanvas().remove(tempCps);
  68. for (CpsObject cps : model.getObjectsOnCanvas()) {
  69. cps.getConnectedTo().remove(tempCps);
  70. }
  71. tempCps = null;
  72. selectRect.setRect(0, 0, 0, 0);
  73. repaint();
  74. }
  75. });
  76. this.addMouseListener(this);
  77. this.addMouseMotionListener(this);
  78. }
  79. /**
  80. * Paints all Components on the Canvas
  81. *
  82. * @param Graphics
  83. *
  84. */
  85. public void paintComponent(Graphics g) {
  86. super.paintComponent(g);
  87. //Rendering
  88. g2 = (Graphics2D) g;
  89. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  90. g2.setRenderingHints(rh);
  91. // Selection
  92. if (selectRect != null) {
  93. g2.setColor(Color.GREEN);
  94. g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
  95. (int) selectRect.getHeight());
  96. }
  97. // drawEdges
  98. g2.setColor(Color.BLACK);
  99. if (drawEdge)
  100. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  101. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  102. for (CpsObject cps : model.getObjectsOnCanvas()) {
  103. for (CpsObject con : cps.getConnectedTo()) {
  104. if (con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
  105. g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
  106. cps.getPosition().y + controller.getScaleDiv2(),
  107. con.getPosition().x + controller.getScaleDiv2(),
  108. con.getPosition().y + controller.getScaleDiv2());
  109. }
  110. }
  111. // Highlighted Edge
  112. g2.setColor(Color.GREEN);
  113. for (CpsObject cps : model.getObjectsOnCanvas()) {
  114. for (CpsObject con : cps.getConnectedTo()) {
  115. if (con.getID() == model.getSelectedObjectID())
  116. g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
  117. cps.getPosition().y + controller.getScaleDiv2(),
  118. con.getPosition().x + controller.getScaleDiv2(),
  119. con.getPosition().y + controller.getScaleDiv2());
  120. }
  121. }
  122. // Objects
  123. for (CpsObject cps : model.getObjectsOnCanvas()) {
  124. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  125. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  126. null);
  127. }
  128. }
  129. @Override
  130. public void mouseClicked(MouseEvent e) {
  131. //If double clicked on a Switch change the Image to on/off
  132. if (doubleClick() && tempCps != null && tempCps.getClass() == HolonSwitch.class) {
  133. System.out.println("trans double click");
  134. if (tempCps.getImage().compareTo("/Images/switch-on.png") == 0) {
  135. tempCps.setImage("/Images/switch-off.png");
  136. System.out.println("off");
  137. } else {
  138. tempCps.setImage("/Images/switch-on.png");
  139. System.out.println("on");
  140. }
  141. }
  142. repaint();
  143. }
  144. @Override
  145. public void mouseEntered(MouseEvent e) {
  146. // TODO Auto-generated method stub
  147. }
  148. @Override
  149. public void mouseExited(MouseEvent e) {
  150. // TODO Auto-generated method stub
  151. }
  152. @Override
  153. public void mousePressed(MouseEvent e) {
  154. // TODO Auto-generated method stub
  155. x = e.getX();
  156. y = e.getY();
  157. tempCps = null;
  158. // Object Selection
  159. for (CpsObject cps : model.getObjectsOnCanvas()) {
  160. cx = cps.getPosition().x;
  161. cy = cps.getPosition().y;
  162. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  163. tempCps = cps;
  164. //If drawing an Edge (CTRL down)
  165. if (e.isControlDown())
  166. drawEdge = true;
  167. }
  168. }
  169. // Object Selection Highlighting (selectRect)
  170. objectSelectionHighlighting();
  171. repaint();
  172. }
  173. @Override
  174. public void mouseReleased(MouseEvent e) {
  175. if (drawEdge) {
  176. drawEdge = false;
  177. drawDeleteEdge();
  178. }
  179. //if Dragged reposition the Object
  180. if (dragging) {
  181. x = e.getX();
  182. y = e.getY();
  183. dragging = false;
  184. tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
  185. }
  186. // Rightclick List
  187. if (e.getButton() == e.BUTTON3) {
  188. if (e.getButton() == e.BUTTON3 && tempCps != null) {
  189. itemDelete.setEnabled(true);
  190. } else {
  191. itemDelete.setEnabled(false);
  192. }
  193. popmenu.show(e.getComponent(), e.getX(), e.getY());
  194. }
  195. repaint();
  196. }
  197. @Override
  198. public void mouseDragged(MouseEvent e) {
  199. //If Edge is drawn
  200. if (drawEdge) {
  201. x = e.getX();
  202. y = e.getY();
  203. repaint();
  204. } else {
  205. try {
  206. //Drag Position
  207. tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
  208. dragging = true;
  209. //Highlighting Position
  210. selectRect.setLocation(tempCps.getPosition().x - (controller.getScale() / 20),
  211. tempCps.getPosition().y - (controller.getScale() / 20));
  212. //TipText Position and name
  213. objectTT.setTipText(tempCps.getName());
  214. objectTT.setLocation(tempCps.getPosition().x, tempCps.getPosition().y+controller.getScale());
  215. repaint();
  216. } catch (Exception e2) {
  217. }
  218. }
  219. }
  220. @Override
  221. public void mouseMoved(MouseEvent e) {
  222. x = e.getX();
  223. y = e.getY();
  224. //Everytghin for the tooltip :)
  225. boolean on = false;
  226. for (CpsObject cps : model.getObjectsOnCanvas()) {
  227. cx = cps.getPosition().x;
  228. cy = cps.getPosition().y;
  229. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  230. objectTT.setTipText(cps.getName());
  231. objectTT.setLocation(cx, cy+controller.getScale());
  232. on = true;
  233. }
  234. }
  235. if (!on) {
  236. objectTT.setLocation(-200, -200);
  237. objectTT.setTipText("");
  238. }
  239. }
  240. /**
  241. * Sets the Highlighting of the Selected Object
  242. */
  243. private void objectSelectionHighlighting() {
  244. if (tempCps != null) {
  245. selectRect.setBounds(tempCps.getPosition().x - (controller.getScale() / 20),
  246. tempCps.getPosition().y - (controller.getScale() / 20),
  247. controller.getScale() + controller.getScale() / 10,
  248. controller.getScale() + controller.getScale() / 10);
  249. controller.setSelectedObjectID(tempCps.getID());
  250. } else {
  251. controller.setSelectedObjectID(0);
  252. selectRect.setRect(0, 0, 0, 0);
  253. }
  254. }
  255. /**
  256. * Draws or Deletes an Edge
  257. */
  258. private void drawDeleteEdge() {
  259. for (CpsObject cps : model.getObjectsOnCanvas()) {
  260. cx = cps.getPosition().x;
  261. cy = cps.getPosition().y;
  262. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  263. if (!cps.getConnectedTo().contains(tempCps)) {
  264. cps.AddConnection(tempCps);
  265. tempCps.AddConnection(cps);
  266. } else {
  267. cps.getConnectedTo().remove(tempCps);
  268. tempCps.getConnectedTo().remove(cps);
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * Checks if a double click was made
  275. *
  276. * @return true if doublecklick, false if not
  277. */
  278. private boolean doubleClick(){
  279. if (click) {
  280. click = false;
  281. return true;
  282. } else {
  283. click = true;
  284. Timer t = new Timer("doubleclickTimer", false);
  285. t.schedule(new TimerTask() {
  286. @Override
  287. public void run() {
  288. click = false;
  289. }
  290. }, 500);
  291. }
  292. return false;
  293. }
  294. }