get Clip Rectangle - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

get Clip Rectangle

Demo Code

/**/*from  w ww.ja va 2  s .c o  m*/
 *   Copyright 2012 Dr. Krusche & Partner PartG
 *
 *   AMES-Web-Service is free software: you can redistribute it and/or 
 *   modify it under the terms of the GNU General Public License 
 *   as published by the Free Software Foundation, either version 3 of 
 *   the License, or (at your option) any later version.
 *
 *   AMES- Web-Service is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * 
 *  See the GNU General Public License for more details. 
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this software. If not, see <http://www.gnu.org/licenses/>.
 *
 */
//package com.java2s;

import java.awt.Point;
import java.awt.Rectangle;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;

public class Main {
    /**
     * @param img
     * @param x
     * @param y
     * @return
     */
    public static Rectangle getClipRectangle(BufferedImage img, int x, int y) {

        int[] aux = { 255, 255, 255, 255 };
        int[] bgColor = { 255, 255, 255, 255 };

        Raster raster = img.getRaster();

        /* 
         * Retrieve the background color
         */
        raster.getPixel(x, y, bgColor);

        Point tl = new Point(0, 0);
        Point br = new Point(raster.getWidth() - 1, raster.getHeight() - 1);

        /* 
         * Find the left border
         */
        boolean gotLef = false;
        for (int c = 0; !gotLef && (c < raster.getWidth()); c++) {
            for (int r = 0; r < raster.getHeight(); r++) {
                int[] pix = raster.getPixel(c, r, aux);
                if (comparePixel(bgColor, pix)) {
                    tl.x = c;
                    gotLef = true;
                    break;
                }
            }
        }

        /* 
         * Find the right border
         */
        boolean gotRig = false;
        for (int c = raster.getWidth() - 1; !gotRig && (c >= 0); c--) {
            // Find the right
            for (int r = 0; r < raster.getHeight(); r++) {
                int[] pix = raster.getPixel(c, r, aux);
                if (comparePixel(bgColor, pix)) {
                    br.x = c;
                    gotRig = true;
                    break;
                }
            }
        }

        /* 
         * Find the top border
         */
        boolean gotTop = false;
        for (int r = 0; !gotTop && (r < raster.getHeight()); r++) {
            for (int c = tl.x; c < br.x; c++) {
                int[] pix = raster.getPixel(c, r, aux);
                if (comparePixel(bgColor, pix)) {
                    tl.y = r;
                    gotTop = true;
                    break;
                }
            }
        }

        /* 
         * Find the bottom border
         */
        boolean gotBot = false;
        for (int r = raster.getHeight() - 1; !gotBot && (r >= 0); r--) {
            for (int c = tl.x; c < br.x; c++) {
                int[] pix = raster.getPixel(c, r, aux);
                if (comparePixel(bgColor, pix)) {
                    br.y = r;
                    gotBot = true;
                    break;
                }
            }
        }

        Rectangle rect = new Rectangle(tl.x, tl.y,
                Math.abs(br.x - tl.x) + 1, Math.abs(br.y - tl.y) + 1);
        return rect;

    }

    /**
     * Compares two pixels and returns true if they 
     * are different, otherwise returns false
     * 
     * @param p1
     * @param p2
     * @return
     */
    public static boolean comparePixel(int[] p1, int[] p2) {
        return ((p2[0] == p1[0]) && (p2[1] == p1[1]) && (p2[2] == p1[2])
                && (p2[3] == p1[3]) ? false : true);
    }
}

Related Tutorials