概要

Windowの形を図形で切り抜きした場合に生じる縁のジャギーをソフトクリッピング効果でなめらかに変更します。

サンプルコード

int width = image.getWidth();
int height = image.getHeight();
Shape shape = new RoundRectangle2D.Float(0f, 0f, width / 2f, height / 2f, 50f, 50f);

BufferedImage clippedImage = makeClippedImage(image, shape);

JWindow window = new JWindow();
window.setBackground(new Color(0x0, true));
window.getContentPane().add(makePanel(clippedImage));
window.pack();
window.setLocationRelativeTo(((AbstractButton) e.getSource()).getRootPane());
window.setVisible(true);
// ...
private static BufferedImage makeClippedImage(BufferedImage source, Shape shape) {
  int width = source.getWidth();
  int height = source.getHeight();

  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = image.createGraphics();
  // g2.setComposite(AlphaComposite.Clear);
  // g2.fillRect(0, 0, width, height);

  g2.setComposite(AlphaComposite.Src);
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  // g2.setColor(Color.WHITE);
  g2.fill(shape);

  g2.setComposite(AlphaComposite.SrcAtop);
  g2.drawImage(source, 0, 0, null);
  g2.dispose();

  return image;
}
view all

解説

参考リンク

コメント