Example usage for java.awt Graphics2D setColor

List of usage examples for java.awt Graphics2D setColor

Introduction

In this page you can find the example usage for java.awt Graphics2D setColor.

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:com.joey.software.regionSelectionToolkit.controlers.ImageProfileTool.java

@Override
public void draw(Graphics2D g) {
    if (isBlockUpdate()) {
        return;/*  www  .j a  v a2 s .  c om*/
    }
    updateSelectionData();

    float tra = (transparance.getValue() / (float) (transparance.getMaximum()));

    float lineSize = 1f / (float) panel.getScale();
    float crossSize = (float) (pointSize / panel.getScale());

    RenderingHints oldHinds = g.getRenderingHints();
    Composite oldcomp = g.getComposite();
    Stroke oldStroke = g.getStroke();
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tra));
    g.setStroke(new BasicStroke(lineSize));
    GraphicsToolkit.setRenderingQuality(g, GraphicsToolkit.HIGH_QUALITY);
    Line2D.Double line = new Line2D.Double();

    double x1 = 0;
    double x2 = 0;
    double y1 = 0;
    double y2 = 0;
    for (int i = 0; i < selectionData.length - 1; i++) {
        if (axis == AXIS_X) {
            y1 = view.getImage().getHeight() / (double) (selectionData.length - 1) * i;
            x1 = view.getImage().getWidth() * (1 - getSelectionValue(i));

            y2 = view.getImage().getHeight() / (double) (selectionData.length - 1) * (i + 1);
            x2 = view.getImage().getWidth() * (1 - getSelectionValue(i + 1));
        } else if (axis == AXIS_Y) {
            x1 = view.getImage().getWidth() / (double) (selectionData.length - 1) * i;
            y1 = view.getImage().getHeight() * (1 - getSelectionValue(i));

            x2 = view.getImage().getWidth() / (double) (selectionData.length - 1) * (i + 1);
            y2 = view.getImage().getHeight() * (1 - getSelectionValue(i + 1));
        }

        if (getUseData(i)) {
            g.setColor(getPointColorSelected());
        } else {
            g.setColor(getPointColorNotSelected());
        }
        line.x1 = x1;
        line.x2 = x2;
        line.y1 = y1;
        line.y2 = y2;

        g.draw(line);

        if (showOffset.isSelected()) {
            if (getUseData(i)) {
                g.setColor(getOffsetColor());
                double offset = (Double) this.offset.getValue();

                if (axis == AXIS_X) {
                    line.x1 += offset;
                    line.x2 += offset;

                } else if (axis == AXIS_Y) {
                    line.y1 += offset;
                    line.y2 += offset;
                }
                g.draw(line);
            }
        }
    }

    // Draw each point
    if (drawCrosses) {
        double x = 0;
        double y = 0;
        for (int i = 0; i < value.length; i++) {

            if (axis == AXIS_X) {
                y = view.getImage().getHeight() / (double) (value.length - 1) * i;
                x = view.getImage().getWidth() * (1 - value[i]);
            } else if (axis == AXIS_Y) {
                x = view.getImage().getWidth() / (double) (value.length - 1) * i;
                y = view.getImage().getHeight() * (1 - value[i]);
            }
            DrawTools.drawCross(g, new Point2D.Double(x, y), crossSize, 0, getCrossColor(), lineSize);

        }
    }
    g.setComposite(oldcomp);
    g.setStroke(oldStroke);
    g.setRenderingHints(oldHinds);
}

From source file:com.flexoodb.common.FlexUtils.java

static public void createTextBanner(String text, int fontsize, int width, int height, int x, int y,
        String outputfile) throws Exception {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();/*from  w  ww  .  j  a  v a 2  s.  c o  m*/
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Arial", Font.BOLD, fontsize);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(Color.BLUE.darker());
    g.drawString(text, x, y);
    ImageIO.write(img, "png", new File(outputfile));
}

From source file:com.flexoodb.common.FlexUtils.java

