union Rectangle array - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

union Rectangle array

Demo Code


//package com.java2s;

import java.awt.Rectangle;

import java.awt.geom.Rectangle2D;

import java.util.ArrayList;

public class Main {
    public static Rectangle union(Rectangle[] rectangles) {
        if (rectangles.length == 0) {
            return null;
        }/*www.j av a2  s  . c o  m*/
        Rectangle union = null;//new Rectangle( rectangles[0] );
        for (int i = 0; i < rectangles.length; i++) {
            Rectangle rectangle = rectangles[i];
            if (rectangle != null) {
                if (union == null) {
                    union = new Rectangle(rectangle);
                } else {
                    union = union.union(rectangle);
                }
            }

        }
        return union;
    }

    public static Rectangle union(ArrayList rectangles) {
        if (rectangles.size() == 0) {
            return null;
        }
        Rectangle union = (Rectangle) rectangles.remove(0);
        while (rectangles.size() > 0) {
            Object r = rectangles.remove(0);
            if (r != null) {
                // WORKAROUND: the list sometimes contains null objects
                union = union.union((Rectangle) r);
            }
        }
        return union;
    }

    public static Rectangle2D union(Rectangle2D[] rectangles) {
        if (rectangles.length == 0) {
            return null;
        }
        Rectangle2D.Double union = null;//new Rectangle( rectangles[0] );
        for (int i = 0; i < rectangles.length; i++) {
            Rectangle2D rectangle = rectangles[i];
            if (rectangle != null) {
                if (union == null) {
                    union = new Rectangle2D.Double(rectangle.getX(),
                            rectangle.getY(), rectangle.getWidth(),
                            rectangle.getHeight());
                } else {
                    Rectangle2D.union(union, rectangle, union);
                    //                    union = union.union( rectangle.getX(),rectangle.getY(),rectangle.getWidth(),rectangle.getLength() );
                }
            }

        }
        return union;
    }
}

Related Tutorials