Return a rectangle that encapsulates all rectangles in the Vector. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Return a rectangle that encapsulates all rectangles in the Vector.

Demo Code

// All rights reserved.
//package com.java2s;
import java.awt.*;
import java.util.*;

public class Main {
    /**//www .  j a v a  2  s. co  m
     * Return a rectangle that encapsulates all rectangles in the Vector.
     * @param     vctRectangles    The list of rectangles.
     */
    public static Rectangle getRectangleListBounds(Vector vctRectangles) {
        // Call the version that takes an Enumeration object.
        return getRectangleListBounds(vctRectangles.elements());
    }

    /**
     * Return a rectangle that encapsulates all rectangles in the Enumeration.
     * @param     enumRectangles    The list of rectangles.
     */
    public static Rectangle getRectangleListBounds(
            Enumeration enumRectangles) {
        double nMinLeft = 20000;
        double nMinTop = 20000;
        double nMaxWidth = -1;
        double nMaxHeight = -1;
        // Process for rectangle in the list.
        while (enumRectangles.hasMoreElements()) {
            Rectangle rBounds = (Rectangle) (enumRectangles.nextElement());
            // Find new rectangle dimensions
            if (rBounds.getX() < nMinLeft) {
                nMinLeft = rBounds.getX();
            }
            if (rBounds.getY() < nMinTop) {
                nMinTop = rBounds.getY();
            }
            if (rBounds.getX() + rBounds.getWidth() > nMaxWidth) {
                nMaxWidth = rBounds.getX() + rBounds.getWidth();
            }
            if (rBounds.getY() + rBounds.getHeight() > nMaxHeight) {
                nMaxHeight = rBounds.getY() + rBounds.getHeight();
            }
        }
        return new Rectangle((int) nMinLeft, (int) nMinTop,
                (int) (nMaxWidth - nMinLeft), (int) (nMaxHeight - nMinTop));
    }
}

Related Tutorials