import java.awt.AWTException; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.Insets; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JRootPane; public class SimpleTransFrame2 extends JFrame { private Robot robot; private BufferedImage image; public SimpleTransFrame2() { super(); } public SimpleTransFrame2(String title) { super(title); init(); } public SimpleTransFrame2(GraphicsConfiguration gc) { super(gc); init(); } public SimpleTransFrame2(String title, GraphicsConfiguration gc) { super(title, gc); init(); } private void init() { try { robot = new Robot(); } catch (AWTException ex) { ex.printStackTrace(); return; } setRootPane(new ImageRootPane()); JComponent content = (JComponent)getContentPane(); content.setOpaque(false); } public void addNotify() { super.addNotify(); Rectangle bounds = getBounds(); Insets insets = getInsets(); bounds = new Rectangle(bounds.x + insets.left, bounds.y + insets.top, bounds.width - insets.left - insets.right, bounds.height - insets.top - insets.bottom); image = robot.createScreenCapture(bounds); } class ImageRootPane extends JRootPane { public void paintComponent(Graphics g) { g.drawImage(image, 0, 0, this); } } public static void main(String[] args) { JFrame frame = new SimpleTransFrame2("Capture Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new FlowLayout()); frame.getContentPane().add(new JButton("Button")); frame.setBounds(100, 100, 400, 200); frame.setVisible(true); } }