FileDocCategorySizeDatePackage
MultiRectArea.javaAPI DocAndroid 1.5 API23374Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl

MultiRectArea

public class MultiRectArea extends Object implements Shape
author
Denis M. Kishenko
version
$Revision$

Fields Summary
private static final boolean
CHECK
If CHECK is true validation check active
boolean
sorted
public int[]
rect
Rectangle buffer
Rectangle
bounds
Bounding box
Rectangle[]
rectangles
Result rectangle array
Constructors Summary
public MultiRectArea()
Constructs a new empty MultiRectArea

        rect = MultiRectAreaOp.createBuf(0);
    
public MultiRectArea(boolean sorted)

       this();
       this.sorted = sorted;
    
public MultiRectArea(MultiRectArea mra)
Constructs a new MultiRectArea as a copy of another one

        if (mra == null) {
            rect = MultiRectAreaOp.createBuf(0);
        } else {
            rect = new int[mra.rect.length];
            System.arraycopy(mra.rect, 0, rect, 0, mra.rect.length);
            check(this, "MultiRectArea(MRA)"); //$NON-NLS-1$
        }
    
public MultiRectArea(Rectangle r)
Constructs a new MultiRectArea consists of single rectangle

        rect = MultiRectAreaOp.createBuf(0);
        if (r != null && !r.isEmpty()) {
            rect[0] = 5;
            rect[1] = r.x;
            rect[2] = r.y;
            rect[3] = r.x + r.width - 1;
            rect[4] = r.y + r.height - 1;
        }
        check(this, "MultiRectArea(Rectangle)"); //$NON-NLS-1$
    
public MultiRectArea(int x0, int y0, int x1, int y1)
Constructs a new MultiRectArea consists of single rectangle

        rect = MultiRectAreaOp.createBuf(0);
        if (x1 >= x0 && y1 >= y0) {
            rect[0] = 5;
            rect[1] = x0;
            rect[2] = y0;
            rect[3] = x1;
            rect[4] = y1;
        }
        check(this, "MultiRectArea(Rectangle)"); //$NON-NLS-1$
    
public MultiRectArea(Rectangle[] buf)
Constructs a new MultiRectArea and append rectangle from buffer

        this();
        for (Rectangle element : buf) {
            add(element);
        }
    
public MultiRectArea(ArrayList buf)
Constructs a new MultiRectArea and append rectangle from array

        this();
        for(int i = 0; i < buf.size(); i++) {
            add(buf.get(i));
        }
    
Methods Summary
public voidadd(org.apache.harmony.awt.gl.MultiRectArea mra)
Union with another MultiRectArea object

        setRect(union(this, mra).rect, false);
        invalidate();
    
public voidadd(java.awt.Rectangle rect)
Union with Rectangle object

        setRect(union(this, new MultiRectArea(rect)).rect, false);
        invalidate();
    
public voidaddRect(int x1, int y1, int x2, int y2)
Add rectangle to the buffer without any checking

        int i = rect[0];
        rect = MultiRectAreaOp.checkBufSize(rect, 4);
        rect[i++] = x1;
        rect[i++] = y1;
        rect[i++] = x2;
        rect[i++] = y2;
    
static org.apache.harmony.awt.gl.MultiRectAreacheck(org.apache.harmony.awt.gl.MultiRectArea mra, java.lang.String msg)
Checks validation of MultiRectArea object

        if (CHECK && mra != null) {
            if (MultiRectArea.checkValidation(mra.getRectangles(), mra.sorted) != -1) {
                // awt.4C=Invalid MultiRectArea in method {0}
                new RuntimeException(Messages.getString("awt.4C", msg)); //$NON-NLS-1$
            }
        }
        return mra;
    
public static intcheckValidation(java.awt.Rectangle[] r, boolean sorted)
Checks validation of MultiRectArea object


        // Check width and height
        for(int i = 0; i < r.length; i++) {
            if (r[i].width <= 0 || r[i].height <= 0) {
                return i;
            }
        }

        // Check order
        if (sorted) {
            for(int i = 1; i < r.length; i++) {
                if (r[i - 1].y > r[i].y) {
                    return i;
                }
                if (r[i - 1].y == r[i].y) {
                    if (r[i - 1].x > r[i].x) {
                        return i;
                    }
                }
            }
        }

        // Check override
        for(int i = 0; i < r.length; i++) {
            for(int j = i + 1; j < r.length; j++) {
                if (r[i].intersects(r[j])) {
                    return i;
                }
            }
        }

        return -1;
    
