Java Rectangle Create Parse newRectangle(int x1, int y1, int x2, int y2)

Here you can find the source of newRectangle(int x1, int y1, int x2, int y2)

Description

creates a rectangle out of two points.

License

Open Source License

Parameter

Parameter Description
x1 x coordinate of first point
y1 y coordinate of first point
x2 x coordinate of second point
y2 y coordinate of second point

Return

the smallest Rectangle containing both points

Declaration

public static Rectangle newRectangle(int x1, int y1, int x2, int y2) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

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

public class Main {
    /**/*from ww w .j  a  va  2  s. c  om*/
     * creates a rectangle out of two points.
     * @param x1 x coordinate of first point
     * @param y1 y coordinate of first point
     * @param x2 x coordinate of second point
     * @param y2 y coordinate of second point
     * @return the smallest Rectangle containing both points
     */
    public static Rectangle newRectangle(int x1, int y1, int x2, int y2) {
        final int x = Math.min(x1, x2);
        final int y = Math.min(y1, y2);
        final int w = Math.abs(x1 - x2 + 1);
        final int h = Math.abs(y1 - y2 + 1);

        return new Rectangle(x, y, w, h);
    }

    /**
     * creates a rectangle out of two points.
     * @param p1   first point
     * @param p2   second point
     * @return the smallest Rectangle containing both points
     */
    public static Rectangle newRectangle(Point p1, Point p2) {
        return newRectangle(p1.x, p1.y, p2.x, p2.y);
    }
}

Related

  1. decodeBounds(String sBounds)
  2. getRectangle(Map bounds)
  3. getRectangle(Preferences prefs, String sName, int iDefaultWidth, int iDefaultHeight)
  4. getRectangle(Properties prop, final String key, final Rectangle def)
  5. getRectangle(String rectStr)
  6. rectangleToString(Rectangle r)
  7. rectangleToString(Rectangle r)
  8. rectangleToString(Rectangle rectangle)
  9. storeBounds(String name, Window window)