MyCanvas.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.Rectangle;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.awt.event.MouseMotionListener;
  14. import java.awt.geom.Line2D;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Timer;
  19. import java.util.TimerTask;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JPanel;
  23. import javax.swing.JPopupMenu;
  24. import javax.swing.JToolTip;
  25. import classes.CpsEdge;
  26. import classes.CpsNode;
  27. import classes.CpsObject;
  28. import classes.HolonElement;
  29. import classes.HolonObject;
  30. import classes.HolonSwitch;
  31. import classes.HolonTransformer;
  32. import ui.controller.Control;
  33. import ui.model.Model;
  34. import ui.model.idCounter;
  35. public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  36. /**
  37. *
  38. */
  39. private static final long serialVersionUID = 1L;
  40. private Image img = null; // Contains the image to draw on MyCanvas
  41. private int x = 0;
  42. private int y = 0;
  43. // edge Object Start Point
  44. private Model model;
  45. private final Control controller;
  46. Graphics2D g2; // For Painting
  47. private int cx, cy, sx, sy;
  48. ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
  49. private boolean dragging = false; // for dragging
  50. private boolean drawEdge = false; // for drawing edges
  51. private boolean click = false; // for double click
  52. private boolean doMark = false; // for double click
  53. public CpsObject tempCps = null;
  54. private Rectangle selectRect = new Rectangle();
  55. private CpsEdge edgeHighlight = null;
  56. // PopUpMenu
  57. private JPopupMenu popmenu = new JPopupMenu();
  58. private JMenuItem itemDelete = new JMenuItem("Delete Object");
  59. private JToolTip objectTT = new JToolTip();
  60. public MyCanvas(final Model model, Control control) {
  61. this.add(objectTT);
  62. this.controller = control;
  63. this.model = model;
  64. popmenu.add(itemDelete);
  65. itemDelete.setEnabled(false);
  66. itemDelete.addActionListener(new ActionListener() {
  67. @Override
  68. public void actionPerformed(ActionEvent e) {
  69. // Remove the selected Object object
  70. controller.delCanvasObject(tempCps);
  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. // drawEdges
  92. // g2.setColor(Color.BLACK);
  93. if (drawEdge) {
  94. g2.setColor(Color.BLACK);
  95. g2.setStroke(new BasicStroke(2));
  96. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  97. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  98. }
  99. for (CpsEdge con : model.getEdgesOnCanvas()) {
  100. if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
  101. && con != edgeHighlight) {
  102. if (con.getFlow() <= con.getCapacity()) {
  103. g2.setColor(Color.GREEN);
  104. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  105. } else {
  106. g2.setColor(Color.RED);
  107. g2.setStroke(new BasicStroke(2));
  108. }
  109. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  110. con.getA().getPosition().y + controller.getScaleDiv2(),
  111. con.getB().getPosition().x + controller.getScaleDiv2(),
  112. con.getB().getPosition().y + controller.getScaleDiv2());
  113. g2.drawString(con.getFlow() + "/" + con.getCapacity(),
  114. (con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
  115. (con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
  116. }
  117. }
  118. // Highlighted Edge
  119. if (model.getSelectedObjectID() > 0) {
  120. g2.setColor(Color.BLUE);
  121. for (CpsEdge con : model.getEdgesOnCanvas()) {
  122. if (con.getFlow() <= con.getCapacity()) {
  123. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  124. } else {
  125. g2.setStroke(new BasicStroke(2));
  126. }
  127. if (con.getA().getID() == model.getSelectedObjectID()
  128. || con.getB().getID() == model.getSelectedObjectID() && con != edgeHighlight) {
  129. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  130. con.getA().getPosition().y + controller.getScaleDiv2(),
  131. con.getB().getPosition().x + controller.getScaleDiv2(),
  132. con.getB().getPosition().y + controller.getScaleDiv2());
  133. g2.drawString(con.getFlow() + "/" + con.getCapacity(),
  134. (con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
  135. (con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
  136. }
  137. }
  138. } else if (edgeHighlight != null) {
  139. g2.setColor(Color.BLUE);
  140. g2.setStroke(new BasicStroke(2));
  141. g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
  142. edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
  143. edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
  144. edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
  145. g2.drawString(edgeHighlight.getFlow() + "/" + edgeHighlight.getCapacity(),
  146. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
  147. + controller.getScaleDiv2(),
  148. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
  149. + controller.getScaleDiv2());
  150. }
  151. // Objects
  152. for (CpsObject cps : model.getObjectsOnCanvas()) {
  153. if (cps.getID() == model.getSelectedObjectID() && controller.searchByID(model.getSelectedObjectID()) != null
  154. && controller.searchByID(model.getSelectedObjectID()) instanceof CpsNode) {
  155. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  156. } else {
  157. if (cps instanceof HolonSwitch) {
  158. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  159. ((HolonSwitch) cps).setState(true);
  160. } else {
  161. ((HolonSwitch) cps).setState(false);
  162. }
  163. }
  164. if (cps == tempCps) {
  165. g2.setColor(Color.BLUE);
  166. g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
  167. (int) selectRect.getHeight());
  168. } else if (cps instanceof HolonObject) {
  169. if (((HolonObject) cps).getSupplied()) {
  170. g2.setColor(Color.GREEN);
  171. } else {
  172. g2.setColor(Color.ORANGE);
  173. }
  174. g2.fillRect(cps.getPosition().x - (controller.getScale() / 20),
  175. cps.getPosition().y - (controller.getScale() / 20),
  176. controller.getScale() + ((controller.getScale() / 20) * 2),
  177. controller.getScale() + ((controller.getScale() / 20) * 2));
  178. }
  179. File checkPath = new File(cps.getImage());
  180. if (checkPath.exists()) {
  181. img = new ImageIcon(cps.getImage()).getImage();
  182. } else {
  183. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  184. }
  185. }
  186. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  187. null);
  188. }
  189. // Dragg Highlighting
  190. if (doMark) {
  191. g2.setColor(Color.BLACK);
  192. g2.setStroke(new BasicStroke(1));
  193. if (sx > x && sy > y) {
  194. g2.drawRect(x, y, sx - x, sy - y);
  195. } else if (sx < x && sy < y) {
  196. g2.drawRect(sx, sy, x - sx, y - sy);
  197. } else if (sx >= x) {
  198. g2.drawRect(x, sy, sx - x, y - sy);
  199. } else if (sy >= y) {
  200. g2.drawRect(sx, y, x - sx, sy - y);
  201. }
  202. }
  203. }
  204. @Override
  205. public void mouseClicked(MouseEvent e) {
  206. }
  207. @Override
  208. public void mouseEntered(MouseEvent e) {
  209. }
  210. @Override
  211. public void mouseExited(MouseEvent e) {
  212. }
  213. @Override
  214. public void mousePressed(MouseEvent e) {
  215. tempCps = null;
  216. dataSelected = null;
  217. edgeHighlight = null;
  218. controller.setSelecteEdge(null);
  219. // Object Selection
  220. for (CpsObject cps : model.getObjectsOnCanvas()) {
  221. cx = cps.getPosition().x;
  222. cy = cps.getPosition().y;
  223. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  224. tempCps = cps;
  225. dragging = true;
  226. // If drawing an Edge (CTRL down)
  227. if (tempCps.getClass() == HolonObject.class) {
  228. HolonObject tempObj = ((HolonObject) tempCps);
  229. dataSelected = tempObj.getElements();
  230. }
  231. if (e.isControlDown()) {
  232. drawEdge = true;
  233. dragging = false;
  234. }
  235. }
  236. }
  237. // Edge Selection
  238. if (tempCps == null) {
  239. edgeHighlight = mousePositionOnEdge(x, y);
  240. controller.setSelecteEdge(edgeHighlight);
  241. }
  242. if (edgeHighlight == null && tempCps == null) {
  243. sx = e.getX();
  244. sy = e.getY();
  245. doMark = true;
  246. }
  247. // Object Selection Highlighting (selectRect)
  248. objectSelectionHighlighting();
  249. repaint();
  250. }
  251. @Override
  252. public void mouseReleased(MouseEvent e) {
  253. if (drawEdge) {
  254. drawEdge = false;
  255. drawDeleteEdge();
  256. }
  257. // if Dragged reposition the Object
  258. if (dragging) {
  259. x = e.getX();
  260. y = e.getY();
  261. dragging = false;
  262. }
  263. // Rightclick List
  264. if (e.getButton() == MouseEvent.BUTTON3) {
  265. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  266. itemDelete.setEnabled(true);
  267. } else {
  268. itemDelete.setEnabled(false);
  269. }
  270. popmenu.show(e.getComponent(), e.getX(), e.getY());
  271. }
  272. doMark = false;
  273. controller.calculateStateForTimeStep(model.getCurIteration());
  274. repaint();
  275. }
  276. @Override
  277. public void mouseDragged(MouseEvent e) {
  278. // If Edge is drawn
  279. x = e.getX();
  280. y = e.getY();
  281. if (dragging) {
  282. try {
  283. // Au�erhalb des Randes gedragged?
  284. x = e.getX() - controller.getScaleDiv2();
  285. y = e.getY() - controller.getScaleDiv2();
  286. if (e.getX() < controller.getScaleDiv2())
  287. x = 0;
  288. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  289. x = this.getWidth() - controller.getScale();
  290. if (e.getY() < controller.getScaleDiv2())
  291. y = 0;
  292. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  293. y = this.getHeight() - controller.getScale();
  294. // Drag Position
  295. tempCps.setPosition(x, y);
  296. // Highlighting Position
  297. selectRect.setLocation(x - (controller.getScale() / 20), y - (controller.getScale() / 20));
  298. // TipText Position and name
  299. objectTT.setTipText(tempCps.getName());
  300. objectTT.setLocation(x, y + controller.getScale());
  301. repaint();
  302. } catch (Exception e2) {
  303. }
  304. }
  305. repaint();
  306. }
  307. @Override
  308. public void mouseMoved(MouseEvent e) {
  309. x = e.getX();
  310. y = e.getY();
  311. // Everytghing for the tooltip :)
  312. boolean on = false;
  313. for (CpsObject cps : model.getObjectsOnCanvas()) {
  314. cx = cps.getPosition().x;
  315. cy = cps.getPosition().y;
  316. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  317. objectTT.setTipText(cps.getName());
  318. objectTT.setLocation(cx, cy + controller.getScale());
  319. on = true;
  320. }
  321. }
  322. if (!on) {
  323. objectTT.setLocation(-200, -200);
  324. objectTT.setTipText("");
  325. }
  326. }
  327. /**
  328. * Sets the Highlighting of the Selected Object
  329. */
  330. public void objectSelectionHighlighting() {
  331. if (tempCps != null) {
  332. selectRect.setBounds(tempCps.getPosition().x - (controller.getScale() / 20),
  333. tempCps.getPosition().y - (controller.getScale() / 20),
  334. controller.getScale() + ((controller.getScale() / 20) * 2),
  335. controller.getScale() + ((controller.getScale() / 20) * 2));
  336. controller.setSelectedObjectID(tempCps.getID());
  337. } else {
  338. controller.setSelectedObjectID(0);
  339. selectRect.setRect(0, 0, 0, 0);
  340. }
  341. }
  342. /**
  343. * Draws or Deletes an Edge
  344. */
  345. private void drawDeleteEdge() {
  346. boolean node = true;
  347. boolean newEdge = true;
  348. boolean onEdge = true;
  349. boolean deleteNode = false;
  350. CpsEdge e = null;
  351. CpsObject tempCPS = null;
  352. for (CpsObject cps : model.getObjectsOnCanvas()) {
  353. cx = cps.getPosition().x;
  354. cy = cps.getPosition().y;
  355. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  356. && cps != tempCps) {
  357. node = false;
  358. onEdge = false;
  359. for (CpsEdge p : tempCps.getConnections()) {
  360. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  361. newEdge = false;
  362. e = p;
  363. }
  364. }
  365. if (!newEdge) {
  366. controller.removeEdgesOnCanvas(e);
  367. tempCps.getConnections().remove(e);
  368. cps.getConnections().remove(e);
  369. // Node ohne Edge?
  370. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  371. tempCps = e.getA();
  372. deleteNode = true;
  373. }
  374. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  375. tempCPS = e.getB();
  376. deleteNode = true;
  377. }
  378. }
  379. if (newEdge) {
  380. e = new CpsEdge(cps, tempCps);
  381. controller.AddEdgeOnCanvas(e);
  382. }
  383. }
  384. }
  385. // Edge auf eine Edge gezogen?
  386. if (onEdge) {
  387. CpsEdge p = mousePositionOnEdge(x, y);
  388. if (p != null) {
  389. CpsEdge temp = null;
  390. CpsEdge e1 = null;
  391. CpsEdge e2 = null;
  392. node = false;
  393. CpsNode n = new CpsNode("Node");
  394. n.setID(idCounter.nextId());
  395. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  396. controller.addObjectCanvas(n);
  397. CpsObject r, k;
  398. r = p.getA();
  399. k = p.getB();
  400. e = new CpsEdge(n, tempCps);
  401. e1 = new CpsEdge(n, r);
  402. e2 = new CpsEdge(n, k);
  403. p.getA().getConnections().remove(p);
  404. p.getB().getConnections().remove(p);
  405. temp = p;
  406. controller.removeEdgesOnCanvas(temp);
  407. controller.AddEdgeOnCanvas(e);
  408. controller.AddEdgeOnCanvas(e1);
  409. controller.AddEdgeOnCanvas(e2);
  410. }
  411. }
  412. // ins leere Gedragged
  413. if (node) {
  414. CpsNode n = new CpsNode("Node");
  415. n.setID(idCounter.nextId());
  416. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  417. controller.addObjectCanvas(n);
  418. e = new CpsEdge(n, tempCps);
  419. controller.AddEdgeOnCanvas(e);
  420. System.out.println("node ID: " + n.getID());
  421. }
  422. // Wenn ein Node ohne Connections da ist
  423. if (deleteNode) {
  424. controller.delCanvasObject(tempCps);
  425. controller.delCanvasObject(tempCPS);
  426. tempCPS = null;
  427. tempCps = null;
  428. objectSelectionHighlighting();
  429. }
  430. }
  431. /**
  432. * Checks if the mouse is on an Edge
  433. *
  434. * @param x
  435. * Position of the Mouse
  436. * @param y
  437. * Position of the Mouse
  438. *
  439. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  440. */
  441. public CpsEdge mousePositionOnEdge(int x, int y) {
  442. int lx, ly, hx, hy;
  443. for (CpsEdge p : model.getEdgesOnCanvas()) {
  444. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  445. p.getB().getPosition().y);
  446. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  447. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  448. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  449. } else {
  450. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  451. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  452. }
  453. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  454. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  455. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  456. } else {
  457. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  458. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  459. }
  460. // distance from a point to a line and between both Objects
  461. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  462. && y < hy) {
  463. return p;
  464. }
  465. }
  466. return null;
  467. }
  468. /**
  469. * Checks if a double click was made
  470. *
  471. * @return true if doublecklick, false if not
  472. */
  473. private boolean doubleClick() {
  474. if (click) {
  475. click = false;
  476. return true;
  477. } else {
  478. click = true;
  479. Timer t = new Timer("doubleclickTimer", false);
  480. t.schedule(new TimerTask() {
  481. @Override
  482. public void run() {
  483. click = false;
  484. }
  485. }, 500);
  486. }
  487. return false;
  488. }
  489. }