point On JavaFX Rectangle - Java JavaFX

Java examples for JavaFX:Rectangle

Description

point On JavaFX Rectangle

Demo Code


import javafx.geometry.Point2D;
import javafx.scene.shape.Rectangle;

public class Main{
    /**/* w  ww  . j a  va  2s.  c  o  m*/
     * @param rec The rectangle.
     * @param dir The direction.
     * @return The coordinates on the rectangle for the direction you asked.
     */
    public static Point2D pointOnRectangle(Rectangle rec, Direction dir) {
        if (rec == null) {
            throw new IllegalArgumentException("Rectangle can't be null.");
        }
        if (dir == null) {
            throw new IllegalArgumentException("Direction can't be null.");
        }
        double centerX;
        double centerY;
        switch (dir) {
        case CENTER:
            centerX = rec.getX() + (rec.getWidth() / 2);
            centerY = rec.getY() + (rec.getHeight() / 2);
            return new Point2D(centerX, centerY);
        case TOP:
            centerX = rec.getX() + (rec.getWidth() / 2);
            centerY = rec.getY();
            return new Point2D(centerX, centerY);
        case BOTTOM:
            centerX = rec.getX() + (rec.getWidth() / 2);
            centerY = rec.getY() + rec.getHeight();
            return new Point2D(centerX, centerY);
        case LEFT:
            centerX = rec.getX();
            centerY = rec.getY() + (rec.getHeight() / 2);
            return new Point2D(centerX, centerY);
        case RIGHT:
            centerX = rec.getX() + rec.getWidth();
            centerY = rec.getY() + (rec.getHeight() / 2);
            return new Point2D(centerX, centerY);
        case TOP_LEFT:
            centerX = rec.getX();
            centerY = rec.getY();
            return new Point2D(centerX, centerY);
        case TOP_RIGHT:
            centerX = rec.getX() + rec.getWidth();
            centerY = rec.getY();
            return new Point2D(centerX, centerY);
        case BOTTOM_RIGHT:
            centerX = rec.getX() + rec.getWidth();
            centerY = rec.getY() + rec.getHeight();
            return new Point2D(centerX, centerY);
        case BOTTOM_LEFT:
            centerX = rec.getX();
            centerY = rec.getY() + rec.getHeight();
            return new Point2D(centerX, centerY);
        default:
            throw new IllegalArgumentException("For the direction " + dir
                    + " there's no point on the rectangle.");
        }
    }
}

Related Tutorials