static public BufferedImage createCaptcha(String text, Color background, Color fontcolor, int fontsize,
        int width, int height, int x, int y) throws Exception {
    BufferedImage img = new BufferedImage(width + 10, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();// w  w  w. ja va 2  s .  co m
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(background);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Monospaced", Font.BOLD + Font.ITALIC, fontsize);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(fontcolor);

    char[] c = text.toCharArray();

    java.util.Random random = new java.util.Random();

    for (int i = 0; i < c.length; i++) {
        int j = random.nextInt(9);
        if (j > 5) {
            j = j - 5;
        } else {
            j = -1 * (j - 2);
        }

        char[] c1 = new char[1];
        c1[0] = c[i];
        g.drawChars(c1, 0, 1, x + (i * 16), y + j);
    }

    for (int i = -10; i < 20; i++) {
        int j = random.nextInt(8);
        g.drawOval((i + j) - 20, ((i * 8) - (j > 3 ? 4 : -4)), width + 20, height);
    }
    //ImageIO.write(img,"png",new File(outputfile));
    return img;

}

From source file:org.gumtree.vis.plot1d.Plot1DPanel.java

@Override
protected void drawToolTipFollower(Graphics2D g2, int x, int y) {
    Rectangle2D dataArea = getScreenDataArea();
    if (((int) dataArea.getMinX() <= x) && (x <= (int) dataArea.getMaxX()) && ((int) dataArea.getMinY() <= y)
            && (y <= (int) dataArea.getMaxY())) {
        String text = "";
        if (indexInTooltip) {
            text += "#" + getItemIndex() + ", ";
        }//  ww  w . ja va  2 s. c  o  m
        text += String.format("(%." + mouseFollowerXPrecision + "f, %." + mouseFollowerYPrecision + "f",
                getChartX(), getChartY());
        boolean isErrorEnabled = false;
        XYItemRenderer renderer = getXYPlot().getRenderer();
        if (renderer instanceof XYErrorRenderer) {
            isErrorEnabled = ((XYErrorRenderer) renderer).getDrawYError();
        }
        if (isErrorEnabled) {
            text += String.format(" \u00B1%." + mouseFollowerYPrecision + "f", getChartError());
        }
        text += ")";
        int xLoc = x + 10;
        int yLoc = y + 20;
        double width = text.length() * 5.5;
        double height = 15;
        if (xLoc + width > dataArea.getMaxX()) {
            xLoc = (int) (x - width);
        }
        if (yLoc + height > dataArea.getMaxY()) {
            yLoc = (int) (y - height);
        }

        Rectangle2D toolTipArea = new Rectangle2D.Double(xLoc, yLoc, width, height);

        g2.setColor(Color.lightGray);
        g2.fill(toolTipArea);
        g2.setColor(Color.black);
        g2.drawString(text, xLoc + 3, yLoc + 11);
    }
}

From source file:com.alibaba.simpleimage.render.FixDrawTextItem.java

@Override
public void drawText(Graphics2D graphics, int width, int height) {
    if (StringUtils.isBlank(text)) {
        return;//from   ww w.  j  a  va 2  s  . c  o m
    }

    int x = 0, y = 0;
    int fontsize = 1;
    if (position == Position.CENTER) {
        // ?
        int textLength = (int) (width * textWidthPercent);
        // ??
        fontsize = textLength / text.length();
        // ?.....?
        if (fontsize < minFontSize) {
            return;
        }

        float fsize = (float) fontsize;
        Font font = defaultFont.deriveFont(fsize);
        graphics.setFont(font);
        FontRenderContext context = graphics.getFontRenderContext();
        int sw = (int) font.getStringBounds(text, context).getWidth();

        // ??
        x = (width - sw) / 2;
        y = height / 2 + fontsize / 2;
    } else if (position == Position.TOP_LEFT) {
        fontsize = ((int) (width * textWidthPercent)) / text.length();
        if (fontsize < minFontSize) {
            return;
        }

        float fsize = (float) fontsize;
        Font font = defaultFont.deriveFont(fsize);
        graphics.setFont(font);

        x = fontsize;
        y = fontsize * 2;
    } else if (position == Position.TOP_RIGHT) {
        fontsize = ((int) (width * textWidthPercent)) / text.length();
        if (fontsize < minFontSize) {
            return;
        }

        float fsize = (float) fontsize;
        Font font = defaultFont.deriveFont(fsize);
        graphics.setFont(font);
        FontRenderContext context = graphics.getFontRenderContext();
        int sw = (int) font.getStringBounds(text, context).getWidth();

        x = width - sw - fontsize;
        y = fontsize * 2;
    } else if (position == Position.BOTTOM_LEFT) {
        fontsize = ((int) (width * textWidthPercent)) / text.length();
        if (fontsize < minFontSize) {
            return;
        }

        float fsize = (float) fontsize;
        Font font = defaultFont.deriveFont(fsize);
        graphics.setFont(font);

        x = fontsize / 2;
        y = height - fontsize;
    } else if (position == Position.BOTTOM_RIGHT) {
        fontsize = ((int) (width * textWidthPercent)) / text.length();
        if (fontsize < minFontSize) {
            return;
        }

        float fsize = (float) fontsize;
        Font font = defaultFont.deriveFont(fsize);
        graphics.setFont(font);
        FontRenderContext context = graphics.getFontRenderContext();
        int sw = (int) font.getStringBounds(text, context).getWidth();

        x = width - sw - fontsize;
        y = height - fontsize;
    } else {
        throw new IllegalArgumentException("Unknown position : " + position);
    }

    if (x <= 0 || y <= 0) {
        return;
    }

    if (fontShadowColor != null) {
        graphics.setColor(fontShadowColor);
        graphics.drawString(text, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize));
    }

    graphics.setColor(fontColor);
    graphics.drawString(text, x, y);
}

