Shifts the position of the container rectangle to ensure that it contains the contained rectangle. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Shifts the position of the container rectangle to ensure that it contains the contained rectangle.

Demo Code

// Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import static com.threerings.geom.Log.log;

public class Main{
    /**//from   ww  w  . ja  va 2  s .  com
     * Shifts the position of the <code>tainer</code> rectangle to ensure that it contains the
     * <code>tained</code> rectangle. The <code>tainer</code> rectangle must be larger than or
     * equal to the size of the <code>tained</code> rectangle.
     */
    public static void shiftToContain(Rectangle tainer, Rectangle tained) {
        if (tained.x < tainer.x) {
            tainer.x = tained.x;
        }
        if (tained.y < tainer.y) {
            tainer.y = tained.y;
        }
        if (tained.x + tained.width > tainer.x + tainer.width) {
            tainer.x = tained.x - (tainer.width - tained.width);
        }
        if (tained.y + tained.height > tainer.y + tainer.height) {
            tainer.y = tained.y - (tainer.height - tained.height);
        }
    }
}

Related Tutorials