public booleancontains(double x, double y)
Tests does point lie inside MultiRectArea object

        for(int i = 1; i < rect[0]; i+= 4) {
            if (rect[i] <= x && x <= rect[i + 2] && rect[i + 1] <= y && y <= rect[i + 3]) {
                return true;
            }
        }
        return false;
    
public booleancontains(java.awt.geom.Point2D p)
Tests does Point2D lie inside MultiRectArea object

        return contains(p.getX(), p.getY());
    
public booleancontains(double x, double y, double w, double h)
Tests does rectangle lie inside MultiRectArea object

        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    
public booleancontains(java.awt.geom.Rectangle2D r)
Tests does Rectangle2D lie inside MultiRectArea object

        throw new RuntimeException("Not implemented"); //$NON-NLS-1$
    
public booleanequals(java.lang.Object obj)
Tests equals with another object

        if (obj == this) {
            return true;
        }
        if (obj instanceof MultiRectArea) {
            MultiRectArea mra = (MultiRectArea) obj;
            for(int i = 0; i < rect[0]; i++) {
                if (rect[i] != mra.rect[i]) {
                    return false;
                }
            }
            return true;
        }
        return false;
    
public java.awt.RectanglegetBounds()
Returns bounds of MultiRectArea object

        if (bounds != null) {
            return bounds;
        }

        if (isEmpty()) {
            return bounds = new Rectangle();
        }

        int x1 = rect[1];
        int y1 = rect[2];
        int x2 = rect[3];
        int y2 = rect[4];
        
        for(int i = 5; i < rect[0]; i += 4) {
            int rx1 = rect[i + 0];
            int ry1 = rect[i + 1];
            int rx2 = rect[i + 2];
            int ry2 = rect[i + 3];
            if (rx1 < x1) {
                x1 = rx1;
            }
            if (rx2 > x2) {
                x2 = rx2;
            }
            if (ry1 < y1) {
                y1 = ry1;
            }
            if (ry2 > y2) {
                y2 = ry2;
            }
        }
        
        return bounds = new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
    
public java.awt.geom.Rectangle2DgetBounds2D()
Returns Bounds2D

        return getBounds();
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform t, double flatness)
Returns path iterator

        return new Iterator(this, t);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform t)
Returns path iterator

        return new Iterator(this, t);
    
public intgetRectCount()
Recturn rectangle count in the buffer

        return (rect[0] - 1) / 4;
    
public java.awt.Rectangle[]getRectangles()
Returns Rectangle array

        if (rectangles != null) {
            return rectangles;
        }

        rectangles = new Rectangle[(rect[0] - 1) / 4];
        int j = 0;
        for(int i = 1; i < rect[0]; i += 4) {
            rectangles[j++] = new Rectangle(
                    rect[i],
                    rect[i + 1],
                    rect[i + 2] - rect[i] + 1,
                    rect[i + 3] - rect[i + 1] + 1);
        }
        return rectangles;
    
public voidintersect(org.apache.harmony.awt.gl.MultiRectArea mra)
Intersect with another MultiRectArea object

        setRect(intersect(this, mra).rect, false);
        invalidate();
    
public voidintersect(java.awt.Rectangle rect)
Intersect with Rectangle object

        setRect(intersect(this, new MultiRectArea(rect)).rect, false);
        invalidate();
    
public static org.apache.harmony.awt.gl.MultiRectAreaintersect(org.apache.harmony.awt.gl.MultiRectArea src1, org.apache.harmony.awt.gl.MultiRectArea src2)
Union two MutliRectareArea objects

        MultiRectArea res = check(MultiRectAreaOp.Intersection.getResult(src1, src2), "intersect(MRA,MRA)"); //$NON-NLS-1$
        return res;
    
public booleanintersects(double x, double y, double w, double h)
Tests does rectangle intersect MultiRectArea object

        Rectangle r = new Rectangle();
        r.setRect(x, y, w, h);
        return intersects(r);
    
public booleanintersects(java.awt.geom.Rectangle2D r)
Tests does Rectangle2D intersect MultiRectArea object

        if (r == null || r.isEmpty()) {
            return false;
        }
        for(int i = 1; i < rect[0]; i+= 4) {
            if (r.intersects(rect[i], rect[i+1], rect[i + 2]-rect[i]+1, rect[i + 3]-rect[i + 1]+1)) {
                return true;
            }
        }
        return false;
    
