MyCanvas.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. package ui.view;
  2. import classes.*;
  3. import javafx.scene.input.KeyCode;
  4. import com.google.gson.JsonParseException;
  5. import ui.controller.Control;
  6. import ui.controller.UpdateController;
  7. import ui.model.CableWithState;
  8. import ui.model.Consumer;
  9. import ui.model.DecoratedCable;
  10. import ui.model.DecoratedHolonObject;
  11. import ui.model.DecoratedHolonObject.HolonObjectState;
  12. import ui.model.DecoratedNetwork;
  13. import ui.model.DecoratedSwitch;
  14. import ui.model.DecoratedSwitch.SwitchState;
  15. import ui.model.MinimumModel;
  16. import ui.model.Model;
  17. import ui.model.Passiv;
  18. import ui.model.Supplier;
  19. import javax.swing.*;
  20. import java.awt.*;
  21. import java.awt.datatransfer.UnsupportedFlavorException;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.KeyEvent;
  24. import java.awt.event.KeyListener;
  25. import java.awt.event.MouseEvent;
  26. import java.awt.event.MouseListener;
  27. import java.awt.event.MouseMotionListener;
  28. import java.awt.font.LineMetrics;
  29. import java.awt.geom.Line2D;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. /**
  33. * This Class is the Canvas. All Objects will be visualized here
  34. *
  35. * @author Gruppe14
  36. */
  37. public class MyCanvas extends AbstractCanvas implements MouseListener,
  38. MouseMotionListener {
  39. private static final long serialVersionUID = 1L;
  40. private int displayOther = 0;
  41. /**
  42. * Constructor.
  43. *
  44. * @param mod
  45. * the Model
  46. * @param control
  47. * the Controller
  48. * @param unitGraph
  49. */
  50. public MyCanvas(Model mod, Control control, UnitGraph unitGraph) {
  51. toolTip = false;
  52. this.controller = control;
  53. this.model = mod;
  54. scalediv20 = model.getScale() / 20;
  55. showedInformation[0] = true;
  56. showedInformation[1] = true;
  57. showedInformation[3] = false;
  58. showedInformation[4] = true;
  59. control.setMaxCapacity(10000);
  60. setKeyBindings();
  61. popmenu.add(itemCut);
  62. popmenu.add(itemCopy);
  63. popmenu.add(itemPaste);
  64. popmenu.add(itemDelete);
  65. popmenu.add(itemAlign);
  66. popmenu.addSeparator();
  67. popmenu.add(itemGroup);
  68. popmenu.add(itemUngroup);
  69. popmenu.add(itemTrack);
  70. popmenu.add(itemUntrack);
  71. popmenu.add(itemCreateTemplate);
  72. updCon = new UpdateController(mod, control);
  73. itemDelete.setEnabled(false);
  74. itemCut.setEnabled(false);
  75. itemCopy.setEnabled(false);
  76. itemPaste.setEnabled(true);
  77. itemAlign.setEnabled(false);
  78. itemGroup.setEnabled(false);
  79. itemUngroup.setEnabled(false);
  80. itemTrack.setEnabled(false);
  81. itemUntrack.setEnabled(false);
  82. itemCut.setText(Languages.getLanguage()[95]);
  83. itemGroup.addActionListener(actionEvent -> {
  84. // calculate uppernode pos (taken from the controller)
  85. unPos = new Position(0, 0);
  86. animCps = new ArrayList<>();
  87. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  88. animCps.add(cps); // add to animation Cps ArrayList
  89. unPos.x += cps.getPosition().x;
  90. unPos.y += cps.getPosition().y;
  91. }
  92. unPos.x /= animCps.size();
  93. unPos.y /= animCps.size();
  94. // save old Position
  95. savePos = new ArrayList<>();
  96. for (int i = 0; i < animCps.size(); i++) {
  97. savePos.add(new Position(0, 0));
  98. savePos.get(i).x = animCps.get(i).getPosition().x;
  99. savePos.get(i).y = animCps.get(i).getPosition().y;
  100. }
  101. animT = new javax.swing.Timer(animDelay, actionEvent1 -> {
  102. if (animDuration - animDelay > 0 && animCps.size() > 1) {
  103. for (AbstractCpsObject animCpObject : animCps) {
  104. double x1 = animCpObject.getPosition().x - unPos.x;
  105. double y1 = animCpObject.getPosition().y - unPos.y;
  106. animCpObject.getPosition().x -= x1 / animSteps;
  107. animCpObject.getPosition().y -= y1 / animSteps;
  108. }
  109. repaint();
  110. animDuration -= animDelay;
  111. animSteps--;
  112. } else {
  113. animDuration = ANIMTIME;
  114. animSteps = animDuration / animDelay;
  115. animT.stop();
  116. for (int i = 0; i < animCps.size(); i++) {
  117. animCps.get(i).getPosition().x = savePos.get(i).x;
  118. animCps.get(i).getPosition().y = savePos.get(i).y;
  119. }
  120. controller.addUpperNode("NodeOfNode", null, animCps);
  121. controller.calculateStateForCurrentTimeStep();
  122. triggerUpdateController();
  123. repaint();
  124. }
  125. });
  126. animT.start();
  127. });
  128. itemUngroup
  129. .addActionListener(actionEvent -> {
  130. // save old Position
  131. int upperNodeId = tempCps.getId();
  132. closeUpperNodeTab(upperNodeId);
  133. savePos = new ArrayList<>();
  134. animCps = ((CpsUpperNode) tempCps).getNodes();
  135. controller.delUpperNode((CpsUpperNode) tempCps, null);
  136. for (int i = 0; i < animCps.size(); i++) {
  137. savePos.add(new Position(0, 0));
  138. savePos.get(i).x = animCps.get(i).getPosition().x;
  139. savePos.get(i).y = animCps.get(i).getPosition().y;
  140. }
  141. for (AbstractCpsObject cps : animCps) {
  142. int x = tempCps.getPosition().x;
  143. int y = tempCps.getPosition().y;
  144. cps.setPosition(new Position(x, y));
  145. }
  146. animT = new javax.swing.Timer(
  147. animDelay,
  148. actionEvent1 -> {
  149. if (animDuration - animDelay >= 0) {
  150. for (int i = 0; i < animCps.size(); i++) {
  151. Position pos = animCps.get(i).getPosition();
  152. double x1 = pos.x - savePos.get(i).x;
  153. double y1 = pos.y - savePos.get(i).y;
  154. animCps.get(i).getPosition().x -= x1 / animSteps;
  155. animCps.get(i).getPosition().y -= y1 / animSteps;
  156. }
  157. repaint();
  158. animDuration -= animDelay;
  159. animSteps--;
  160. } else {
  161. animDuration = ANIMTIME;
  162. animSteps = animDuration / animDelay;
  163. animT.stop();
  164. for (int i = 0; i < animCps.size(); i++) {
  165. animCps.get(i).getPosition().x = savePos
  166. .get(i).x;
  167. animCps.get(i).getPosition().y = savePos
  168. .get(i).y;
  169. }
  170. controller
  171. .calculateStateForCurrentTimeStep();
  172. triggerUpdateController();
  173. repaint();
  174. }
  175. });
  176. animT.start();
  177. });
  178. // adds the selected object(s) to the statistic panel
  179. itemTrack.addActionListener(actionEvent -> {
  180. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  181. if (!((controller.getTrackingObj() != null) && (controller.getTrackingObj().contains(o)))) {
  182. controller.addTrackingObj(o);
  183. if (o instanceof HolonObject) {
  184. ((HolonObject) o).updateTrackingInfo();
  185. }
  186. }
  187. if (model.getShowConsoleLog()) {
  188. controller.addTextToConsole("Tracking: ", Color.BLACK, 12,
  189. false, false, false);
  190. controller.addTextToConsole("" + o.getName(), Color.BLUE,
  191. 12, true, false, false);
  192. controller.addTextToConsole(", ID:", Color.BLACK, 12,
  193. false, false, false);
  194. controller.addTextToConsole("" + o.getId(), Color.RED, 12,
  195. true, false, true);
  196. }
  197. }
  198. });
  199. itemUntrack.addActionListener(actionEvent -> {
  200. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  201. if (o instanceof HolonObject) {
  202. boolean found = false;
  203. if (controller.getTrackingObj() != null) {
  204. for (AbstractCpsObject obj : controller
  205. .getTrackingObj()) {
  206. if (obj instanceof HolonObject) {
  207. if (obj.getId() == o.getId()) {
  208. found = true;
  209. }
  210. }
  211. }
  212. }
  213. if (found) {
  214. // Removed from tracking array and tracking
  215. // information reseted
  216. controller.removeTrackingObj(o);
  217. ((HolonObject) o).setTrackingProd(new float[100]);
  218. ((HolonObject) o).setTrackingCons(new float[100]);
  219. }
  220. if (model.getShowConsoleLog()) {
  221. controller.addTextToConsole("Untracking: ", Color.BLACK, 12,
  222. false, false, false);
  223. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12,
  224. true, false, false);
  225. controller.addTextToConsole(", ID:", Color.BLACK, 12, false,
  226. false, false);
  227. controller.addTextToConsole("" + o.getId(), Color.RED, 12,
  228. true, false, true);
  229. }
  230. }
  231. }
  232. }) ;
  233. itemDelete.addActionListener(actionEvent -> {
  234. // Remove the selected Object objects
  235. //Edge Deleting
  236. if (tempCps == null && edgeHighlight != null) {
  237. controller.removeEdgesOnCanvas(edgeHighlight);
  238. //Look for a CPSNode with no Connections and delete them
  239. if(edgeHighlight.getA().getClass() == CpsNode.class && edgeHighlight.getA().getConnections().size() == 0){
  240. controller.delCanvasObject(edgeHighlight.getA(), false);
  241. }
  242. if(edgeHighlight.getB().getClass() == CpsNode.class && edgeHighlight.getB().getConnections().size() == 0){ //Look on the other end of the cable
  243. controller.delCanvasObject(edgeHighlight.getB(), false);
  244. }
  245. edgeHighlight = null;
  246. }
  247. boolean save = false;
  248. for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {
  249. AbstractCpsObject cps = model.getSelectedCpsObjects()
  250. .get(j);
  251. if (j == model.getSelectedCpsObjects().size() - 1)
  252. save = true;
  253. controller.delCanvasObject(cps, save);
  254. controller.removeTrackingObj(cps);
  255. // Remove UpperNodeTab if UpperNode deleted
  256. if (cps instanceof CpsUpperNode) {
  257. JSplitPane tempSplit = (JSplitPane) getParent().getParent()
  258. .getParent().getParent();
  259. JTabbedPane tabbedPane;
  260. JTabbedPane tabbedPane2;
  261. // if SplitView is activated
  262. if (tempSplit.getLeftComponent() instanceof JTabbedPane
  263. && tempSplit.getRightComponent() instanceof JTabbedPane) {
  264. tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
  265. tabbedPane2 = (JTabbedPane) tempSplit
  266. .getRightComponent();
  267. } else {
  268. tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
  269. tabbedPane2 = null;
  270. }
  271. // Look if the uppernode is open in a Tab
  272. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  273. if (tabbedPane.getComponentAt(i) != null
  274. && ((UpperNodeCanvas) ((JScrollPane) tabbedPane
  275. .getComponentAt(i)).getViewport()
  276. .getComponent(0)).upperNode.getId() == cps
  277. .getId()) {
  278. ((ButtonTabComponent) tabbedPane
  279. .getTabComponentAt(i)).removeTabs();
  280. break;
  281. }
  282. }
  283. // If SplitView is on and the view on
  284. // tabbedPane2 is the deleted upperNode
  285. try {
  286. if (tabbedPane2 != null
  287. && ((UpperNodeCanvas) ((JScrollPane) tabbedPane2
  288. .getSelectedComponent()).getViewport()
  289. .getComponent(0)).upperNode.getId() == cps
  290. .getId()) {
  291. ((ButtonTabComponent) tabbedPane
  292. .getTabComponentAt(tabbedPane2
  293. .getSelectedIndex())).removeTabs();
  294. }
  295. } catch (Exception e2) {
  296. }
  297. }
  298. toolTip = false;
  299. }
  300. model.getSelectedCpsObjects().clear();
  301. tempCps = null;
  302. repaint();
  303. });
  304. itemCut.addActionListener(actionEvent -> {
  305. controller.cut(null);
  306. itemPaste.setEnabled(true);
  307. repaint();
  308. });
  309. itemCopy.addActionListener(actionEvent -> {
  310. if(tempCps instanceof CpsUpperNode)
  311. controller.getObjectsInDepth();
  312. controller.copy(null);
  313. itemPaste.setEnabled(true);
  314. repaint();
  315. });
  316. itemAlign.addActionListener(actionEvent ->
  317. {
  318. for(AbstractCpsObject cps: model.getSelectedCpsObjects())
  319. align(cps, model.getScaleDiv2());
  320. repaint();
  321. });
  322. itemPaste
  323. .addActionListener(actionEvent -> {
  324. try {
  325. controller.paste(null, mousePosition);
  326. unitGraph.update(model.getSelectedCpsObjects());
  327. } catch (JsonParseException | UnsupportedFlavorException
  328. | IOException e1) {
  329. JLabel message = new JLabel(
  330. "The Clipboard information cannot be pastet into Application.");
  331. JOptionPane.showMessageDialog(null, message, "",
  332. JOptionPane.ERROR_MESSAGE);
  333. }
  334. repaint();
  335. });
  336. /*
  337. * create Template
  338. */
  339. itemCreateTemplate.addActionListener(actionEvent -> {
  340. controller.createTemplate((HolonObject)tempCps,(JFrame)SwingUtilities.getRoot(this));
  341. });
  342. this.addMouseListener(this);
  343. this.addMouseMotionListener(this);
  344. }
  345. /**
  346. * Paints all Components on the Canvas.
  347. *
  348. * @param g
  349. * Graphics
  350. */
  351. private Color getStateColor(HolonObjectState state) {
  352. switch(state) {
  353. case NOT_SUPPLIED:
  354. return new Color(230, 120, 100);
  355. case NO_ENERGY:
  356. return Color.white;
  357. case OVER_SUPPLIED:
  358. return new Color(138, 43, 226);
  359. case PARTIALLY_SUPPLIED:
  360. return Color.yellow;
  361. case PRODUCER:
  362. return Color.lightGray;
  363. case SUPPLIED:
  364. return Color.green;
  365. default:
  366. return Color.BLACK;
  367. }
  368. }
  369. private void paintCanvasObject(Graphics2D g, DecoratedHolonObject decoratedHolonObject){
  370. Position pos = decoratedHolonObject.getModel().getPosition();
  371. Color statecolor = getStateColor(decoratedHolonObject.getState());
  372. g.setColor(statecolor);
  373. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  374. drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
  375. }
  376. private void drawCanvasObjectString(Graphics2D g, Position posOfCanvasObject, float energy) {
  377. g.setColor(Color.BLACK);
  378. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (controller.getScale() / 4f) ));
  379. g.drawString((energy > 0)? "+" + Float.toString(energy): Float.toString(energy), posOfCanvasObject.x - controller.getScaleDiv2(), posOfCanvasObject.y - controller.getScaleDiv2() - 1);
  380. }
  381. private void paintConsumer(Graphics2D g, Consumer con){
  382. paintCanvasObject(g, con);
  383. paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
  384. drawCanvasObjectString(g, con.getModel().getPosition(), con.getEnergyNeededFromNetwork());
  385. }
  386. private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
  387. g.drawImage(Util.loadImage(Image) ,
  388. pos.x - controller.getScaleDiv2(),
  389. pos.y - controller.getScaleDiv2(),
  390. controller.getScale(), controller.getScale(), null);
  391. }
  392. private void paintCable(Graphics2D g, DecoratedCable cable)
  393. {
  394. Position start = cable.getModel().getA().getPosition();
  395. Position end = cable.getModel().getB().getPosition();
  396. float currentEnergy = cable.getFlowEnergy();
  397. float capacity = cable.getModel().getCapacity();
  398. g.setColor(Color.BLACK);
  399. g.setStroke(new BasicStroke(2));
  400. switch(cable.getState()) {
  401. case Burned:
  402. g.setColor(Color.RED);
  403. g.setStroke(new BasicStroke(2));
  404. break;
  405. case Working:
  406. g.setColor(Color.GREEN);
  407. g.setStroke(new BasicStroke((currentEnergy / capacity* 3) + 1));
  408. break;
  409. }
  410. g.drawLine(start.x, start.y, end.x, end.y);
  411. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  412. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  413. g.drawString(currentEnergy + "/" + capacity , middle.x, middle.y);
  414. }
  415. private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
  416. {
  417. drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
  418. }
  419. private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
  420. // +1, -2, -1 little Ajustment for pixelperfect allignment
  421. int barWidth = (int) (controller.getScale());
  422. int barHeight = (int) (controller.getScale() / 5);
  423. g.setColor(Color.WHITE);
  424. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  425. g.setColor(color);
  426. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  427. g.setColor(Color.BLACK);
  428. g.setStroke(new BasicStroke(1));
  429. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  430. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  431. String percentageString = (Math.round((percentage * 100))) + "%";
  432. int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  433. if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
  434. g.drawString(percentageString, pos.x + 1 - stringWidth / 2, pos.y + controller.getScaleDiv2() - 1+ barHeight);
  435. }
  436. //old code
  437. void drawMarker(Graphics2D g) {
  438. if (sx > x && sy > y) {
  439. g.drawRect(x, y, sx - x, sy - y);
  440. } else if (sx < x && sy < y) {
  441. g.drawRect(sx, sy, x - sx, y - sy);
  442. } else if (sx >= x) {
  443. g.drawRect(x, sy, sx - x, y - sy);
  444. } else if (sy >= y) {
  445. g.drawRect(sx, y, x - sx, sy - y);
  446. }
  447. }
  448. public void paintComponent(Graphics g) {
  449. super.paintComponent(g);
  450. Graphics2D g2d = (Graphics2D) g;
  451. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  452. RenderingHints.VALUE_ANTIALIAS_ON));
  453. //-->Old code
  454. if (drawEdge) {
  455. g2d.setColor(Color.BLACK);
  456. g2d.setStroke(new BasicStroke(1));
  457. g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  458. }
  459. //<--
  460. //timstep:
  461. g.setFont(new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  462. g.drawString("DEBUG Timestep:" + controller.getSimManager().getActualDecorState().getTimestepOfState() + ((displayOther != 0) ? " " + ((displayOther > 0) ? "+" + displayOther: displayOther): "") , 10, 10);
  463. g2d.setColor(Color.BLACK);
  464. for(DecoratedCable cable : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getLeftOverEdges()) {
  465. paintCable(g2d, cable);
  466. }
  467. for(DecoratedNetwork network : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getNetworkList()) {
  468. for(DecoratedCable cable : network.getDecoratedCableList()) {
  469. paintCable(g2d, cable);
  470. }
  471. for(Consumer con: network.getConsumerList()) {
  472. paintConsumer(g2d, con);
  473. }
  474. for(Consumer con: network.getConsumerSelfSuppliedList()) {
  475. paintConsumer(g2d, con);
  476. }
  477. for(Supplier sup: network.getSupplierList()) {
  478. paintCanvasObject(g2d, sup);
  479. }
  480. for(Passiv pas: network.getPassivNoEnergyList()) {
  481. paintCanvasObject(g2d, pas);
  482. }
  483. }
  484. for(DecoratedSwitch dSwitch : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getDecoratedSwitches()) {
  485. paintSwitch(g2d, dSwitch);
  486. }
  487. //should be in DecorState
  488. for(CpsNode node : controller.getSimManager().getActualDecorStateWithOffSet(displayOther).getNodeList()) {
  489. drawCanvasObject(g2d, "/Images/node_selected.png" , node.getPosition());
  490. }
  491. //oldCode
  492. if (doMark) {
  493. g2d.setColor(Color.BLACK);
  494. g2d.setStroke(new BasicStroke(0));
  495. drawMarker(g2d);
  496. }
  497. /*
  498. *
  499. for(HolonSwitch switch2 : controller.getSimManager().getMinimumModel().getSwitchList()) {
  500. drawCanvasObject(g2d, switch2.getSwitchClosedImage() , switch2.getPosition());
  501. }
  502. */
  503. // String maxCap = null;
  504. // // Rendering
  505. // g2 = (Graphics2D) g;
  506. // RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  507. // RenderingHints.VALUE_ANTIALIAS_ON);
  508. // g2.setRenderingHints(rh);
  509. //
  510. // // Paint the Background
  511. // if (!model.getCanvasImagePath().isEmpty()) {
  512. // img = new ImageIcon(model.getCanvasImagePath()).getImage();
  513. // switch (model.getCanvasImageMode()) {
  514. // case BackgroundPopUp.IMAGE_PIXELS:
  515. // g2.drawImage(img, 0, 0, img.getWidth(null),
  516. // img.getHeight(null), null);
  517. // break;
  518. // case BackgroundPopUp.STRETCHED:
  519. // g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(),
  520. // null);
  521. // break;
  522. // case BackgroundPopUp.CUSTOM:
  523. // g2.drawImage(img, 0, 0, model.getCanvasImageWidth(),
  524. // model.getCanvasImageHeight(), null);
  525. // break;
  526. // default:
  527. // break;
  528. // }
  529. // }
  530. //
  531. // // SubNet Coloring
  532. // int i = 0;
  533. // for (SubNet s : controller.getSimManager().getSubNets()) {
  534. //
  535. // if (model.getSubNetColors().size() - 1 < i) {
  536. // controller.addSubNetColor(new Color(
  537. // (int) (Math.random() * 255),
  538. // (int) (Math.random() * 255),
  539. // (int) (Math.random() * 255)));
  540. // }
  541. // if (showedInformation[3]) {
  542. // for (HolonObject cps : s.getObjects()) {
  543. // cps.setBorderColor(model.getSubNetColors().get(i));
  544. // }
  545. // }
  546. // i++;
  547. // }
  548. //
  549. // // drawEdges that is being dragged
  550. // if (drawEdge) {
  551. // g2.setColor(Color.BLACK);
  552. // g2.setStroke(new BasicStroke(2));
  553. // g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  554. // }
  555. //
  556. // if(model.getEdgesOnCanvas().isEmpty() && !model.getObjectsOnCanvas().isEmpty()
  557. // && !model.getObjectsOnCanvas().get(0).getConnections().isEmpty()){
  558. // ArrayList<CpsEdge> edgesOnCanvas= model.getEdgesOnCanvas();
  559. // for(AbstractCpsObject cps :model.getObjectsOnCanvas())
  560. // for(CpsEdge e: cps.getConnections()){
  561. // if(!edgesOnCanvas.contains(e))
  562. // model.addEdgeOnCanvas(e);
  563. // }
  564. // }
  565. //
  566. // for (CpsEdge con : model.getEdgesOnCanvas()) {
  567. // maxCap = paintEdge(con, maxCap);
  568. // }
  569. //
  570. // // Highlighted Edge
  571. // if (!model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty() || model.getSelectedObjectID() > 0) {
  572. // g2.setColor(Color.BLUE);
  573. // for (CpsEdge con : model.getEdgesOnCanvas()) {
  574. // if (con.getFlow() <= con.getCapacity()) {
  575. // g2.setStroke(new BasicStroke(Math.min(
  576. // ((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  577. // } else {
  578. // g2.setStroke(new BasicStroke(2));
  579. // }
  580. //
  581. // maxCap = drawEdgeLine(con, maxCap);
  582. // }
  583. // } else if (edgeHighlight != null) {
  584. // g2.setColor(Color.BLUE);
  585. // if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  586. // g2.setStroke(new BasicStroke(Math.min(((edgeHighlight.getFlow()
  587. // / edgeHighlight.getCapacity() * 3) + 1), 4)));
  588. // } else {
  589. // g2.setStroke(new BasicStroke(2));
  590. // }
  591. // g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight
  592. // .getA().getPosition().y,
  593. // edgeHighlight.getB().getPosition().x, edgeHighlight.getB()
  594. // .getPosition().y);
  595. //
  596. // maxCap = setCapacityString(edgeHighlight, maxCap);
  597. //
  598. // if (showedInformation[0]) {
  599. // g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  600. // (edgeHighlight.getA().getPosition().x + edgeHighlight
  601. // .getB().getPosition().x) / 2, (edgeHighlight
  602. // .getA().getPosition().y + edgeHighlight.getB()
  603. // .getPosition().y) / 2);
  604. // }
  605. // }
  606. //
  607. //
  608. // /**
  609. // * highlight the Object that would be replaced
  610. // */
  611. // highlightMayBeReplaced(g2);
  612. //
  613. // // Objects
  614. // for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  615. // // Border Highlighting
  616. // if (showedInformation[3]) {
  617. // g2.setColor(cps.getBorderColor());
  618. // if (g2.getColor() != Color.WHITE && !(cps instanceof CpsNode)) {
  619. // g2.fillRect(
  620. // (int) (cps.getPosition().x
  621. // - controller.getScaleDiv2() - scalediv20 - 3),
  622. // (int) (cps.getPosition().y
  623. // - controller.getScaleDiv2() - scalediv20 - 3),
  624. // (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  625. // (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  626. // }
  627. // }
  628. //
  629. // setEdgePictureAndHighlighting(cps);
  630. //
  631. // g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
  632. // cps.getPosition().y - controller.getScaleDiv2(),
  633. // controller.getScale(), controller.getScale(), null);
  634. //
  635. // paintSupplyBar(g, cps);
  636. // }
  637. //
  638. // // Dragged marker Highlighting
  639. // if (doMark) {
  640. // g2.setColor(Color.BLACK);
  641. // g2.setStroke(new BasicStroke(0));
  642. // drawMarker();
  643. // }
  644. // // Tooltip
  645. // showTooltip(g);
  646. }
  647. @Override
  648. public void mouseClicked(MouseEvent e) {
  649. if (e.getButton() == MouseEvent.BUTTON1) {
  650. DefaulTable propertyTable = model.getPropertyTable();
  651. if (propertyTable.getRowCount() > 0) {
  652. for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
  653. propertyTable.removeRow(i);
  654. }
  655. }
  656. triggerUpdateController();
  657. }
  658. stopEditing();
  659. }
  660. @Override
  661. public void mouseEntered(MouseEvent e) {
  662. }
  663. @Override
  664. public void mouseExited(MouseEvent e) {
  665. }
  666. @Override
  667. public void mousePressed(MouseEvent e) {
  668. stopEditing();
  669. tempCps = null;
  670. edgeHighlight = null;
  671. controller.setSelecteEdge(null);
  672. // Object Selection
  673. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  674. cx = cps.getPosition().x - controller.getScaleDiv2();
  675. cy = cps.getPosition().y - controller.getScaleDiv2();
  676. if (x - controller.getScale() <= cx
  677. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  678. tempCps = cps;
  679. setConsoleTextAfterSelect(cps);
  680. dragging = true;
  681. if (e.isControlDown() && tempCps != null) {
  682. if (model.getSelectedCpsObjects().contains(tempCps)) {
  683. controller.deleteSelectedObject(tempCps);
  684. //TODO: RemoveDepth
  685. } else {
  686. controller.addSelectedObject(tempCps);
  687. if(tempCps instanceof CpsUpperNode)
  688. controller.getObjectsInDepth();
  689. }
  690. }
  691. // If drawing an Edge (CTRL down)
  692. if (tempCps.getClass() == HolonObject.class) {
  693. HolonObject tempObj = ((HolonObject) tempCps);
  694. dataSelected = tempObj.getElements();
  695. }
  696. if (e.isShiftDown()) {
  697. drawEdge = true;
  698. dragging = false;
  699. }
  700. }
  701. }
  702. // Edge Selection
  703. if (tempCps == null) {
  704. edgeHighlight = mousePositionOnEdge(x, y);
  705. controller.setSelecteEdge(edgeHighlight);
  706. controller.setSelectedObjectID(0);
  707. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  708. model.getSelectedCpsObjects().clear();
  709. }
  710. }
  711. if (edgeHighlight == null && tempCps == null) {
  712. sx = e.getX();
  713. sy = e.getY();
  714. doMark = true;
  715. }
  716. repaint();
  717. }
  718. @Override
  719. public void mouseReleased(MouseEvent e) {
  720. x = e.getX();
  721. y = e.getY();
  722. dragging = false;
  723. if (drawEdge) {
  724. drawEdge = false;
  725. drawDeleteEdge();
  726. }
  727. if (dragged) {
  728. try {
  729. /**
  730. * Save before further Dragged interactions happen
  731. */
  732. controller.autoSave();
  733. } catch (IOException ex) {
  734. System.err.println("AutoSave error by dragging");
  735. ex.printStackTrace();
  736. }
  737. /**
  738. * check if a unique tempCps could replace an Object on the canvas
  739. */
  740. if(model.getSelectedCpsObjects().size()==1
  741. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  742. /**
  743. * if UpperNode would be replaced, close its tabs
  744. */
  745. if(mayBeReplaced instanceof CpsUpperNode)
  746. closeUpperNodeTab(mayBeReplaced.getId());
  747. /**
  748. * replace on canvas (will save)
  749. */
  750. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  751. mayBeReplaced=null;
  752. }
  753. }
  754. if (!e.isControlDown() && !dragged && tempCps != null
  755. && MouseEvent.BUTTON3 != e.getButton()) {
  756. model.getSelectedCpsObjects().clear();
  757. controller.addSelectedObject(tempCps);
  758. model.setSelectedCpsObject(tempCps);
  759. if(tempCps instanceof CpsUpperNode)
  760. controller.getObjectsInDepth();
  761. }
  762. dragged = false;
  763. // Rightclick List
  764. setRightClickMenu(e);
  765. markObjects();
  766. if (doubleClick() && tempCps instanceof HolonSwitch
  767. && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
  768. ((HolonSwitch) tempCps).switchState();
  769. }
  770. controller.calculateStateForTimeStep(model.getCurIteration());
  771. triggerUpdateController();
  772. repaint();
  773. }
  774. @Override
  775. public void mouseDragged(MouseEvent e) {
  776. // If Edge is drawn
  777. x = e.getX();
  778. y = e.getY();
  779. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  780. model.getSelectedCpsObjects().clear();
  781. if (tempCps != null) {
  782. controller.addSelectedObject(tempCps);
  783. }
  784. }
  785. if (dragging) {
  786. try {
  787. dragged = true;
  788. float xDist, yDist; // Distance
  789. x = e.getX();
  790. y = e.getY();
  791. // Make sure its in bounds
  792. int scaleDiv2 = controller.getScaleDiv2();
  793. if (e.getX() < scaleDiv2)
  794. x = scaleDiv2;
  795. else if (e.getX() > this.getWidth() - scaleDiv2)
  796. x = this.getWidth() - scaleDiv2;
  797. if (e.getY() < scaleDiv2)
  798. y = scaleDiv2;
  799. else if (e.getY() > this.getHeight()
  800. - scaleDiv2)
  801. y = this.getHeight() - scaleDiv2;
  802. // Distance
  803. xDist = x - tempCps.getPosition().x;
  804. yDist = y - tempCps.getPosition().y;
  805. tempCps.setPosition(x, y); // Drag Position
  806. // ToolTipText Position and name
  807. toolTip = true;
  808. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  809. toolTipPos.x = tempCps.getPosition().x
  810. - scaleDiv2;
  811. toolTipPos.y = tempCps.getPosition().y
  812. + scaleDiv2;
  813. // All Selected Objects
  814. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  815. if (cps != tempCps) {
  816. x = (int) (cps.getPosition().x + xDist);
  817. y = (int) (cps.getPosition().y + yDist);
  818. // Make sure its in bounds
  819. if (x <= scaleDiv2)
  820. x = scaleDiv2;
  821. else if (x > this.getWidth()
  822. - scaleDiv2)
  823. x = this.getWidth() - scaleDiv2;
  824. if (y <= scaleDiv2)
  825. y = scaleDiv2;
  826. else if (y > this.getHeight()
  827. - scaleDiv2)
  828. y = this.getHeight() - scaleDiv2;
  829. cps.setPosition(x, y);
  830. }
  831. }
  832. /**
  833. * check if something might be replaced
  834. */
  835. if(model.getSelectedCpsObjects().size()==1)
  836. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  837. repaint();
  838. } catch (Exception eex) {
  839. }
  840. }
  841. // Mark Objects
  842. if (doMark) {
  843. tempSelected.clear();
  844. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  845. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  846. if (sx >= x) {
  847. x1 = x;
  848. x2 = sx;
  849. }
  850. if (sy >= y) {
  851. y1 = y;
  852. y2 = sy;
  853. }
  854. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  855. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  856. && x2 >= cps.getPosition().x
  857. && y2 >= cps.getPosition().y) {
  858. tempSelected.add(cps);
  859. }
  860. }
  861. }
  862. repaint();
  863. }
  864. @Override
  865. public void mouseMoved(MouseEvent e) {
  866. x = e.getX();
  867. y = e.getY();
  868. // Everything for the tooltip :)
  869. boolean on = false;
  870. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  871. cx = cps.getPosition().x - controller.getScaleDiv2();
  872. cy = cps.getPosition().y - controller.getScaleDiv2();
  873. on = setToolTipInfoAndPosition(on, cps);
  874. }
  875. if(on||(!on && toolTip))
  876. repaint();
  877. toolTip = on;
  878. }
  879. /**
  880. * Draws or Deletes an Edge.
  881. */
  882. void drawDeleteEdge() {
  883. if (getMousePosition() != null) {
  884. boolean node = true;
  885. boolean newEdge = true;
  886. boolean onEdge = true;
  887. boolean deleteNode = false;
  888. CpsEdge e = null;
  889. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  890. cx = cps.getPosition().x - controller.getScaleDiv2();
  891. cy = cps.getPosition().y - controller.getScaleDiv2();
  892. if (x - controller.getScale() <= cx
  893. && y - controller.getScale() <= cy && x >= cx
  894. && y >= cy && cps != tempCps) {
  895. node = false;
  896. onEdge = false;
  897. for (CpsEdge p : tempCps.getConnections()) {
  898. if ((p.getA() == tempCps && p.getB() == cps)
  899. || (p.getB() == tempCps && p.getA() == cps)) {
  900. newEdge = false;
  901. e = p;
  902. }
  903. }
  904. if (!newEdge) {
  905. controller.removeEdgesOnCanvas(e);
  906. // Node ohne Edge?
  907. if (e.getA().getClass() == CpsNode.class
  908. && e.getA().getConnections().isEmpty()) {
  909. tempCps = e.getA();
  910. deleteNode = true;
  911. }
  912. if (e.getB().getClass() == CpsNode.class
  913. && e.getB().getConnections().isEmpty()) {
  914. deleteNode = true;
  915. }
  916. } else {
  917. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  918. controller.addEdgeOnCanvas(e);
  919. }
  920. }
  921. }
  922. // Edge auf eine Edge gezogen?
  923. if (onEdge && !checkForReplacement(x, y)) {
  924. CpsEdge p = mousePositionOnEdge(x, y);
  925. if (p != null) {
  926. CpsEdge e1;
  927. CpsEdge e2;
  928. node = false;
  929. CpsNode n = new CpsNode("Node");
  930. n.setPosition(x, y);
  931. controller.addObjectCanvas(n);
  932. AbstractCpsObject r, k;
  933. r = p.getA();
  934. k = p.getB();
  935. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  936. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  937. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  938. controller.removeEdgesOnCanvas(p);
  939. controller.addEdgeOnCanvas(e);
  940. controller.addEdgeOnCanvas(e1);
  941. controller.addEdgeOnCanvas(e2);
  942. }
  943. }else{
  944. mayBeReplaced = null;
  945. }
  946. // ins leere Gedragged
  947. if (node && !checkForReplacement(x, y)) {
  948. CpsNode n = new CpsNode("Node");
  949. n.setPosition(x, y);
  950. controller.addObjectCanvas(n);
  951. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  952. controller.addEdgeOnCanvas(e);
  953. }else{
  954. mayBeReplaced = null;
  955. }
  956. // Wenn ein Node ohne Connections da ist
  957. if (deleteNode) {
  958. controller.delCanvasObject(tempCps, true);
  959. tempCps = null;
  960. }
  961. }
  962. }
  963. /**
  964. * Checks if the mouse is on an Edge.
  965. *
  966. * @param x
  967. * Position of the Mouse
  968. * @param y
  969. * Position of the Mouse
  970. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  971. */
  972. private CpsEdge mousePositionOnEdge(int x, int y) {
  973. x += controller.getScaleDiv2();
  974. y += controller.getScaleDiv2();
  975. for (CpsEdge p : model.getEdgesOnCanvas()) {
  976. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  977. .getPosition().y, p.getB().getPosition().x, p.getB()
  978. .getPosition().y);
  979. int[] positions = determineMousePositionOnEdge(p);
  980. int lx = positions[0];
  981. int ly = positions[1];
  982. int hx = positions[2];
  983. int hy = positions[3];
  984. // distance from a point to a line and between both Objects
  985. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  986. y - model.getScaleDiv2()) < 20
  987. && x > lx && x < hx && y > ly && y < hy) {
  988. return p;
  989. }
  990. }
  991. return null;
  992. }
  993. void updateLanguages() {
  994. itemCut.setText(Languages.getLanguage()[95]);
  995. itemCopy.setText(Languages.getLanguage()[96]);
  996. itemPaste.setText(Languages.getLanguage()[97]);
  997. itemDelete.setText(Languages.getLanguage()[98]);
  998. itemGroup.setText(Languages.getLanguage()[99]);
  999. itemUngroup.setText(Languages.getLanguage()[100]);
  1000. itemTrack.setText(Languages.getLanguage()[101]);
  1001. itemUntrack.setText(Languages.getLanguage()[102]);
  1002. }
  1003. /**
  1004. * Set if Information should be shown.
  1005. *
  1006. * @param connection
  1007. * boolean for conecction
  1008. * @param object
  1009. * boolean for objects
  1010. * @param nodeOfnode
  1011. */
  1012. void setShowedInformation(boolean connection, boolean object,
  1013. boolean border, boolean nodeOfnode) {
  1014. showedInformation[0] = connection;
  1015. showedInformation[1] = object;
  1016. showedInformation[3] = border;
  1017. showedInformation[4] = nodeOfnode;
  1018. }
  1019. /**
  1020. * Returns if Information should be shown.
  1021. *
  1022. * @return Array of boolean [0] = connection, [1] = objects
  1023. */
  1024. boolean[] getShowedInformation() {
  1025. return showedInformation;
  1026. }
  1027. /**
  1028. * set toolTip
  1029. *
  1030. * @param bool
  1031. */
  1032. void setToolTip(boolean bool) {
  1033. this.toolTip = bool;
  1034. }
  1035. /**
  1036. * Set the Mouse
  1037. *
  1038. * @param x
  1039. * @param y
  1040. */
  1041. void setXY(int x, int y) {
  1042. this.x = x;
  1043. this.y = y;
  1044. }
  1045. @Override
  1046. public boolean checkForReplacement(int x, int y) {
  1047. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  1048. }
  1049. @Override
  1050. public void tryToAlignObjects(){
  1051. /**
  1052. * Align all Objects
  1053. */
  1054. for(AbstractCpsObject cps: model.getObjectsOnCanvas())
  1055. align(cps,3*model.getScaleDiv2());
  1056. /**
  1057. * AutoSave new Positons
  1058. */
  1059. try{
  1060. controller.autoSave();
  1061. } catch (IOException ex) {
  1062. System.err.println("AutoSave error by aligning");
  1063. ex.printStackTrace();
  1064. }
  1065. }
  1066. @Override
  1067. public void closeUpperNodeTab(int upperNodeId) {
  1068. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  1069. .getParent().getParent().getParent();
  1070. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  1071. if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner
  1072. .getComponentAt(i)).getViewport().getComponent(
  1073. 0)).upperNode.getId() == upperNodeId) {
  1074. tabbedPaneInner.remove(i);
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. private void setKeyBindings() {
  1080. ActionMap actionMap = getActionMap();
  1081. int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
  1082. InputMap inputMap = getInputMap(condition );
  1083. String vkLeft = "VK_UP";
  1084. String vkRight = "VK_DOWN";
  1085. inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), vkLeft);
  1086. inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), vkRight);
  1087. actionMap.put(vkLeft, new KeyAction(vkLeft));
  1088. actionMap.put(vkRight, new KeyAction(vkRight));
  1089. }
  1090. @SuppressWarnings("serial")
  1091. private class KeyAction extends AbstractAction {
  1092. public KeyAction(String actionCommand) {
  1093. putValue(ACTION_COMMAND_KEY, actionCommand);
  1094. }
  1095. @Override
  1096. public void actionPerformed(ActionEvent actionEvent) {
  1097. // TODO Auto-generated method stub
  1098. System.out.println(actionEvent.getActionCommand() + " pressed");
  1099. switch(actionEvent.getActionCommand()) {
  1100. case "VK_UP":
  1101. System.out.println("increase");
  1102. if(displayOther < 0)displayOther++;
  1103. repaint();
  1104. break;
  1105. case "VK_DOWN":
  1106. System.out.println("decrease");
  1107. displayOther--;
  1108. repaint();
  1109. break;
  1110. default:
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. }