From source file:net.sf.maltcms.chromaui.annotations.PeakAnnotationRenderer.java

public void draw(Graphics2D g2, ChartPanel chartPanel, XYPlot plot, Color fillColor,
        Collection<? extends VisualPeakAnnotation> shapes,
        Collection<? extends VisualPeakAnnotation> selectedShapes) {
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Shape savedClip = g2.getClip();
    Rectangle2D dataArea = chartPanel.getScreenDataArea();
    g2.clip(dataArea);// w  w  w  .j  av  a2  s .  co  m

    ValueAxis xAxis = plot.getDomainAxis();
    RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
    ValueAxis yAxis = plot.getRangeAxis();
    RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
    Color c = g2.getColor();
    //         Color fillColor = peakAnnotations.getColor();
    //         if (fillColor == null || fillColor.equals(Color.WHITE) || fillColor.equals(new Color(255, 255, 255, 0))) {
    ////            System.out.println("Peak annotation color was null or white, using color from treatment group!");
    //            fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
    //         }
    for (VisualPeakAnnotation x : shapes) {
        Shape s = toViewXY(x, chartPanel, x.getCenter());
        switch (x.getPeakAnnotationType()) {
        case LINE:
            drawEntity(s, g2, fillColor, null, chartPanel, false, 0.1f);
            break;
        case OUTLINE:
            //                  plot.addAnnotation(new XYShapeAnnotation(x, new BasicStroke(1.0f), new Color(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(), 64), new Color(254, 254, 254, 254)), false);
            drawOutline(s, g2, fillColor, Color.DARK_GRAY, chartPanel, false, 0.25f);
            break;
        case POINTER:
            drawEntity(s, g2, fillColor, Color.DARK_GRAY, chartPanel, false, 0.25f);
            break;
        default:
            drawEntity(s, g2, fillColor, null, chartPanel, false, 0.1f);
        }
    }
    for (VisualPeakAnnotation x : selectedShapes) {
        Shape s = toViewXY(x, chartPanel, x.getCenter());
        switch (x.getPeakAnnotationType()) {
        case LINE:
            drawEntity(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
            break;
        case OUTLINE:
            //                  plot.addAnnotation(new XYShapeAnnotation(x, new BasicStroke(1.0f), new Color(fillColor.getRed(),fillColor.getGreen(),fillColor.getBlue(),64), new Color(254,254,254,254)), false);
            drawOutline(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
            break;
        case POINTER:
            drawEntity(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
            break;
        default:
            drawEntity(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
        }
    }
    g2.setColor(c);
    g2.setClip(savedClip);
    //         chartPanel.repaint();
}

From source file:edu.harvard.iq.dvn.core.web.ExploreDataPage.java

private BufferedImage getCompositeImage(BufferedImage image) throws FontFormatException, IOException {
    Integer heightAdjustment = new Integer(0);
    if (this.heightInt == 1) {
        heightAdjustment = 40;//from   w w w  .  j  a v a2s  . c  om
    }
    if (this.heightInt == 3) {
        heightAdjustment = -100;
    }

    BufferedImage yAxisImage = new BufferedImage(100, 500, BufferedImage.TYPE_INT_ARGB);
    BufferedImage combinedImage = new BufferedImage(776, 575 + heightAdjustment, BufferedImage.TYPE_INT_ARGB);

    if (graphTitle.trim().isEmpty()) {
        graphTitle = " ";
    }

    File retFile = generateImageString("16", "620x", "South", "0", graphTitle);
    BufferedImage titleImage = ImageIO.read(retFile);
    if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) {
        if (retFile != null && retFile.exists()) {
            retFile.delete();
        }
    }

    String source = "";

    if (!sourceString.trim().isEmpty()) {
        source = sourceString;
    }

    if (source.trim().isEmpty()) {
        source = " ";
    }
    retFile = generateImageString("14", "676x", "NorthWest", "0", source);
    BufferedImage sourceImage = ImageIO.read(retFile);
    if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) {
        if (retFile != null && retFile.exists()) {
            retFile.delete();
        }
    }

    if (yAxisLabel.trim().isEmpty()) {
        yAxisLabel = " ";
    }
    retFile = generateImageString("14", new Integer(350 + heightAdjustment) + "x", "South", "-90", yAxisLabel);
    BufferedImage yAxisVertImage = ImageIO.read(retFile);
    if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) {
        if (retFile != null && retFile.exists()) {
            retFile.delete();
        }
    }

    Graphics2D yag2 = yAxisImage.createGraphics();
    Graphics2D cig2 = combinedImage.createGraphics();
    Graphics2D sig2 = sourceImage.createGraphics();

    cig2.setColor(Color.WHITE);
    yag2.setColor(Color.WHITE);
    yag2.fillRect(0, 0, 676, 500);
    cig2.fillRect(0, 0, 776, 550);

    cig2.drawImage(yAxisImage, 0, 0, null);
    cig2.drawImage(yAxisVertImage, 15, 50, null);
    cig2.drawImage(image, 50, 50, null);
    cig2.drawImage(titleImage, 85, 15, null);
    cig2.drawImage(sourceImage, 45, 475 + heightAdjustment, null);

    yag2.dispose();
    sig2.dispose();
    cig2.dispose();

    return combinedImage;
}

From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java

private void createNodeImage(long id, Map<Long, Integer> channelMap, Map<Long, Integer> colorMap, int x, int y,
        Graphics2D g2) {
    Integer channel = channelMap.get(id);
    Integer colorIndex = colorMap.get(id);
    int radius = 11;
    if (channel == null || channel == 0 || colorIndex == null || colorIndex > startChannelColors.length - 1) {
        g2.setColor(new Color(180, 180, 180));
        g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
        return;// ww  w  .  j  a v  a 2  s. c om
    }
    g2.setColor(startChannelColors[colorIndex]);
    g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    int dy = 6, w = 21;
    g2.fillRect(x, y - dy, w, dy + dy);
    dy = 5;
    g2.setColor(new Color(255, 255, 255));
    g2.fillRect(x + 1, y - dy, w - 2, dy + dy);
    g2.setColor(new Color(0, 51, 102));
    int chx = x + 2;
    if (channel < 100) {
        chx += 3;
    }
    if (channel < 10) {
        chx += 3;
    }
    g2.drawString("" + channel, chx, y + 4);
}

From source file:net.sf.firemox.clickable.target.card.VirtualCard.java

@SuppressWarnings("null")
@Override/*from   ww  w .  ja va  2s .c  o  m*/
public void paintComponent(Graphics g) {
    final Graphics2D g2D = (Graphics2D) g;

    // Renderer
    g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // Optimization card painting
    final MZone container = card.getContainer();
    final boolean shortPaint = container == null || !container.isMustBePainted(card);

    // tap/reverse operation : PI/2, PI, -PI/2 rotation
    if (!shortPaint) {
        if (container.isMustBePaintedReversed(card)) {
            if (card.tapped) {
                g2D.translate(rotateTransformY, CardFactory.cardWidth + rotateTransformX);
                g2D.rotate(angle - Math.PI / 2);
            } else {
                g2D.translate(CardFactory.cardWidth + rotateTransformX,
                        CardFactory.cardHeight + rotateTransformY);
                g2D.rotate(Math.PI - angle);
            }
        } else {
            if (card.tapped) {
                g2D.translate(CardFactory.cardHeight + rotateTransformY, rotateTransformX);
                g2D.rotate(Math.PI / 2 + angle);
            } else {
                g2D.translate(rotateTransformX, rotateTransformY);
                g2D.rotate(angle);
            }
        }
    }

    if (container != null) {
        if (container.isVisibleForOpponent() && !card.isVisibleForOpponent() && card.isVisibleForYou()) {
            /*
             * This card is visible for you but not for opponent in despite of the
             * fact the container is public.
             */
            g2D.drawImage(card.scaledImage(), null, null);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
            g2D.drawImage(DatabaseFactory.scaledBackImage, null, null);
        } else if (!card.isVisibleForYou()) {
            g2D.drawImage(DatabaseFactory.scaledBackImage, null, null);
        } else {
            g2D.drawImage(card.scaledImage(), null, null);
        }
    }

    if (shortPaint)
        return;

    /*
     * The card picture is displayed as a rounded rectangle with 0,90,180 or
     * 270
     */
    if (card.isHighLighted && (card.isVisibleForYou() || StackManager.idHandedPlayer == 0)) {
        // Draw the rounded colored rectangle
        g2D.setColor(card.highLightColor);
        g2D.draw3DRect(0, 0, CardFactory.cardWidth - 2, CardFactory.cardHeight - 2, false);
    }

    // Draw the eventual progress bar
    card.database.updatePaintNotification(card, g);

    // Cursor for our pretty pictures
    int px = 3;
    int py = 3;
    int maxX = CardFactory.cardWidth - 2;

    // for in-play, visible cards
    if (card.isVisibleForYou() || (container != null && container.isVisibleForYou())) {

        // draw registers above the card's picture
        if (refreshToolTipFilter().powerANDtoughness) {
            // power / toughness
            final String powerANDtoughness = String.valueOf(card.getValue(IdTokens.POWER)) + "/"
                    + card.getValue(IdTokens.TOUGHNESS);
            g2D.setFont(g2D.getFont().deriveFont(Font.BOLD, 13));
            final Rectangle2D stringDim = g2D.getFontMetrics().getStringBounds(powerANDtoughness, g2D);
            g2D.setColor(CardFactory.powerToughnessColor);
            g2D.drawString(powerANDtoughness, (int) (CardFactory.cardWidth - stringDim.getWidth() - 3),
                    CardFactory.cardHeight - 5);
            g2D.setColor(Color.BLUE);
            g2D.drawString(powerANDtoughness, (int) (CardFactory.cardWidth - stringDim.getWidth() - 3),
                    CardFactory.cardHeight - 6);
        }

        /*
         * START drawing additional pictures
         */

        // draw state pictures
        for (StatePicture statePicture : CardFactory.statePictures) {
            if (px + 13 > maxX) {
                px = 3;
                py += 13;
            }
            if (statePicture.paint(card, g2D, px, py)) {
                px += 13;
            }
        }

        // draw properties
        if (card.cachedProperties != null) {
            for (Integer property : card.cachedProperties) {
                if (Configuration.getBoolean("card.property.picture", true)
                        && CardFactory.propertyPictures.get(property) != null) {
                    // There is an associated picture to this property
                    if (px + 13 > maxX) {
                        px = 3;
                        py += 13;
                    }
                    g2D.drawImage(CardFactory.propertyPictures.get(property), px, py, 12, 12, null);
                    px += 13;
                }
            }
        }
    }

    // draw attached objects
    int startX = 3;
    int startY = CardFactory.cardHeight - 18;
    if (card.registerModifiers != null) {
        for (int i = card.registerModifiers.length; i-- > 0;) {
            if (card.registerModifiers[i] != null) {
                startX = card.registerModifiers[i].paintObject(g, startX, startY);
            }
        }
    }
    if (card.propertyModifier != null) {
        startX = card.propertyModifier.paintObject(g, startX, startY);
    }
    if (card.idCardModifier != null) {
        card.idCardModifier.paintObject(g, startX, startY);
        /*
         * END drawing additional pictures
         */
    }

    // Draw the target Id if helper said it
    final String id = TargetHelper.getInstance().getMyId(card);
    if (id != null) {
        if (px + 13 > maxX) {
            px = 3;
            py += 13;
        }
        if (id == TargetHelper.STR_CONTEXT1) {
            // TODO I am in the context 1, draw a picture
            g2D.setColor(Color.BLUE);
            g2D.setFont(g2D.getFont().deriveFont(Font.BOLD, 60 / id.length()));
            g2D.drawString(id, 5, CardFactory.cardHeight - 15);
        } else if (id == TargetHelper.STR_CONTEXT2) {
            // TODO I am in the context 2, draw a picture
            g2D.setColor(Color.BLUE);
            g2D.setFont(g2D.getFont().deriveFont(Font.BOLD, 60 / id.length()));
            g2D.drawString(id, 5, CardFactory.cardHeight - 15);
        } else if (id != TargetHelper.STR_SOURCE) {
            // } else if (id == TargetHelper.STR_SOURCE) {
            // TODO I am the source, draw a picture
            // } else {
            // I am a target
            g2D.drawImage(TargetHelper.getInstance().getTargetPictureSml(), px, py, null);
            py += 12;
        }
    }

    g2D.dispose();
}

From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java

private BufferedImage createFloorImage(HmFolder floor, double scale, int floorWidth, int floorHeight,
        Map<Long, Integer> channelMap, Map<Long, Integer> colorMap, int borderX, int borderY, double gridSize)
        throws Exception {
    BufferedImage image = new BufferedImage(floorWidth + borderX + 1, floorHeight + borderY + 1,
            BufferedImage.TYPE_INT_ARGB);
    if (floor == null) {
        return image;
    }/*from w w w.  j a  v  a  2  s  .  c o m*/
    double metricWidth = 0.0, metricHeight = 0.0, offsetX = 0.0, offsetY = 0.0;
    int imageWidth = 0;
    LengthUnit lengthUnit = LengthUnit.METERS;

    if (null != floor.getMetricWidth()) {
        metricWidth = floor.getMetricWidth().doubleValue();
    }
    if (null != floor.getMetricHeight()) {
        metricHeight = floor.getMetricHeight().doubleValue();
    }
    if (null != floor.getImageWidth()) {
        imageWidth = floor.getImageWidth().intValue();
    }
    if (null != floor.getOffsetX()) {
        offsetX = floor.getOffsetX().doubleValue();
    }
    if (null != floor.getOffsetY()) {
        offsetY = floor.getOffsetY().doubleValue();
    }
    if (null != floor.getLengthUnit()) {
        lengthUnit = floor.getLengthUnit();
    }

    Graphics2D g2 = image.createGraphics();
    g2.setStroke(new BasicStroke(1));
    if (getDistanceMetric(metricWidth, lengthUnit) == 0) {
        g2.setColor(new Color(255, 255, 255));
        g2.fillRect(borderX, 0, floorWidth + 1, borderY);
        g2.fillRect(0, 0, borderX, borderY + floorHeight + 1);
        g2.setColor(new Color(120, 120, 120));
        g2.drawLine(0, borderY, floorWidth + borderX, borderY);
        g2.drawLine(borderX, 0, borderX, floorHeight + borderY);
        g2.setColor(new Color(255, 255, 204));
        g2.fillRect(borderX + 2, borderY + 2, 162, 25);
        g2.setColor(new Color(0, 51, 102));
        g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
        g2.drawString("Please size this floor plan.", borderX + 8, borderY + 19);
        return image;
    }
    double screenWidth = scale * getDistanceMetric(metricWidth, lengthUnit);
    double imageScale = screenWidth / imageWidth;
    int originX = (int) (getDistanceMetric(offsetX, lengthUnit) * scale);
    int originY = (int) (getDistanceMetric(offsetY, lengthUnit) * scale);
    g2.setColor(new Color(255, 255, 255));
    if (floor.getBackground() != null && floor.getBackground().length() > 0) {
        LinkedMultiValueMap<String, String> metadata = new LinkedMultiValueMap<>();
        String url = getImageBaseUrl(floor.getOwnerId()) + floor.getBackground();
        try {
            BufferedImage map = ImageIO.read(clientFileService.getFile(url, "AFS_TOKEN", metadata));
            AffineTransform transform = new AffineTransform();
            transform.scale(imageScale, imageScale);
            g2.drawImage(map, new AffineTransformOp(transform, null), getFloorX(0, borderX, originX),
                    getFloorY(0, borderY, originY));
        } catch (Exception e) {
            logger.error(String.format("image file not found with url: %s", url));
            double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit);
            g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth,
                    (int) screenHeight);
        }
    } else {
        double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit);
        g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth,
                (int) screenHeight);
    }
    g2.setColor(new Color(204, 204, 204));
    // Right edge border
    g2.drawLine(borderX + floorWidth, borderY + 1, borderX + floorWidth, borderY + floorHeight);
    // Left edge border (right of tick marks)
    g2.drawLine(borderX + 1, borderY + floorHeight, borderX + floorWidth, borderY + floorHeight);
    g2.setColor(new Color(255, 255, 255));
    g2.fillRect(borderX, 0, floorWidth + 1, borderY);
    g2.fillRect(0, 0, borderX, borderY + floorHeight + 1);
    g2.setColor(new Color(120, 120, 120));
    g2.drawLine(0, borderY, floorWidth + borderX, borderY);
    g2.drawLine(borderX, 0, borderX, floorHeight + borderY);

    Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12);
    double actualWidth = floorWidth / scale;
    double actualHeight = floorHeight / scale;
    String firstLabel;
    double unitScale = scale;
    if (LengthUnit.FEET == lengthUnit) {
        firstLabel = "0 feet";
        actualWidth /= HmFolder.FEET_TO_METERS;
        actualHeight /= HmFolder.FEET_TO_METERS;
        unitScale *= HmFolder.FEET_TO_METERS;
    } else {
        firstLabel = "0 meters";
    }
    g2.drawString(firstLabel, borderX + 4, 12);
    double gridX = gridSize;
    while (gridX < actualWidth) {
        int x = (int) (gridX * unitScale) + borderX;
        g2.drawLine(x, 0, x, borderY);
        boolean label = true;
        if (gridX + gridSize >= actualWidth) {
            // Last mark
            if (x + getNumberPixelWidth(gridX) + 2 > floorWidth) {
                label = false;
            }
        }
        if (label) {
            g2.drawString("" + (int) gridX, x + 4, 12);
        }
        gridX += gridSize;
    }

    double gridY = 0;
    while (gridY < actualHeight) {
        int y = (int) (gridY * unitScale) + borderY;
        g2.drawLine(0, y, borderX, y);
        double lx = gridY;
        int dx = 1;
        for (int bx = borderX; bx >= 16; bx -= 7) {
            if (lx < 10) {
                dx += 7;
            } else {
                lx /= 10;
            }
        }
        boolean label = true;
        if (gridY + gridSize >= actualHeight) {
            // Last mark
            if (y - borderY + 13 > floorHeight) {
                label = false;
            }
        }
        if (label) {
            g2.drawString("" + (int) gridY, dx, y + 13);
        }
        gridY += gridSize;
    }

    double mapToImage = getMapToMetric(metricWidth, imageWidth, lengthUnit) * scale;
    if (floor.getPerimeter().size() > 0) {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(new Color(2, 159, 245));
        int[] xPoints = new int[floor.getPerimeter().size()];
        int[] yPoints = new int[floor.getPerimeter().size()];
        int nPoints = 0;
        int perimId = floor.getPerimeter().get(0).getId();
        for (int i = 0; i < floor.getPerimeter().size(); i++) {
            HmVertex vertex = floor.getPerimeter().get(i);
            if (vertex.getId() != perimId) {
                g2.drawPolygon(xPoints, yPoints, nPoints);
                nPoints = 0;
                perimId = vertex.getId();
            }
            xPoints[nPoints] = getFloorX((int) (vertex.getX() * mapToImage), borderX, originX);
            yPoints[nPoints++] = getFloorY((int) (vertex.getY() * mapToImage), borderY, originY);
        }
        g2.drawPolygon(xPoints, yPoints, nPoints);
    }

    g2.setStroke(new BasicStroke(1));
    g2.setColor(new Color(0, 170, 0));
    g2.setFont(font.deriveFont(Font.BOLD, 11));

    List<HmDeviceLocationEx> devices = deviceLocationExRep.findAllHmDevices(floor.getId());
    if (null != devices && !devices.isEmpty()) {
        for (HmDeviceLocationEx device : devices) {
            double x = device.getX() * mapToImage;
            double y = device.getY() * mapToImage;
            createNodeImage(device.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX),
                    getFloorY((int) y, borderY, originY), g2);
        }
    } else {
        List<HmDevicePlanningEx> plannedDevices = devicePlanningExRep.findAllPlannedDevices(floor.getId());
        for (HmDevicePlanningEx plannedDevice : plannedDevices) {
            double x = plannedDevice.getX() * mapToImage;
            double y = plannedDevice.getY() * mapToImage;
            createNodeImage(plannedDevice.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX),
                    getFloorY((int) y, borderY, originY), g2);
        }
    }
    return image;
}