center Resize JavaFX Rectangle - Java JavaFX

Java examples for JavaFX:Rectangle

Description

center Resize JavaFX Rectangle

Demo Code


//package com.java2s;
import static java.util.Objects.requireNonNull;

import javafx.geometry.Rectangle2D;

public class Main {
    public static Rectangle2D centerResize(Rectangle2D bounds,
            double horizontalDelta, double verticalDelta) {
        requireNonNull(bounds, "bounds is null");

        double w = bounds.getWidth() + horizontalDelta;
        if (w < 0) {
            w = 0.0d;//from   ww w  .  j  ava  2s . c  o  m
        }

        double h = bounds.getHeight() + verticalDelta;
        if (h < 0) {
            h = 0.0d;
        }

        final double centerX = bounds.getMinX() + bounds.getWidth() * .5d;
        final double centerY = bounds.getMinY() + bounds.getHeight() * .5d;

        return new Rectangle2D(centerX - w * .5d, centerY - h * .5d, w, h);
    }
}

Related Tutorials