import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;

public final class BrokenBoundsClip {

    public static final int SIZE = 100;

    public static void main(String[] args) {
        BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_RGB);

        Graphics2D g2d = bi.createGraphics();
        g2d.setColor(Color.RED);
        g2d.fillRect(SIZE / 2, SIZE / 2, SIZE / 2, SIZE / 2);
        g2d.setClip(-bi.getWidth() / 2, -bi.getHeight() / 2, SIZE, SIZE);

        g2d.copyArea(bi.getWidth() / 2, bi.getHeight() / 2,
                     Integer.MAX_VALUE , Integer.MAX_VALUE ,
                     -bi.getWidth() / 2, -bi.getHeight() / 2);
        int actual = bi.getRGB(0, 0);
        int expected = Color.RED.getRGB();
        if (actual != expected) {
            System.err.println("Actual:   " + Integer.toHexString(actual));
            System.err.println("Expected: " + Integer.toHexString(expected));
            throw new RuntimeException("Wrong color");
        }
    }
}
