find Color on BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

find Color on BufferedImage

Demo Code


import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Main{
    /**//from   w ww.jav  a 2s. c  om
     * 
     * @param c
     *            - the color to search for.
     * @return the location of Color on screen.
     */
    public static Point findColor(Color c) {
        BufferedImage b = getScreen();
        for (int i = 0; i < b.getWidth(); i++) {
            for (int j = 0; j < b.getHeight(); j++) {
                if (new Color(b.getRGB(i, j)).equals(c)) {
                    return new Point(i, j);
                }
            }
        }
        return null;
    }
    /**
     * 
     * @param c
     *            the desired color.
     * @param b
     *            the buffered imagine in which you would like to search for the
     *            color specified.
     * @return Point location
     */
    public static Point findColor(Color c, BufferedImage b) {
        for (int i = 0; i < b.getWidth(); i++) {
            for (int j = 0; j < b.getHeight(); j++) {
                if (new Color(b.getRGB(i, j)).equals(c)) {
                    return new Point(i, j);
                }
            }
        }
        return null;
    }
    /**
     * 
     * @param c
     *            the desired color.
     * @param r
     *            the rectangle area on the screen in which you would like to
     *            search for the color specified
     * @return Point location
     */
    public static Point findColor(Color c, Rectangle r) {
        BufferedImage b = getScreenPart(r);
        for (int i = 0; i < b.getWidth(); i++) {
            for (int j = 0; j < b.getHeight(); j++) {
                if (new Color(b.getRGB(i, j)).equals(c)) {
                    return new Point(i, j);
                }
            }
        }
        return null;
    }
    /**
     * 
     * @param c
     *            the given color to search for.
     * @param start
     *            the start point.
     * @param end
     *            the end point.
     * @return the point of the given color between the start and end points.
     */
    public static Point findColor(java.awt.Color c, Point start, Point end) {
        BufferedImage b = getScreen();
        if (b != null) {
            for (int i = start.x; i < end.x; i++) {
                for (int j = start.y; j < end.y; j++) {

                    if (new java.awt.Color(b.getRGB(i, j)).equals(c)) {
                        return new Point(i, j);
                    }

                }
            }
        }
        return null;
    }
    /**
     * @return Game Screen as BufferedImage.
     */
    public static BufferedImage getScreen() {
        return Client.getScreen();
    }
    /**
     * 
     * @param area
     *            the area's rectangle.
     * @return BufferedImage of the given rectangle.
     */
    public static BufferedImage getScreenPart(Rectangle area) {
        if (area.x > 0 && area.x < 756 && area.x + area.width > 0
                && area.x + area.width < 756) {
            if (area.y > 0 && area.y < 756 && area.y + area.height > 0
                    && area.y + area.height < 756) {
                return getScreen().getSubimage(area.x, area.y, area.width,
                        area.height);
            }
        }
        return null;
    }
}

Related Tutorials