File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes anti-aliased image clipping Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply Bookmark "anti-aliased image clipping" Watch "anti-aliased image clipping" New topic
Author

anti-aliased image clipping

Eric Wagner
Greenhorn

Joined: May 19, 2005
Posts: 1
I could really use some help with this.. I am completely stuck!

I want to take a square BufferedImage (created from a jpeg)
and clip it with an inscribed circle, such that only a circular image
cutout remains (and the exterior of the circle is transparent).
However, I need to do this with anti-aliasing so that the edge of the
circle is smooth.

I have tried the following two methods, but neither one produces
the correct result:

//simply returns the original image
private static BufferedImage cutoutCircle(BufferedImage img, int w, int h) {
Graphics2D g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Shape circle = new java.awt.geom.Ellipse2D.Double(0,0,w,h);
g2d.setClip(circle);
return img;
}

//cuts out circle but not anti-aliased
private static BufferedImage cutoutCircle(BufferedImage orig, int w, int h) {
BufferedImage bimg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bimg.createGraphics(); //this is blank (transparent)
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Shape circle = new java.awt.geom.Ellipse2D.Double(0,0,w,h);
g2d.setClip(circle);
g2d.drawImage(orig, 0, 0, null);
return bimg;
}
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: anti-aliased image clipping
 
Similar Threads
JTextPane to BufferedImage
How draw background (behind image) in only one part, and draw buttons/components?
BufferedImage to Icon - have I done this in the correct way?
AffineTransform - please help me out
Add image to JPanel