MyCanvas.java 17 KB

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