Returns middle point for the specified rectangle. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Returns middle point for the specified rectangle.

Demo Code

/*//from ww  w  .  jav a 2 s . com
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

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

public class Main {
    /**
     * Returns middle point for the specified rectangle.
     *
     * @param rectangle
     *            rectangle to process
     * @return middle point for the specified rectangle
     */
    public static Point middle(final Rectangle rectangle) {
        return new Point(rectangle.x + rectangle.width / 2, rectangle.y
                + rectangle.height / 2);
    }

    /**
     * Returns middle point between the specified points.
     *
     * @param p1
     *            first point
     * @param p2
     *            second point
     * @return middle point between the specified points
     */
    public static Point middle(final Point p1, final Point p2) {
        return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
    }

    /**
     * Returns middle point between the specified points.
     *
     * @param x1
     *            first point X coordinate
     * @param y1
     *            first point Y coordinate
     * @param x2
     *            second point X coordinate
     * @param y2
     *            second point Y coordinate
     * @return middle point between the specified points
     */
    public static Point middle(final int x1, final int y1, final int x2,
            final int y2) {
        return new Point((x1 + x2) / 2, (y1 + y2) / 2);
    }
}

Related Tutorials