Do these two rectangles overlap - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Do these two rectangles overlap

Demo Code

// All rights reserved.
//package com.java2s;
import java.awt.*;

import javax.swing.*;

public class Main {
    /**/*  w w w .j  a v  a 2s  .  co m*/
     * Utility method.  Do these two rectangles overlap?
     */
    public static boolean isOverlapping(Rectangle rec1, Rectangle rec2) {
        boolean overlapX = rectanglesOverlapping_XAxis(rec1, rec2);
        boolean overlapY = rectanglesOverlapping_YAxis(rec1, rec2);
        return (overlapX && overlapY);
    }

    /**
     * Utility method.  Do these two components overlap?
     */
    public static boolean isOverlapping(JComponent comp1, JComponent comp2) {
        return isOverlapping(comp1.getBounds(), comp2.getBounds());
    }

    /**
     * Private utility method.  Do these two rectangles overlap horizontally? <p>
     * NOTE:  This method will still return true if the two rectangles are positioned
     * one above the other even though they don't completely overlap.
     */
    private static boolean rectanglesOverlapping_XAxis(Rectangle rec1,
            Rectangle rec2) {
        boolean bOverlapping = false;
        if ((rec1 == null) || (rec2 == null)) {
            return false;
        }
        int nLeft1 = (int) (rec1.getX());
        int nRight1 = (int) (rec1.getX()) + (int) (rec1.getWidth());
        int nLeft2 = (int) (rec2.getX());
        int nRight2 = (int) (rec2.getX()) + (int) (rec2.getWidth());
        // Find the intersections. If two lines coming from a rectangle corner
        // both have intersection points then there IS overlap.
        boolean bLeft1_Intersects = false;
        boolean bRight1_Intersects = false;
        boolean bLeft2_Intersects = false;
        boolean bRight2_Intersects = false;
        // Are there intersections based on rectangle #1?
        if ((nLeft1 > nLeft2) && (nLeft1 < nRight2)) {
            bLeft1_Intersects = true;
        }
        if ((nRight1 > nLeft2) && (nRight1 < nRight2)) {
            bRight1_Intersects = true;
        }
        // Are there intersections based on rectangle #2?
        if ((nLeft2 > nLeft1) && (nLeft2 < nRight1)) {
            bLeft2_Intersects = true;
        }
        if ((nRight2 > nLeft1) && (nRight2 < nRight1)) {
            bRight2_Intersects = true;
        }
        // Do we overlap or not?
        if (bLeft1_Intersects || bLeft2_Intersects || bRight1_Intersects
                || bRight2_Intersects) {
            bOverlapping = true;
        }
        return bOverlapping;
    }

    /**
     * Private utility method.  Do these two rectangles overlap vertically? <p>
     * NOTE:  This method will still return true if the two rectangles are positioned
     * beside each other even though they don't completely overlap.
     */
    private static boolean rectanglesOverlapping_YAxis(Rectangle rec1,
            Rectangle rec2) {
        boolean bOverlapping = false;
        if ((rec1 == null) || (rec2 == null)) {
            return false;
        }
        int nTop1 = (int) (rec1.getY());
        int nBottom1 = (int) (rec1.getY()) + (int) (rec1.getHeight());
        int nTop2 = (int) (rec2.getY());
        int nBottom2 = (int) (rec2.getY()) + (int) (rec2.getHeight());
        // Find the intersections. If two lines coming from a rectangle corner
        // both have intersection points then there IS overlap.
        boolean bTop1_Intersects = false;
        boolean bBottom1_Intersects = false;
        boolean bTop2_Intersects = false;
        boolean bBottom2_Intersects = false;
        // Are there intersections based on rectangle #1?
        if ((nTop1 > nTop2) && (nTop1 < nBottom2)) {
            bTop1_Intersects = true;
        }
        if ((nBottom1 > nTop2) && (nBottom1 < nBottom2)) {
            bBottom1_Intersects = true;
        }
        // Are there intersections based on rectangle #2?
        if ((nTop2 > nTop1) && (nTop2 < nBottom1)) {
            bTop2_Intersects = true;
        }
        if ((nBottom2 > nTop1) && (nBottom2 < nBottom1)) {
            bBottom2_Intersects = true;
        }
        // Do we overlap or not?
        if (bTop1_Intersects || bTop2_Intersects || bBottom1_Intersects
                || bBottom2_Intersects) {
            bOverlapping = true;
        }
        return bOverlapping;
    }
}

Related Tutorials