Java BufferedImage Operation markImageBorders(final BufferedImage img, final int startW, final int startH)

Here you can find the source of markImageBorders(final BufferedImage img, final int startW, final int startH)

Description

Fully marks the bottom and right borders of an image transparent (transparent white).

License

Open Source License

Parameter

Parameter Description
img the image to mark
startW the width from which to start marking
startH the height from which the marking starts

Return

the marked image

Declaration

protected static BufferedImage markImageBorders(final BufferedImage img, final int startW, final int startH) 

Method Source Code


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

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    /**//from  w w w  .  ja  v  a 2 s  .  co  m
     * Fully marks the bottom and right borders of an image transparent (transparent white). Used in isEqual to mark the
     * previously not existent parts of an image.
     * 
     * @param img
     *            the image to mark
     * @param startW
     *            the width from which to start marking
     * @param startH
     *            the height from which the marking starts
     * @return the marked image
     */
    protected static BufferedImage markImageBorders(final BufferedImage img, final int startW, final int startH) {
        final BufferedImage copy = copyImage(img);

        final Color markTransparentWhite = new Color(255, 255, 255, 0);
        final Graphics2D g = copy.createGraphics();

        g.setColor(markTransparentWhite);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f));

        if (startW < copy.getWidth()) {
            g.fillRect(startW, 0, copy.getWidth() - startW, copy.getHeight());
        }
        if (startH < copy.getHeight()) {
            g.fillRect(0, startH, copy.getWidth(), copy.getHeight() - startH);
        }
        g.dispose();

        return copy;
    }

    /**
     * Creates another image, which is a copy of the source image
     * 
     * @param source
     *            the image to copy
     * @return a copy of that image as <b>BufferedImage</b>
     */
    protected static BufferedImage copyImage(final BufferedImage source) {
        // Creates a fresh BufferedImage that has the same size and content of
        // the source image

        // if source has an image type of 0 set ARGB as type
        int imageType = source.getType();
        if (imageType == 0) {
            imageType = BufferedImage.TYPE_INT_ARGB;
        }

        final BufferedImage copy = new BufferedImage(source.getWidth(), source.getHeight(), imageType);
        final Graphics g = copy.getGraphics();
        g.drawImage(source, 0, 0, null);
        g.dispose();
        return copy;
    }
}

Related

  1. makeItBlack(BufferedImage image)
  2. makeMouseover(final BufferedImage src)
  3. makeOpaque(BufferedImage img, Color col)
  4. MakePoly(BufferedImage spr, int d, int angle, int baseX, int baseY)
  5. makeTintedCopy(BufferedImage bi, Color tint)
  6. matBytesToBufferedImage(byte[] data, int cols, int rows, int type)
  7. matte(BufferedImage image, int top, int bottom, int left, int right, Color bg)
  8. mirror(final BufferedImage image, final boolean horizontal)
  9. newOptimizedImageLike(GraphicsConfiguration destination, BufferedImage img)