Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest this method that takes an image and produces an image and keeps the image IO outside:</p> <p><strong>Edit:</strong> I finally managed to make Java2D soft-clip the graphics with the help of <a href="http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html" rel="noreferrer">Java 2D Trickery: Soft Clipping</a> by Chris Campbell. Sadly, this isn't something Java2D supports out of the box with some <code>RenderhingHint</code>.</p> <pre><code>public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); // This is what we want, but it only does hard-clipping, i.e. aliasing // g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip shape // in fully opaque white with antialiasing enabled... g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius)); // ... then compositing the image on top, // using the white shape from above as alpha source g2.setComposite(AlphaComposite.SrcAtop); g2.drawImage(image, 0, 0, null); g2.dispose(); return output; } </code></pre> <p>Here's a test driver:</p> <pre><code>public static void main(String[] args) throws IOException { BufferedImage icon = ImageIO.read(new File("icon.png")); BufferedImage rounded = makeRoundedCorner(icon, 20); ImageIO.write(rounded, "png", new File("icon.rounded.png")); } </code></pre> <p>This it what the input/output of the above method looks like:</p> <p>Input:</p> <p><img src="https://i.stack.imgur.com/bDgUl.png" alt="input image"></p> <p>Ugly, jagged output with <code>setClip()</code>:</p> <p><img src="https://i.stack.imgur.com/IUIwl.png" alt="jagged with setclip"></p> <p>Nice, smooth output with composite trick:</p> <p><img src="https://i.stack.imgur.com/pBa57.png" alt="smooth with composite trick"></p> <p>Close up of the corners on gray background (<code>setClip()</code> obviously left, composite right):</p> <p><img src="https://i.stack.imgur.com/BX3J3.png" alt="closeup corners on gray bacjground"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload