package holeg.ui.view.canvas; import holeg.model.*; import holeg.preferences.ColorPreference; import holeg.preferences.ImagePreference; import holeg.ui.model.GuiSettings; import holeg.ui.view.image.Import; import holeg.utility.math.vector.Vec2i; import java.awt.*; class Rendering { private static final RenderingHints RenderingHint = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); private static final Font CanvasFont = new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)); private static final Color[] colors = {ColorPreference.HolonObject.Producer, ColorPreference.HolonObject.NotSupplied, ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.Supplied, ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.NoEnergy}; private static final BasicStroke OnePixelStroke = new BasicStroke(1); private static final BasicStroke TwoPixelStroke = new BasicStroke(2); private static final Dimension SupplyBarDimensions = new Dimension(GuiSettings.getPictureScale(), GuiSettings.getPictureScale() / 5); private static final Font SupplyBarFont = new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() * 0.3) - 2); static Graphics2D initGraphics2D(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHints(RenderingHint); g2d.setFont(CanvasFont); return g2d; } static void drawSwitchObject(Graphics2D g, HolonSwitch hS) { drawCanvasObject(g, hS); } static void drawHolonObject(Graphics2D g, HolonObject hO) { Vec2i pos = hO.getPosition(); Color stateColor = ColorPreference.HolonObject.getStateColor(hO.getState()); g.setColor(stateColor); g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScale(), GuiSettings.getPictureScale()); drawCanvasObject(g, hO.getImage(), pos); if(GuiSettings.showSupplyBars && hO.getActualEnergy() <= 0){ drawSupplyBar(g, hO.getSupplyBarPercentage(), stateColor, pos); } } static void drawCanvasObject(Graphics2D g, AbstractCanvasObject obj) { drawCanvasObject(g, obj.getImage(), obj.getPosition()); } static void drawCanvasObject(Graphics2D g, String imageName, Vec2i pos) { int pictureScale = GuiSettings.getPictureScale(); int pictureScaleDiv2 = GuiSettings.getPictureScaleDiv2(); Image image = Import.loadImage(imageName, pictureScale, pictureScale); g.drawImage(image, pos.getX() - pictureScaleDiv2, pos.getY() - pictureScaleDiv2, pictureScale, pictureScale, null); } static void drawNode(Graphics2D g, Node node) { Vec2i pos = node.getPosition(); drawCanvasObject(g, ImagePreference.Canvas.Node.Unselected, pos); } static void drawEdge(Graphics2D g, Edge edge) { //both Vec2i start = edge.getA().getPosition(); Vec2i end = edge.getB().getPosition(); float currentEnergy = edge.getActualFlow(); float capacity = edge.maxCapacity; boolean unlimited = edge.mode == Edge.EdgeMode.Unlimited; switch (edge.getState()) { case Burned -> { g.setColor(ColorPreference.Edge.Burned); g.setStroke(TwoPixelStroke); } case Working -> { g.setColor(ColorPreference.Edge.Working); g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1)); } } g.drawLine(start.getX(), start.getY(), end.getX(), end.getY()); Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2); g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY()); } static void drawNewEdgeLine(Graphics2D g, Vec2i start, Vec2i end){ g.setStroke(TwoPixelStroke); g.setColor(ColorPreference.Edge.Working); g.drawLine(start.getX(), start.getY(), end.getX(), end.getY()); } static void drawGroupNode(Graphics2D g, GroupNode groupNode) { Vec2i pos = groupNode.getPosition(); g.setColor(Color.gray); g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScale(), GuiSettings.getPictureScale()); drawCanvasObject(g, groupNode.getImage(), pos); } static void drawSelection(Graphics2D g) { g.setStroke(OnePixelStroke); for (AbstractCanvasObject aCps : GuiSettings.getSelectedObjects()) { Vec2i pos = aCps.getPosition(); if (aCps instanceof Node) { g.setColor(ColorPreference.Canvas.ObjectSelectionFill); g.fillOval(pos.getX() - (GuiSettings.getPictureScaleDiv2()), pos.getY() - (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale()); g.setColor(ColorPreference.Canvas.ObjectSelectionBorder); g.drawOval(pos.getX() - (GuiSettings.getPictureScaleDiv2()), pos.getY() - (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale()); } else { g.setColor(ColorPreference.Canvas.ObjectSelectionFill); g.fillRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f)); g.setColor(ColorPreference.Canvas.ObjectSelectionBorder); g.drawRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f)); } } } static void drawSelectionBox(Graphics2D g, Rectangle selectionBox) { g.setStroke(OnePixelStroke); g.setColor(ColorPreference.Canvas.MouseSelectionBorder); g.draw(selectionBox); g.setColor(ColorPreference.Canvas.MouseSelectionFill); g.fill(selectionBox); } //protected void drawCanvasObjectString(Graphics2D g, Vec2i posOfCanvasObject, float energy) { // g.setColor(Color.BLACK); // g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() / 4f))); // g.drawString((energy > 0) ? "+" + Float.toString(energy) : Float.toString(energy), // posOfCanvasObject.getX() - GuiSettings.getPictureScaleDiv2(), // posOfCanvasObject.getY() - GuiSettings.getPictureScaleDiv2() - 1); //} // //protected void paintConsumer(Graphics2D g, Consumer con) { // paintCanvasObject(g, con); // drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork()); // if (GuiSettings.showSupplyBars) { // paintSupplyBar(g, con.getSupplyBarPercentage(), ColorPreference.HolonObject.getStateColor(con.getState()), // con.getModel().getPosition()); // } //} // //protected void paintSupplier(Graphics2D g, Supplier sup) { // paintCanvasObject(g, sup); // drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork()); //} // //protected void drawCanvasObject(Graphics2D g, String Image, Vec2i pos) { // g.drawImage(Import.loadImage(Image, GuiSettings.getPictureScale(), GuiSettings.getPictureScale()), // pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScale(), // GuiSettings.getPictureScale(), null); //} // //protected void paintCable(Graphics2D g, Edge cable, boolean isSelected) { // Vec2i start = cable.getA().getPosition(); // Vec2i end = cable.getB().getPosition(); // float currentEnergy = cable.getActualFlow(); // float capacity = cable.maxCapacity; // boolean unlimited = cable.isUnlimitedCapacity(); // switch (cable.getState()) { // case Burned: // g.setColor(ColorPreference.Edge.Burned); // g.setStroke(new BasicStroke(2)); // break; // case Working: // g.setColor(ColorPreference.Edge.Working); // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1)); // break; // } // if (isSelected) { // g.setColor(Color.lightGray); // } // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY()); // if (showConnectionInformation) { // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2); // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10))); // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY()); // } //} //protected void paintExitCable(Graphics2D g, ExitCable eCable) { // Vec2i start = eCable.getStart().getPosition(); // Vec2i end = eCable.getFinish().getPosition(); // float currentEnergy; // float capacity = eCable.getEdge().maxCapacity; // boolean unlimited = eCable.getEdge().isUnlimitedCapacity(); // if(eCable.getEdge().getState() == null) { // System.err.print(eCable.getEdge()); // } // switch (eCable.getEdge().getState()) { // case Burned: // currentEnergy = 0.0f; // g.setColor(Color.RED); // g.setStroke(new BasicStroke(2)); // break; // case Working: // default: // currentEnergy = eCable.getEdge().getActualFlow(); // g.setColor(new Color(13, 175, 28)); // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1)); // break; // } // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY()); // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2); // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10))); // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY()); //} //private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode, Vec2i pos) { // // +1, -2, -1 little Adjustment for pixel perfect alignment // int barWidth = (int) (GuiSettings.getPictureScale()); // int barHeight = (int) (GuiSettings.getPictureScale() / 5); // g.setColor(Color.WHITE); // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) barWidth, // barHeight); // float[] percentages = getGroupNodeBarPercentages(dGroupNode); // // for (int i = 5; i >= 0; i--) { // g.setColor(colors[i]); // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, // (int) (barWidth * percentages[i] - 1), barHeight); // } //// g.setColor(color); //// g.fillRect(pos.getX() - GuiSettings.GetPictureScaleDiv2(), pos.getY() + GuiSettings.GetPictureScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight); // g.setColor(Color.BLACK); // g.setStroke(new BasicStroke(1)); // g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, barWidth - 1, // barHeight); //} // ///** // * HardCoded Stuff dont try at Home ;) // * // * @param dGroupNode // * @return // */ //public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) { // int[] amountOfObjects = new int[6]; // amountOfObjects[0] = dGroupNode.getAmountOfSupplier(); // amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED); // amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED); // amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED); // amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED); // amountOfObjects[5] = dGroupNode.getAmountOfPassiv(); // int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] // + amountOfObjects[4] + amountOfObjects[5]; // float[] percentages = new float[6]; // int count = 0; // for (int i = 0; i < 6; i++) { // count += amountOfObjects[i]; // percentages[i] = (float) count / (float) countHolonObjects; // } // return percentages; //} private static void drawSupplyBar(Graphics2D g, float percentage, Color color, Vec2i pos) { // +1, -2, -1 little Adjustment for pixel perfect alignment g.setColor(Color.WHITE); g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, SupplyBarDimensions.width, SupplyBarDimensions.height); g.setColor(color); g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) (SupplyBarDimensions.width * (percentage < 1 ? percentage : 1.0f) - 1), SupplyBarDimensions.height); g.setColor(Color.BLACK); g.setStroke(new BasicStroke(1)); g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, SupplyBarDimensions.width - 1, SupplyBarDimensions.height); g.setFont(SupplyBarFont); String percentageString = (Math.round((percentage * 100))) + "%"; int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth(); if (percentage > 1.0f){ g.setColor(Color.WHITE); // Just to see better on purple } g.drawString(percentageString, pos.getX() + 1 - stringWidth / 2, pos.getY() + GuiSettings.getPictureScaleDiv2() - 1 + SupplyBarDimensions.height); } }