Canvas.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package holeg.ui.view.canvas;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.util.logging.Logger;
  6. import javax.swing.JPanel;
  7. import holeg.model.GroupNode;
  8. import holeg.model.HolonObject;
  9. import holeg.preferences.ColorPreference;
  10. import holeg.ui.controller.Control;
  11. import holeg.ui.model.GuiSettings;
  12. import holeg.utility.image.Import;
  13. import holeg.utility.math.vector.Vec2i;
  14. import java.awt.Graphics2D;
  15. import java.awt.Image;
  16. import java.awt.RenderingHints;
  17. /**
  18. * This Class is the Canvas. All Objects will be visualized here
  19. *
  20. * @author Gruppe14
  21. */
  22. public class Canvas extends JPanel {
  23. private static final Logger log = Logger.getLogger(Canvas.class.getName());
  24. private boolean enabled = false;
  25. private GroupNode groupNode;
  26. private Control control;
  27. private static Color[] colors = { ColorPreference.HolonObject.Producer, ColorPreference.HolonObject.NotSupplied,
  28. ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.Supplied,
  29. ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.NoEnergy };
  30. public Canvas(Control control, GroupNode groupNode) {
  31. this.control = control;
  32. this.groupNode = groupNode;
  33. control.OnGuiSetEnabled.addListener(this::setCanvasEnabled);
  34. // TODO(Tom2022-01-14): remove listener when not needed anymore
  35. }
  36. private void setCanvasEnabled(boolean state) {
  37. enabled = state;
  38. }
  39. // protected void paintCanvasObject(Graphics2D g, DecoratedHolonObject decoratedHolonObject) {
  40. // Vec2i pos = decoratedHolonObject.getModel().getPosition();
  41. // Color statecolor = ColorPreference.HolonObject.getStateColor(decoratedHolonObject.getState());
  42. // g.setColor(statecolor);
  43. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  44. // GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  45. // drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
  46. // }
  47. //
  48. // protected void drawCanvasObjectString(Graphics2D g, Vec2i posOfCanvasObject, float energy) {
  49. // g.setColor(Color.BLACK);
  50. // g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() / 4f)));
  51. // g.drawString((energy > 0) ? "+" + Float.toString(energy) : Float.toString(energy),
  52. // posOfCanvasObject.getX() - GuiSettings.getPictureScaleDiv2(),
  53. // posOfCanvasObject.getY() - GuiSettings.getPictureScaleDiv2() - 1);
  54. // }
  55. //
  56. // protected void paintConsumer(Graphics2D g, Consumer con) {
  57. // paintCanvasObject(g, con);
  58. // drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork());
  59. // if (GuiSettings.showSupplyBars) {
  60. // paintSupplyBar(g, con.getSupplyBarPercentage(), ColorPreference.HolonObject.getStateColor(con.getState()),
  61. // con.getModel().getPosition());
  62. // }
  63. // }
  64. //
  65. // protected void paintSupplier(Graphics2D g, Supplier sup) {
  66. // paintCanvasObject(g, sup);
  67. // drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork());
  68. // }
  69. //
  70. // protected void drawCanvasObject(Graphics2D g, String Image, Vec2i pos) {
  71. // g.drawImage(Import.loadImage(Image, GuiSettings.getPictureScale(), GuiSettings.getPictureScale()),
  72. // pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScale(),
  73. // GuiSettings.getPictureScale(), null);
  74. // }
  75. //
  76. // protected void paintCable(Graphics2D g, Edge cable, boolean isSelected) {
  77. // Vec2i start = cable.getA().getPosition();
  78. // Vec2i end = cable.getB().getPosition();
  79. // float currentEnergy = cable.getActualFlow();
  80. // float capacity = cable.maxCapacity;
  81. // boolean unlimited = cable.isUnlimitedCapacity();
  82. // switch (cable.getState()) {
  83. // case Burned:
  84. // g.setColor(ColorPreference.Edge.Burned);
  85. // g.setStroke(new BasicStroke(2));
  86. // break;
  87. // case Working:
  88. // g.setColor(ColorPreference.Edge.Working);
  89. // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  90. // break;
  91. // }
  92. // if (isSelected) {
  93. // g.setColor(Color.lightGray);
  94. // }
  95. // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  96. // if (showConnectionInformation) {
  97. // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  98. // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)));
  99. // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  100. // }
  101. // }
  102. //
  103. // protected void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch) {
  104. // drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage()
  105. // : HolonSwitch.getSwitchClosedImage(), dSwitch.getModel().getPosition());
  106. // }
  107. //
  108. // protected void paintExitCable(Graphics2D g, ExitCable eCable) {
  109. // Vec2i start = eCable.getStart().getPosition();
  110. // Vec2i end = eCable.getFinish().getPosition();
  111. // float currentEnergy;
  112. // float capacity = eCable.getEdge().maxCapacity;
  113. // boolean unlimited = eCable.getEdge().isUnlimitedCapacity();
  114. // if(eCable.getEdge().getState() == null) {
  115. // System.err.print(eCable.getEdge());
  116. // }
  117. // switch (eCable.getEdge().getState()) {
  118. // case Burned:
  119. // currentEnergy = 0.0f;
  120. // g.setColor(Color.RED);
  121. // g.setStroke(new BasicStroke(2));
  122. // break;
  123. // case Working:
  124. // default:
  125. // currentEnergy = eCable.getEdge().getActualFlow();
  126. // g.setColor(new Color(13, 175, 28));
  127. // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  128. // break;
  129. // }
  130. // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  131. // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  132. // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)));
  133. // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  134. // }
  135. //
  136. // protected void paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode) {
  137. // Vec2i pos = dGroupNode.getModel().getPosition();
  138. // g.setColor(Color.lightGray);
  139. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  140. // GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  141. // drawCanvasObject(g, ImagePreference.Canvas.GroupNode, pos);
  142. // paintGroupNodeBar(g, dGroupNode, pos);
  143. // }
  144. //
  145. // private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode, Vec2i pos) {
  146. // // +1, -2, -1 little Adjustment for pixel perfect alignment
  147. // int barWidth = (int) (GuiSettings.getPictureScale());
  148. // int barHeight = (int) (GuiSettings.getPictureScale() / 5);
  149. // g.setColor(Color.WHITE);
  150. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) barWidth,
  151. // barHeight);
  152. // float[] percentages = getGroupNodeBarPercentages(dGroupNode);
  153. //
  154. // for (int i = 5; i >= 0; i--) {
  155. // g.setColor(colors[i]);
  156. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
  157. // (int) (barWidth * percentages[i] - 1), barHeight);
  158. // }
  159. //// g.setColor(color);
  160. //// g.fillRect(pos.getX() - GuiSettings.GetPictureScaleDiv2(), pos.getY() + GuiSettings.GetPictureScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  161. // g.setColor(Color.BLACK);
  162. // g.setStroke(new BasicStroke(1));
  163. // g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, barWidth - 1,
  164. // barHeight);
  165. // }
  166. //
  167. // /**
  168. // * HardCoded Stuff dont try at Home ;)
  169. // *
  170. // * @param dGroupNode
  171. // * @return
  172. // */
  173. // public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
  174. // int[] amountOfObjects = new int[6];
  175. // amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
  176. // amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  177. // amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  178. // amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  179. // amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  180. // amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
  181. // int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3]
  182. // + amountOfObjects[4] + amountOfObjects[5];
  183. // float[] percentages = new float[6];
  184. // int count = 0;
  185. // for (int i = 0; i < 6; i++) {
  186. // count += amountOfObjects[i];
  187. // percentages[i] = (float) count / (float) countHolonObjects;
  188. // }
  189. // return percentages;
  190. // }
  191. // private void paintSupplyBar(Graphics2D g, float percentage, Color color, Vec2i pos) {
  192. // // +1, -2, -1 little Adjustment for pixel perfect alignment
  193. // int barWidth = (int) (GuiSettings.getPictureScale());
  194. // int barHeight = (int) (GuiSettings.getPictureScale() / 5);
  195. // g.setColor(Color.WHITE);
  196. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) barWidth,
  197. // barHeight);
  198. // g.setColor(color);
  199. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
  200. // (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  201. // g.setColor(Color.BLACK);
  202. // g.setStroke(new BasicStroke(1));
  203. // g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, barWidth - 1,
  204. // barHeight);
  205. // g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  206. // String percentageString = (Math.round((percentage * 100))) + "%";
  207. // int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  208. // if (percentage > 1.0f)
  209. // g.setColor(Color.WHITE); // Just to see better on purple
  210. // g.drawString(percentageString, pos.getX() + 1 - stringWidth / 2,
  211. // pos.getY() + GuiSettings.getPictureScaleDiv2() - 1 + barHeight);
  212. //
  213. // }
  214. @Override
  215. public void paintComponent(Graphics g) {
  216. super.paintComponent(g);
  217. Graphics2D g2d = initGraphics2D(g);
  218. log.info("Draw");
  219. groupNode.getHolonObjects().forEach(hO -> {
  220. drawHolonObject(g2d, hO);
  221. });
  222. //
  223. // Optional<VisualRepresentationalState> optVisualState = control.getSimManager().getActualVisualRepresentationalState();
  224. // // VisualState Representation:
  225. // if (optVisualState.isEmpty()) {
  226. // return;
  227. // }
  228. // VisualRepresentationalState visualState = optVisualState.get();
  229. //
  230. // for (ExitCable cable : visualState.getExitCableList()) {
  231. // paintExitCable(g2d, cable);
  232. // }
  233. // for (Edge cable : visualState.getCableList()) {
  234. // paintCable(g2d, cable, GuiSettings.getSelectedEdges().contains(cable));
  235. // }
  236. // for (DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
  237. // paintGroupNode(g2d, dGroupNode);
  238. // }
  239. // log.info(visualState.getConsumerList().stream().map(Object::toString).collect(Collectors.joining(", ")));
  240. // for (Consumer con : visualState.getConsumerList()) {
  241. // paintConsumer(g2d, con);
  242. // }
  243. // log.info(visualState.getSupplierList().stream().map(Object::toString).collect(Collectors.joining(", ")));
  244. // for (Supplier sup : visualState.getSupplierList()) {
  245. // paintSupplier(g2d, sup);
  246. // }
  247. // for (Passiv pas : visualState.getPassivList()) {
  248. // paintCanvasObject(g2d, pas);
  249. // }
  250. // for (DecoratedSwitch dSwitch : visualState.getSwitchList()) {
  251. // paintSwitch(g2d, dSwitch);
  252. // }
  253. // for (Node node : visualState.getNodeList()) {
  254. // drawCanvasObject(g2d, ImagePreference.Canvas.Node.Unselected, node.getPosition());
  255. // }
  256. //
  257. // // -->oldCode
  258. // if (doMark) {
  259. // g2d.setColor(Color.BLACK);
  260. // g2d.setStroke(new BasicStroke(0));
  261. // drawMarker(g2d);
  262. // }
  263. // // Test Selection
  264. // // Objects:
  265. // g2d.setColor(Color.BLUE);
  266. // g2d.setStroke(new BasicStroke(1));
  267. // Color transparentGrey = ColorPreference.Panel.ObjectSelection;
  268. // for (AbstractCanvasObject aCps : GuiSettings.getSelectedObjects()) {
  269. // if (aCps instanceof Node) {
  270. // Vec2i pos = aCps.getPosition();
  271. // g2d.setColor(transparentGrey);
  272. // g2d.fillOval(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2()),
  273. // pos.getY() - (int) (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  274. // g2d.setColor(Color.LIGHT_GRAY);
  275. // g2d.setStroke(new BasicStroke(2));
  276. // g2d.drawOval(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2()),
  277. // pos.getY() - (int) (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  278. // } else {
  279. // Vec2i pos = aCps.getPosition();
  280. // g2d.setColor(transparentGrey);
  281. // g2d.fillRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  282. // pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  283. // (int) (GuiSettings.getPictureScale() * 1.5f));
  284. // g2d.setColor(Color.LIGHT_GRAY);
  285. // g2d.setStroke(new BasicStroke(2));
  286. // g2d.drawRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  287. // pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  288. // (int) (GuiSettings.getPictureScale() * 1.5f));
  289. // }
  290. //
  291. // }
  292. // // maybeReplace:
  293. // if (mayBeReplaced != null) {
  294. // g2d.setColor(Color.RED);
  295. // Vec2i pos = mayBeReplaced.getPosition();
  296. // g2d.drawImage(Import.loadImage(ImagePreference.Canvas.ReplaceSymbol), pos.getX() + GuiSettings.getPictureScaleDiv2(),
  297. // pos.getY() - GuiSettings.getPictureScale(), GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScaleDiv2(), null);
  298. // }
  299. // // <-- OldCode
  300. }
  301. private void drawHolonObject(Graphics2D g, HolonObject hO) {
  302. Vec2i pos = hO.getPosition();
  303. // Color statecolor = ColorPreference.HolonObject.getStateColor(hO.getState());
  304. // g.setColor(statecolor);
  305. g.setColor(Color.cyan);
  306. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  307. GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  308. drawCanvasObject(g, hO.getImage(), pos);
  309. }
  310. private void drawCanvasObject(Graphics2D g, String imageName, Vec2i pos) {
  311. int pictureScale = GuiSettings.getPictureScale();
  312. int pictureScaleDiv2 = GuiSettings.getPictureScaleDiv2();
  313. Image image = Import.loadImage(imageName, pictureScale, pictureScale);
  314. g.drawImage(image, pos.getX() - pictureScaleDiv2, pos.getY() - pictureScaleDiv2, pictureScale, pictureScale,
  315. null);
  316. }
  317. private static final RenderingHints RenderingHint = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  318. RenderingHints.VALUE_ANTIALIAS_ON);
  319. private static final Font CanvasFont = new Font("TimesNewRoman", Font.PLAIN,
  320. Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10));
  321. private static Graphics2D initGraphics2D(Graphics g) {
  322. Graphics2D g2d = (Graphics2D) g;
  323. g2d.setRenderingHints(RenderingHint);
  324. g2d.setFont(CanvasFont);
  325. return g2d;
  326. }
  327. public GroupNode getGroupNode() {
  328. return this.groupNode;
  329. }
  330. public void setGroupNode(GroupNode groupNode) {
  331. this.groupNode = groupNode;
  332. }
  333. public void checkForReplacement(int x, int y) {
  334. }
  335. }