Making round corners of an Image in Java

An image looks better with round corners and luckily can be easily done using java api.
Here is the code :

 public static BufferedImage makeRoundedCorner(BufferedImage image, int r)
    {
        BufferedImage output = new BufferedImage(r, r, 2);
        Graphics2D g2 = output.createGraphics();
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new java.awt.geom.Ellipse2D.Double(0.0D, 0.0D, r, r));
        g2.setComposite(AlphaComposite.SrcAtop);
        g2.drawImage(image.getScaledInstance(r, r, 4), 0, 0, null);
        g2.dispose();
        return output;
    }

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *