Rectangle r1 is on the right of Rectangle r2 - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Rectangle r1 is on the right of Rectangle r2

Demo Code


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

public class Main {
    /**//w  w  w . j av  a 2  s  .  c  om
     * r1 is on the right of r2
     * Pre: the areas are not overlapped
     */
    public static boolean isRight(Rectangle r1, Rectangle r2, int margin) {
        if (sameRow(r1, r2, margin) && r1.getMaxX() > r2.getMaxX())
            return true;
        else
            return false;
    }

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

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

Related Tutorials