get Rectangle Centering Point - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

get Rectangle Centering Point

Demo Code


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JDialog;
import org.apache.log4j.Logger;

public class Main{
    private final static Logger LOG = Logger.getLogger(DialogUtil.class);
    private static Point getCenteringPoint(Rectangle area, Dimension size) {
        Point base = area.getLocation();
        LOG.debug(String.format("Base: %s", base));
        LOG.debug(String.format("dx = %s / 2 - %s / 2", area.getWidth(),
                size.getWidth()));//  w  w  w  . ja v  a2 s  .  c  o m
        int dx = (int) (area.getWidth() / 2 - size.getWidth() / 2);
        LOG.debug(String.format("dy = %s / 2 - %s / 2", area.getHeight(),
                size.getHeight()));
        int dy = (int) (area.getHeight() / 2 - size.getHeight() / 2);
        base.translate(dx, dy);
        LOG.debug(String.format("dx: %s - dy: %s - translated: %s", dx, dy,
                base));
        return base;
    }
}

Related Tutorials