voidinvalidate()

        bounds = null;
        rectangles = null;
    
public booleanisEmpty()
Tests is MultiRectArea empty

        return rect[0] == 1;
    
public static voidprint(org.apache.harmony.awt.gl.MultiRectArea mra, java.lang.String msg)
Print MultiRectArea object to output stream

        if (mra == null) {
            System.out.println(msg + "=null"); //$NON-NLS-1$
        } else {
            Rectangle[] rects = mra.getRectangles();
            System.out.println(msg + "(" + rects.length + ")"); //$NON-NLS-1$ //$NON-NLS-2$
            for (Rectangle element : rects) {
                System.out.println(
                        element.x + "," + //$NON-NLS-1$
                        element.y + "," + //$NON-NLS-1$
                        (element.x + element.width - 1) + "," + //$NON-NLS-1$
                        (element.y + element.height - 1));
            }
        }
    
voidresort()
Sort rectangle buffer

        int[] buf = new int[4];
        for(int i = 1; i < rect[0]; i += 4) {
            int k = i;
            int x1 = rect[k];
            int y1 = rect[k + 1];
            for(int j = i + 4; j < rect[0]; j += 4) {
                int x2 = rect[j];
                int y2 = rect[j + 1];
                if (y1 > y2 || (y1 == y2 && x1 > x2)) {
                    x1 = x2;
                    y1 = y2;
                    k = j;
                }
            }
            if (k != i) {
                System.arraycopy(rect, i, buf, 0, 4);
                System.arraycopy(rect, k, rect, i, 4);
                System.arraycopy(buf, 0, rect, k, 4);
            }
        }
        invalidate();
    
protected voidsetRect(int[] buf, boolean copy)
Assigns rectangle from another buffer

        if (copy) {
            rect = new int[buf.length];
            System.arraycopy(buf, 0, rect, 0, buf.length);
        } else {
            rect = buf;
        }
        invalidate();
    
public voidsubstract(org.apache.harmony.awt.gl.MultiRectArea mra)
Subtract another MultiRectArea object

        setRect(subtract(this, mra).rect, false);
        invalidate();
    
public voidsubstract(java.awt.Rectangle rect)
Subtract rectangle object

        setRect(subtract(this, new MultiRectArea(rect)).rect, false);
    
public static org.apache.harmony.awt.gl.MultiRectAreasubtract(org.apache.harmony.awt.gl.MultiRectArea src1, org.apache.harmony.awt.gl.MultiRectArea src2)
Subtract two MultiRectArea objects

        MultiRectArea res = check(MultiRectAreaOp.Subtraction.getResult(src1, src2), "subtract(MRA,MRA)"); //$NON-NLS-1$
        return res;
    
public java.lang.StringtoString()
Returns MultiRectArea object converted to string

        int cnt = getRectCount();
        StringBuffer sb = new StringBuffer((cnt << 5) + 128);
        sb.append(getClass().getName()).append(" ["); //$NON-NLS-1$
        for(int i = 1; i < rect[0]; i += 4) {
            sb.append(i > 1 ? ", [" : "[").append(rect[i]).append(", ").append(rect[i + 1]). //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            append(", ").append(rect[i + 2] - rect[i] + 1).append(", "). //$NON-NLS-1$ //$NON-NLS-2$
            append(rect[i + 3] - rect[i + 1] + 1).append("]"); //$NON-NLS-1$
        }
        return sb.append("]").toString(); //$NON-NLS-1$
    
public voidtranslate(int x, int y)
Translate MultiRectArea object by (x, y)

        for(int i = 1; i < rect[0];) {
            rect[i++] += x;
            rect[i++] += y;
            rect[i++] += x;
            rect[i++] += y;
        }

        if (bounds != null && !bounds.isEmpty()) {
            bounds.translate(x, y);
        }

        if (rectangles != null) {
            for (Rectangle element : rectangles) {
                element.translate(x, y);
            }
        }
    
public static org.apache.harmony.awt.gl.MultiRectAreaunion(org.apache.harmony.awt.gl.MultiRectArea src1, org.apache.harmony.awt.gl.MultiRectArea src2)
Intersect two MultiRectArea objects

        MultiRectArea res = check(new MultiRectAreaOp.Union().getResult(src1, src2), "union(MRA,MRA)"); //$NON-NLS-1$
        return res;