center In Rectangle - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

center In Rectangle

Demo Code


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

public class Main {
    public static Rectangle centerInRect(Rectangle source, Rectangle target) {
        int centerX = target.x + (int) (target.getWidth() / 2);
        int centerY = target.y + (int) (target.getHeight() / 2);
        return new Rectangle(centerX - (int) (source.getWidth() / 2),
                centerY - (int) (source.getHeight() / 2), source.width,
                source.height);//from   w  w w. j a  va 2  s  . c  o  m
    }
}

Related Tutorials