Here you can find the source of getMaximalRectangle(Rectangle r1, Rectangle r2)
public static Rectangle getMaximalRectangle(Rectangle r1, Rectangle r2)
//package com.java2s; /*// w w w. j a v a2 s . c om * Copyright (c) 2015 Instituto Politecnico de Tomar. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * */ import java.awt.Rectangle; public class Main { public static Rectangle getMaximalRectangle(Rectangle r1, Rectangle r2) { int x0 = Math.min(r1.x, r2.x); int y0 = Math.min(r1.y, r2.y); int w = Math.max(r1.x + r1.width, r2.x + r2.width) - x0; int h = Math.max(r1.y + r1.height, r2.y + r2.height) - y0; return new Rectangle(x0, y0, w, h); } public static int min(int a, int b, int c, int d) { return Math.min(Math.min(Math.min(a, b), c), d); } public static int max(int a, int b, int c, int d) { return Math.max(Math.max(Math.max(a, b), c), d); } }