Java examples for 2D Graphics:Rectangle
Calculates a rectangle into two points, where as the points are handled as the diagonal vector.
//package com.java2s; import java.awt.Point; import java.awt.Rectangle; public class Main { /**//from w w w . ja v a 2s .com * Calculates a rectangle into two points, where as the points are handled as the diagonal vector. * However, the coordinates are adjusted correctly when p1,p2 are mixed up wrongly * * @param p1 Top left edge * @param p2 Bottom right edge * @return */ public static Rectangle rectFromPoints(Point p1, Point p2) { // find the top left point of the rectangle int x = p1.x < p2.x ? p1.x : p2.x; int y = p1.y < p2.y ? p1.y : p2.y; return new Rectangle(x, y, Math.abs(p1.x - p2.x), Math.abs(p1.y - p2.y)); } }