Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * A collection of small utilities for component management and reflection handling.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import javax.swing.*;
import java.awt.*;

public class Main {
    public static Point arrangeWithin(final Shape shapeToArrange, final Rectangle window, final int arrangement,
            final int padding) {
        return arrangeWithin(shapeToArrange, window, arrangement, new Insets(padding, padding, padding, padding));
    }

    public static Point arrangeWithin(final Shape shapeToArrange, final Rectangle window, final int arrangement,
            Insets padding) {
        if (shapeToArrange == null)
            throw new IllegalArgumentException("Parameter 'shapeToArrange' must not be null!");
        if (window == null)
            throw new IllegalArgumentException("Parameter 'window' must not be null!");
        if (padding == null)
            padding = new Insets(0, 0, 0, 0);

        final Rectangle bounds = shapeToArrange.getBounds();

        switch (arrangement) {
        case SwingConstants.NORTH:
            return new Point((window.width - bounds.width) / 2, padding.top);
        case SwingConstants.NORTH_EAST:
            return new Point(window.width - padding.right, padding.top);
        case SwingConstants.EAST:
            return new Point(window.width - padding.right, (window.height - bounds.height) / 2);
        case SwingConstants.SOUTH_EAST:
            return new Point(window.width - padding.right, window.height - padding.bottom);
        case SwingConstants.SOUTH:
            return new Point((window.width - bounds.width) / 2, window.height - padding.bottom);
        case SwingConstants.SOUTH_WEST:
            return new Point(padding.left, window.height - padding.bottom);
        case SwingConstants.WEST:
            return new Point(padding.left, (window.height - bounds.height) / 2);
        case SwingConstants.NORTH_WEST:
            return new Point(padding.left, padding.top);
        case SwingConstants.CENTER:
            return new Point((window.width - bounds.width) / 2, (window.height - bounds.height) / 2);
        default:
            throw new IllegalArgumentException("Illegal arrangement key, expected one of the SwingConstants keys");
        }
    }
}