move Rectangle To Fit - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

move Rectangle To Fit

Demo Code


//package com.java2s;

import java.awt.*;

public class Main {
    public static void moveToFit(final Rectangle rectangle,
            final Rectangle container, Insets padding) {
        Insets insets = padding != null ? padding : new Insets(0, 0, 0, 0);

        Rectangle move = new Rectangle(rectangle.x - insets.left,
                rectangle.y - insets.top, rectangle.width + insets.left
                        + insets.right, rectangle.height + insets.top
                        + insets.bottom);

        if (move.getMaxX() > container.getMaxX()) {
            move.x = (int) container.getMaxX() - move.width;
        }// w  w  w  .  j  ava  2s.c o  m

        if (move.getMinX() < container.getMinX()) {
            move.x = (int) container.getMinX();
        }

        if (move.getMaxY() > container.getMaxY()) {
            move.y = (int) container.getMaxY() - move.height;
        }

        if (move.getMinY() < container.getMinY()) {
            move.y = (int) container.getMinY();
        }

        rectangle.x = move.x + insets.left;
        rectangle.y = move.y + insets.right;
        rectangle.width = move.width - insets.left - insets.right;
        rectangle.height = move.height - insets.top - insets.bottom;
    }
}

Related Tutorials