FileDocCategorySizeDatePackage
Region.javaAPI DocJMF 2.1.1e4841Mon May 12 12:20:52 BST 2003com.sun.media.ui

Region

public class Region extends Object implements Serializable, Cloneable

Fields Summary
protected Vector
rects
List of rectangles that make up the region.
Constructors Summary
public Region()
Create a new region with no rectangles.

        rects = new Vector();
    
public Region(Rectangle r)
Create a region with one rectangle in it.

        rects = new Vector();
        addRectangle(r);
    
public Region(Rectangle r1, Rectangle r2)
Create a region with two rectangles. Useful for moving-sprites which need to invalidate old and new positions.

        rects = new Vector();
        addRectangle(r1);
        addRectangle(r2);
    
Methods Summary
public voidaddRectangle(java.awt.Rectangle r)
Adds a rectangle to the region. This method checks for intersecting rectangles and merges them into one.


        Rectangle current;
        int position = 0;
        
        while (position < rects.size())
        {
            current = (Rectangle)rects.elementAt(position);

            // First check to see if the current rectangle
            // already includes the new one
            if ((r.x > current.x) && (r.y > current.y) &&
                (right(r) <= right(current)) && 
                (bottom(r) <= bottom(current)))
                return;

            if (r.intersects(current))
            {
                r = r.union(current);
                rects.removeElementAt(position);
            } else {
                position++;
            }
        }
        // Add it to the end of the list
        rects.addElement(r);
    
public voidaddRegion(com.sun.media.ui.Region r)
Adds a region r to the current region.


        for (Enumeration e = r.rectangles(); e.hasMoreElements(); )
        {
            addRectangle((Rectangle)e.nextElement());
        }
    
public static intbottom(java.awt.Rectangle r)
Returns the y-coordinate of the bottom edge of a rectangle.

        return r.y + r.height - 1;
    
public java.lang.Objectclone()
Returns a clone of the region.

        Region r = new Region();
        
        r.rects = (Vector)rects.clone();

        return r;
    
public java.awt.RectanglegetBounds()

	Rectangle r = new Rectangle();
	
	for (int i = 0; i < rects.size(); i++) {
	    r = r.union((Rectangle)rects.elementAt(i));
	}
	return r;
    
public intgetNumRectangles()

	return rects.size();
    
public voidintersect(java.awt.Rectangle r)
Intersects all the rectangles in the region with rectangle r and throws away any that dont intersect.


        Rectangle rect;
        int position = 0;

        while (position < rects.size()) {
            rect = (Rectangle)rects.elementAt(position);
            rect = rect.intersection(r);
            if (rect.isEmpty()) {
                rects.removeElementAt(position);
            } else {
                rects.setElementAt(rect, position);
                position++;
            }
        }
    
public booleanintersects(java.awt.Rectangle r)
Checks if the rectangle r intersects any of the rectangles in the region.


	Rectangle rect;
	int position = 0;
	
	while (position < rects.size()) {
	    rect = (Rectangle)rects.elementAt(position);
	    if (rect.intersects(r)) {
		return true;
	    }
	    position++;
	}
	return false;
    
public booleanisEmpty()
Returns true if the region contains no rectangles.

        return rects.isEmpty();
    
public java.util.Enumerationrectangles()
Returns an Enumeration of the list of rectangles.

        return rects.elements();
    
public static intright(java.awt.Rectangle r)
Returns the x-coordinate of the right edge of a rectangle.

        return r.x + r.width - 1;
    
public java.lang.StringtoString()
Converts the list of rectangles to a displayable string.

        String s = getClass().getName() + " = [\n";

        for (Enumeration e = rectangles(); e.hasMoreElements(); )
        {
            s += "(" + (Rectangle)e.nextElement() + ")\n";
        }
        return s + "]";
    
public voidtranslate(int dx, int dy)
Translates all rectangles in the region by (dx,dy).


        Rectangle r;

        for (int p = 0; p < rects.size(); p++) {
            r = (Rectangle)rects.elementAt(p);
            r.translate(dx, dy);
        }