Rendering.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package holeg.ui.view.canvas;
  2. import holeg.model.*;
  3. import holeg.preferences.ColorPreference;
  4. import holeg.preferences.ImagePreference;
  5. import holeg.ui.model.GuiSettings;
  6. import holeg.ui.view.image.Import;
  7. import holeg.utility.math.vector.Vec2i;
  8. import java.awt.*;
  9. class Rendering {
  10. private static final RenderingHints RenderingHint = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  11. RenderingHints.VALUE_ANTIALIAS_ON);
  12. private static final Font CanvasFont = new Font("TimesNewRoman", Font.PLAIN,
  13. Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10));
  14. private static Color[] colors = {ColorPreference.HolonObject.Producer, ColorPreference.HolonObject.NotSupplied,
  15. ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.Supplied,
  16. ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.NoEnergy};
  17. private static final BasicStroke OnePixelStroke = new BasicStroke(1);
  18. private static final BasicStroke TwoPixelStroke = new BasicStroke(2);
  19. static Graphics2D initGraphics2D(Graphics g) {
  20. Graphics2D g2d = (Graphics2D) g;
  21. g2d.setRenderingHints(RenderingHint);
  22. g2d.setFont(CanvasFont);
  23. return g2d;
  24. }
  25. static void drawSwitchObject(Graphics2D g, HolonSwitch hS) {
  26. drawCanvasObject(g, hS);
  27. }
  28. static void drawHolonObject(Graphics2D g, HolonObject hO) {
  29. Vec2i pos = hO.getPosition();
  30. // Color statecolor = ColorPreference.HolonObject.getStateColor(hO.getState());
  31. // g.setColor(statecolor);
  32. g.setColor(Color.cyan);
  33. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  34. GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  35. drawCanvasObject(g, hO.getImage(), pos);
  36. }
  37. static void drawCanvasObject(Graphics2D g, AbstractCanvasObject obj) {
  38. drawCanvasObject(g, obj.getImage(), obj.getPosition());
  39. }
  40. static void drawCanvasObject(Graphics2D g, String imageName, Vec2i pos) {
  41. int pictureScale = GuiSettings.getPictureScale();
  42. int pictureScaleDiv2 = GuiSettings.getPictureScaleDiv2();
  43. Image image = Import.loadImage(imageName, pictureScale, pictureScale);
  44. g.drawImage(image, pos.getX() - pictureScaleDiv2, pos.getY() - pictureScaleDiv2, pictureScale, pictureScale,
  45. null);
  46. }
  47. static void drawNode(Graphics2D g, Node node) {
  48. Vec2i pos = node.getPosition();
  49. drawCanvasObject(g, ImagePreference.Canvas.Node.Unselected, pos);
  50. }
  51. static void drawEdge(Graphics2D g, Edge edge) {
  52. //both
  53. Vec2i start = edge.getA().getPosition();
  54. Vec2i end = edge.getB().getPosition();
  55. float currentEnergy = edge.getActualFlow();
  56. float capacity = edge.maxCapacity;
  57. boolean unlimited = edge.isUnlimitedCapacity();
  58. switch (edge.getState()) {
  59. case Burned -> {
  60. g.setColor(ColorPreference.Edge.Burned);
  61. g.setStroke(TwoPixelStroke);
  62. }
  63. case Working -> {
  64. g.setColor(ColorPreference.Edge.Working);
  65. g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  66. }
  67. }
  68. // if (isSelected) {
  69. // g.setColor(Color.lightGray);
  70. // }
  71. g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  72. Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  73. g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  74. }
  75. static void drawNewEdgeLine(Graphics2D g, Vec2i start, Vec2i end){
  76. g.setStroke(TwoPixelStroke);
  77. g.setColor(ColorPreference.Edge.Working);
  78. g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  79. }
  80. static void drawGroupNode(Graphics2D g, GroupNode groupNode) {
  81. Vec2i pos = groupNode.getPosition();
  82. g.setColor(Color.gray);
  83. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  84. GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  85. drawCanvasObject(g, groupNode.getImage(), pos);
  86. }
  87. static void drawSelection(Graphics2D g) {
  88. g.setStroke(OnePixelStroke);
  89. for (AbstractCanvasObject aCps : GuiSettings.getSelectedObjects()) {
  90. Vec2i pos = aCps.getPosition();
  91. if (aCps instanceof Node) {
  92. g.setColor(ColorPreference.Canvas.ObjectSelectionFill);
  93. g.fillOval(pos.getX() - (GuiSettings.getPictureScaleDiv2()),
  94. pos.getY() - (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  95. g.setColor(ColorPreference.Canvas.ObjectSelectionBorder);
  96. g.drawOval(pos.getX() - (GuiSettings.getPictureScaleDiv2()),
  97. pos.getY() - (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  98. } else {
  99. g.setColor(ColorPreference.Canvas.ObjectSelectionFill);
  100. g.fillRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  101. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  102. (int) (GuiSettings.getPictureScale() * 1.5f));
  103. g.setColor(ColorPreference.Canvas.ObjectSelectionBorder);
  104. g.drawRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  105. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  106. (int) (GuiSettings.getPictureScale() * 1.5f));
  107. }
  108. }
  109. }
  110. static void drawSelectionBox(Graphics2D g, Rectangle selectionBox) {
  111. g.setStroke(OnePixelStroke);
  112. g.setColor(ColorPreference.Canvas.MouseSelectionBorder);
  113. g.draw(selectionBox);
  114. g.setColor(ColorPreference.Canvas.MouseSelectionFill);
  115. g.fill(selectionBox);
  116. }
  117. //protected void drawCanvasObjectString(Graphics2D g, Vec2i posOfCanvasObject, float energy) {
  118. // g.setColor(Color.BLACK);
  119. // g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() / 4f)));
  120. // g.drawString((energy > 0) ? "+" + Float.toString(energy) : Float.toString(energy),
  121. // posOfCanvasObject.getX() - GuiSettings.getPictureScaleDiv2(),
  122. // posOfCanvasObject.getY() - GuiSettings.getPictureScaleDiv2() - 1);
  123. //}
  124. //
  125. //protected void paintConsumer(Graphics2D g, Consumer con) {
  126. // paintCanvasObject(g, con);
  127. // drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork());
  128. // if (GuiSettings.showSupplyBars) {
  129. // paintSupplyBar(g, con.getSupplyBarPercentage(), ColorPreference.HolonObject.getStateColor(con.getState()),
  130. // con.getModel().getPosition());
  131. // }
  132. //}
  133. //
  134. //protected void paintSupplier(Graphics2D g, Supplier sup) {
  135. // paintCanvasObject(g, sup);
  136. // drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork());
  137. //}
  138. //
  139. //protected void drawCanvasObject(Graphics2D g, String Image, Vec2i pos) {
  140. // g.drawImage(Import.loadImage(Image, GuiSettings.getPictureScale(), GuiSettings.getPictureScale()),
  141. // pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScale(),
  142. // GuiSettings.getPictureScale(), null);
  143. //}
  144. //
  145. //protected void paintCable(Graphics2D g, Edge cable, boolean isSelected) {
  146. // Vec2i start = cable.getA().getPosition();
  147. // Vec2i end = cable.getB().getPosition();
  148. // float currentEnergy = cable.getActualFlow();
  149. // float capacity = cable.maxCapacity;
  150. // boolean unlimited = cable.isUnlimitedCapacity();
  151. // switch (cable.getState()) {
  152. // case Burned:
  153. // g.setColor(ColorPreference.Edge.Burned);
  154. // g.setStroke(new BasicStroke(2));
  155. // break;
  156. // case Working:
  157. // g.setColor(ColorPreference.Edge.Working);
  158. // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  159. // break;
  160. // }
  161. // if (isSelected) {
  162. // g.setColor(Color.lightGray);
  163. // }
  164. // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  165. // if (showConnectionInformation) {
  166. // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  167. // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)));
  168. // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  169. // }
  170. //}
  171. //protected void paintExitCable(Graphics2D g, ExitCable eCable) {
  172. // Vec2i start = eCable.getStart().getPosition();
  173. // Vec2i end = eCable.getFinish().getPosition();
  174. // float currentEnergy;
  175. // float capacity = eCable.getEdge().maxCapacity;
  176. // boolean unlimited = eCable.getEdge().isUnlimitedCapacity();
  177. // if(eCable.getEdge().getState() == null) {
  178. // System.err.print(eCable.getEdge());
  179. // }
  180. // switch (eCable.getEdge().getState()) {
  181. // case Burned:
  182. // currentEnergy = 0.0f;
  183. // g.setColor(Color.RED);
  184. // g.setStroke(new BasicStroke(2));
  185. // break;
  186. // case Working:
  187. // default:
  188. // currentEnergy = eCable.getEdge().getActualFlow();
  189. // g.setColor(new Color(13, 175, 28));
  190. // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  191. // break;
  192. // }
  193. // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  194. // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  195. // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)));
  196. // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  197. //}
  198. //private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode, Vec2i pos) {
  199. // // +1, -2, -1 little Adjustment for pixel perfect alignment
  200. // int barWidth = (int) (GuiSettings.getPictureScale());
  201. // int barHeight = (int) (GuiSettings.getPictureScale() / 5);
  202. // g.setColor(Color.WHITE);
  203. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) barWidth,
  204. // barHeight);
  205. // float[] percentages = getGroupNodeBarPercentages(dGroupNode);
  206. //
  207. // for (int i = 5; i >= 0; i--) {
  208. // g.setColor(colors[i]);
  209. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
  210. // (int) (barWidth * percentages[i] - 1), barHeight);
  211. // }
  212. //// g.setColor(color);
  213. //// g.fillRect(pos.getX() - GuiSettings.GetPictureScaleDiv2(), pos.getY() + GuiSettings.GetPictureScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  214. // g.setColor(Color.BLACK);
  215. // g.setStroke(new BasicStroke(1));
  216. // g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, barWidth - 1,
  217. // barHeight);
  218. //}
  219. //
  220. ///**
  221. // * HardCoded Stuff dont try at Home ;)
  222. // *
  223. // * @param dGroupNode
  224. // * @return
  225. // */
  226. //public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
  227. // int[] amountOfObjects = new int[6];
  228. // amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
  229. // amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  230. // amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  231. // amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  232. // amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  233. // amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
  234. // int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3]
  235. // + amountOfObjects[4] + amountOfObjects[5];
  236. // float[] percentages = new float[6];
  237. // int count = 0;
  238. // for (int i = 0; i < 6; i++) {
  239. // count += amountOfObjects[i];
  240. // percentages[i] = (float) count / (float) countHolonObjects;
  241. // }
  242. // return percentages;
  243. //}
  244. //private void paintSupplyBar(Graphics2D g, float percentage, Color color, Vec2i pos) {
  245. // // +1, -2, -1 little Adjustment for pixel perfect alignment
  246. // int barWidth = (int) (GuiSettings.getPictureScale());
  247. // int barHeight = (int) (GuiSettings.getPictureScale() / 5);
  248. // g.setColor(Color.WHITE);
  249. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) barWidth,
  250. // barHeight);
  251. // g.setColor(color);
  252. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
  253. // (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  254. // g.setColor(Color.BLACK);
  255. // g.setStroke(new BasicStroke(1));
  256. // g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, barWidth - 1,
  257. // barHeight);
  258. // g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  259. // String percentageString = (Math.round((percentage * 100))) + "%";
  260. // int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  261. // if (percentage > 1.0f)
  262. // g.setColor(Color.WHITE); // Just to see better on purple
  263. // g.drawString(percentageString, pos.getX() + 1 - stringWidth / 2,
  264. // pos.getY() + GuiSettings.getPictureScaleDiv2() - 1 + barHeight);
  265. //
  266. //}
  267. }