Crop the image to a smooth oval - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage

Description

Crop the image to a smooth oval

Demo Code


//package com.java2s;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import java.awt.geom.Ellipse2D;

import java.awt.image.BufferedImage;

public class Main {
    /** Crop the image to a smooth oval
     * //  w  ww  .j  a v  a  2s. c o m
     * @param input
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage makeOval(BufferedImage input, int width,
            int height) {
        BufferedImage output = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = output.createGraphics();

        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.WHITE);
        g.fill(new Ellipse2D.Float(0, 0, width, height));

        g.setComposite(AlphaComposite.SrcAtop);
        g.drawImage(input, 0, 0, null);

        g.dispose();

        return output;
    }
}

Related Tutorials