Java Rectangle Fit fitRectangle(Rectangle rect, Dimension d)

Here you can find the source of fitRectangle(Rectangle rect, Dimension d)

Description

fit Rectangle

License

Open Source License

Declaration

public static void fitRectangle(Rectangle rect, Dimension d) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2006 Christian Schneider
 * /*from   w  w  w.  j a va2 s. com*/
 * This file is part of Nomad.
 * 
 * Nomad is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Nomad is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Nomad; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.awt.Dimension;
import java.awt.Rectangle;

public class Main {
    public static void fitRectangle(Rectangle rect, Dimension d) {
        fitRectangle(rect, d.width, d.height);
    }

    public static void fitRectangle(Rectangle rect, int w, int h) {
        // check top left point
        if (rect.x < 0) {
            rect.width -= (-rect.x);
            rect.x = 0;
        }
        if (rect.y < 0) {
            rect.height -= (-rect.y);
            rect.y = 0;
        }
        // check bottom right point
        int r = rect.x + rect.width;
        int b = rect.y + rect.height;

        if (r > w)
            rect.width -= (r - w);
        if (b > h)
            rect.height -= (b - h);

        // note: rect.width,rect.height can be <= 0
    }
}

Related

  1. fitRectangle(Rectangle rect, Rectangle target)
  2. fitRectangles(Dimension imageSize, Dimension size)
  3. fitRectInRect(Rectangle rect, Rectangle bounds)
  4. fits(Rectangle r)