Java Draw Image drawBorders(final BufferedImage image, final int currentX, final int currentY, final int width, final int height, final int subImageWidth, final int subImageHeight, final Color c)

Here you can find the source of drawBorders(final BufferedImage image, final int currentX, final int currentY, final int width, final int height, final int subImageWidth, final int subImageHeight, final Color c)

Description

Colors the borders of a certain rectangle.

License

Open Source License

Parameter

Parameter Description
image The image in which something will be marked
currentX Starting position
currentY Starting position
width Vertical length of the rectangle to mark
height Horizontal length of the rectangle to mark
subImageWidth the width of the partial image
subImageHeight the height of the partial image

Declaration

protected static void drawBorders(final BufferedImage image, final int currentX, final int currentY,
        final int width, final int height, final int subImageWidth, final int subImageHeight, final Color c) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import java.awt.image.BufferedImage;

public class Main {
    /**/* w ww .  j  a  v a  2 s .  c  om*/
     * Colors the borders of a certain rectangle. Used to mark blocks. Uses the colorPixel method and subImageHeight/
     * subImageWidth. <br>
     * Works directly on imgOut.
     * 
     * @param image
     *            The image in which something will be marked
     * @param currentX
     *            Starting position
     * @param currentY
     *            Starting position
     * @param width
     *            Vertical length of the rectangle to mark
     * @param height
     *            Horizontal length of the rectangle to mark
     * @param subImageWidth the width of the partial image
     * @param subImageHeight the height of the partial image
     */
    protected static void drawBorders(final BufferedImage image, final int currentX, final int currentY,
            final int width, final int height, final int subImageWidth, final int subImageHeight, final Color c) {
        int x, y;

        for (int a = 0; a < subImageWidth; a++) {
            x = currentX * width + a;
            y = currentY * height;
            colorPixel(image, x, y, c);

            y = currentY * height + subImageHeight - 1;
            colorPixel(image, x, y, c);
        }

        for (int b = 1; b < subImageHeight - 1; b++) {
            x = currentX * width;
            y = currentY * height + b;
            colorPixel(image, x, y, c);

            x = currentX * width + subImageWidth - 1;
            colorPixel(image, x, y, c);
        }
    }

    /**
     * Colors a certain pixel using getComplementaryColor. Works directly on imgOut.
     * 
     * @param x
     *            the x coordinate of the pixel to color
     * @param y
     *            the y coordinate of the pixel to color
     */
    protected static void colorPixel(final BufferedImage image, final int x, final int y, final Color c) {
        Color newColor;

        if (c == null) {
            final Color currentColor = new Color(image.getRGB(x, y));
            final double redLimit = 0.8;

            final int nonRedSum = currentColor.getGreen() + currentColor.getBlue();
            final double results = nonRedSum > 0 ? currentColor.getRed() / nonRedSum : 0;

            if (results > redLimit) {
                // red is strong in that one
                newColor = Color.GREEN;
            } else {
                newColor = Color.RED;
            }
        } else {
            newColor = c;
        }

        image.setRGB(x, y, newColor.getRGB());
    }
}

Related

  1. draw(final Image img, final int type)
  2. draw4on1(BufferedImage[] src, BufferedImage dst)
  3. draw9Patch(BufferedImage image, Integer[] patches, float ratio)
  4. drawBorder(BufferedImage buffImage)
  5. drawBorder(BufferedImage originalImage, Color borderColor, int borderWidth)
  6. drawBoundingPolygon(BufferedImage canvas, Polygon p, Color fgColour)
  7. drawBubble(BufferedImage img, Graphics g, Component component)
  8. drawBytes(BufferedImage image, byte[] buf)
  9. drawCenterCrop(Graphics2D g, BufferedImage source, Rectangle dstRect)