find All Color from BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

find All Color from 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{
    /**/*ww  w.j  a  v a2 s . c om*/
     * 
     * @param c
     *            - desired Color
     * @return - Array of point locations for colors = c
     */
    public static Point[] findAllColor(Color c) {
        ArrayList<Point> list = new ArrayList<Point>();
        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)) {
                    list.add(new Point(i, j));
                }
            }
        }
        Point p[] = new Point[list.size()];
        for (int i = 0; i < p.length; i++) {
            p[i] = list.get(i);
        }
        return p;
    }
    /**
     * 
     * @param c
     *            - desired Color
     * @param b
     *            - the buffered image in which to search for the desired color
     * @return - Array of point locations for colors = c
     */
    public static Point[] findAllColor(Color c, BufferedImage b) {
        ArrayList<Point> list = new ArrayList<Point>();
        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)) {
                    list.add(new Point(i, j));
                }
            }
        }
        Point p[] = new Point[list.size()];
        for (int i = 0; i < p.length; i++) {
            p[i] = list.get(i);
        }
        return p;
    }
    /**
     * 
     * @param c
     *            - desired Color
     * @param r
     *            - the rectangle in which to search for the desired color
     * @return - Array of point locations for colors = c
     */
    public static Point[] findAllColor(Color c, Rectangle r) {
        BufferedImage b = getScreenPart(r);
        ArrayList<Point> list = new ArrayList<Point>();
        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)) {
                    list.add(new Point(i, j));
                }
            }
        }
        Point p[] = new Point[list.size()];
        for (int i = 0; i < p.length; i++) {
            p[i] = list.get(i);
        }
        return p;
    }
    /**
     * @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