DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005
    Posts
    1

    anti-aliased image clipping

    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.

    Also, I need the resulting BufferedImage to contain the anti-aliasing
    itself, independent of the graphics context in which it happens to be
    rendered.

    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;
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Just a short comment:

    Code:
    //simply returns the original image
    private static BufferedImage cutoutCircle(BufferedImage img, int w, int h) { 
      Graphics2D g2d = img.createGraphics();
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASI NG,
      RenderingHints.VALUE_ANTIALIAS_ON);
      Shape circle = new java.awt.geom.Ellipse2D.Double(0,0,w,h);
      g2d.setClip(circle);
      return img;
    }
    I cannot see what else this method should be able to return...
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links