Graphics#setClip(Shape)
正常に動作します(camickrがすでに提案しているように):
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ScreenShotClipTest {
private JComponent makeUI() {
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(new JTree()));
p.add(new JButton(new AbstractAction("screenshot") {
@Override public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
Window f = SwingUtilities.getWindowAncestor(b);
try {
BufferedImage ss = new Robot().createScreenCapture(f.getBounds());
int w = ss.getWidth(null), h = ss.getHeight(null);
BufferedImage bi = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.setClip(new RoundRectangle2D.Float(0,0,w,h,64,64));
g.drawImage(ss, 0, 0, null);
g.dispose();
ImageIO.write(bi, "png", new File("screenshot.png"));
} catch(Exception ex) {
ex.printStackTrace();
}
}
}), BorderLayout.SOUTH);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ScreenShotClipTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}