title: Java - rounded corner panel with compositing in paintComponent
body: <p>From the original question (below), I am now offering a bounty for the following:</p>
  
  <p>An <code>AlphaComposite</code> based solution for rounded corners.</p>
  
  <ul>
  <li>Please demonstrate with a <code>JPanel</code>.  </li>
  <li>Corners must be completely transparent.  </li>
  <li>Must be able to support JPG painting, but still have rounded corners</li>
  <li>Must not use setClip (or any clipping)</li>
  <li>Must have decent performance</li>
  </ul>
  
  <p>Hopefully someone picks this up quick, it seems easy. </p>
  
  <p>I will also award the bounty if there is a well-explained reason why this can never be done, that others agree with.</p>
  
  <p>Here is a sample image of what I have in mind (but using<code>AlphaComposite</code>)<img src="https://i.stack.imgur.com/Fi5gG.jpg" alt="enter image description here"></p>
  
  <hr>
  
  <p><em>Original question</em></p>
  
  <p>I've been trying to figure out a way to do rounded corners using compositing, very similar to <a href="https://stackoverflow.com/questions/7603400/how-to-make-a-rounded-corner-image-in-java">How to make a rounded corner image in Java</a> or <a href="http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html" rel="nofollow noreferrer">http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html</a>.</p>
  
  <p>However, my attempts without an intermediate BufferedImage don't work - the rounded destination composite apparently doesn't affect the source.  I've tried different things but nothing works.  Should be getting a rounded red rectangle, instead I'm getting a square one.</p>
  
  <p>So, I have two questions, really:</p>
  
  <p>1) Is there a way to make this work?</p>
  
  <p>2) Will an intermediate image actually generate better performance?</p>
  
  <p>SSCCE: </p>
  
  <p>the test panel <strong>TPanel</strong></p>
  
  <pre><code>import java.awt.AlphaComposite;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  
  import javax.swing.JLabel;
  
  public class TPanel extends JLabel {
  int w = 300;
  int h = 200;
  
  public TPanel() {
      setOpaque(false);
      setPreferredSize(new Dimension(w, h));
          setMaximumSize(new Dimension(w, h));
          setMinimumSize(new Dimension(w, h));
  }
  
  @Override
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g.create();
  
      // Yellow is the clipped area.
      g2d.setColor(Color.yellow);
      g2d.fillRoundRect(0, 0, w, h, 20, 20);
      g2d.setComposite(AlphaComposite.Src);
  
      // Red simulates the image.
      g2d.setColor(Color.red);
      g2d.setComposite(AlphaComposite.SrcAtop);
  
      g2d.fillRect(0, 0, w, h);
      }
  }
  </code></pre>
  
  <p>and its <strong>Sandbox</strong></p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.FlowLayout;
  
  import javax.swing.JFrame;
  
  public class Sandbox {
  public static void main(String[] args) {
      JFrame f = new JFrame();
          f.setMinimumSize(new Dimension(800, 600));
          f.setLocationRelativeTo(null);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.setLayout(new FlowLayout());
  
          TPanel pnl = new TPanel();
          f.getContentPane().add(pnl);
  
          f.setVisible(true);
      }
  }
  </code></pre>