Java Rectangle Fit fitRectInRect(Rectangle rect, Rectangle bounds)

Here you can find the source of fitRectInRect(Rectangle rect, Rectangle bounds)

Description

Returns the most reasonable position for the specified rectangle to be placed at so as to maximize its containment by the specified bounding rectangle while still placing it as near its original coordinates as possible.

License

Open Source License

Parameter

Parameter Description
rect the rectangle to be positioned.
bounds the containing rectangle.

Declaration

public static Point fitRectInRect(Rectangle rect, Rectangle bounds) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from  w ww  .j  a v a 2  s .c  om*/
     * Returns the most reasonable position for the specified rectangle to be placed at so as to
     * maximize its containment by the specified bounding rectangle while still placing it as near
     * its original coordinates as possible.
     *
     * @param rect the rectangle to be positioned.
     * @param bounds the containing rectangle.
     */
    public static Point fitRectInRect(Rectangle rect, Rectangle bounds) {
        // Guarantee that the right and bottom edges will be contained and do our best for the top
        // and left edges.
        return new Point(Math.min(bounds.x + bounds.width - rect.width, Math.max(rect.x, bounds.x)),
                Math.min(bounds.y + bounds.height - rect.height, Math.max(rect.y, bounds.y)));
    }
}

Related

  1. fitRectangle(Rectangle rect, Dimension d)
  2. fitRectangle(Rectangle rect, Rectangle target)
  3. fitRectangles(Dimension imageSize, Dimension size)
  4. fits(Rectangle r)
  5. fits(Rectangle2D o1, Rectangle2D o2)
  6. fitsInside(Dimension2D dim, Rectangle2D rect)
  7. fitsRotated(Rectangle2D o1, Rectangle2D o2)