MyCanvas.java 10 KB

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