Rectangle r1 is below Rectangle r2 - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Rectangle r1 is below Rectangle r2

Demo Code


//package com.java2s;
import java.awt.Rectangle;

public class Main {
    /**/*  www.j a  va2  s.  c  o  m*/
     * r1 is below r2
     * Pre: the areas are not overlapped
     */
    public static boolean isDown(Rectangle r1, Rectangle r2, int margin) {
        if (sameColumn(r1, r2, margin) && r1.getMaxY() > r2.getMaxY())
            return true;
        else
            return false;
    }

    /**
     * Returns true if both rectangles are in the same column, taking into account the margin
     * Pre: margin >= 0
     */
    public static boolean sameColumn(Rectangle r1, Rectangle r2, int margin) {
        if (vSharingValue(r1, r2) > margin)
            return true;
        else
            return false;
    }

    /**
     * Returns the vertical sharing value
     * If the value returned is negative, then the widgets are not in the same column
     */
    public static int vSharingValue(Rectangle r1, Rectangle r2) {
        int d2 = Math.min((int) r1.getMaxX(), (int) r2.getMaxX());
        int d1 = Math.max((int) r1.getMinX(), (int) r2.getMinX());
        int d = d2 - d1 + 1;
        return d;
    }
}

Related Tutorials