대신 프레임 경계를 캡처, 당신은 당신은 단지 물리적 내용을 원하는 경우, 당신은해야뿐만 아니라
JRootPane rootPane = frame.getRootPane();
Rectangle bounds = new Rectangle(rootPane.getSize());
bounds.setLocation(rootPane.getLocationOnScreen());
BufferedImage contentsImage = bot.createScreenCapture(bounds);
이 캡처합니다 ... 예를 들어, 대신 내용을 캡처 메뉴 표시 줄을 시도해야 대신 예를 들어 frame.getRootPane()
의 frame.getContentPane()
...
원래 프레임을 사용
캡처 결과 (전체 프레임/루트 창)
이상하게
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ScreenShot {
public static void main(String[] args) {
new ScreenShot();
}
public ScreenShot() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
JButton capture = new JButton("Snap shot");
capture.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Robot bot = new Robot();
BufferedImage frameImage = bot.createScreenCapture(frame.getBounds());
JRootPane rootPane = frame.getRootPane();
Rectangle bounds = new Rectangle(rootPane.getSize());
bounds.setLocation(rootPane.getLocationOnScreen());
BufferedImage contentsImage = bot.createScreenCapture(bounds);
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(new JLabel(new ImageIcon(frameImage)));
panel.add(new JLabel(new ImageIcon(contentsImage)));
JOptionPane.showMessageDialog(frame, panel);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
frame.add(capture, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
try {
BufferedImage img = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\Ponies\\sillydash-small.png"));
JLabel label = new JLabel(new ImageIcon(img));
add(label);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
, 당신은 당신이 원하는 거의 모든 구성 요소에 대해이 방법을 사용할 수 있습니다 ...
완벽하게 작동했습니다! 나는 크기와 위치에 대해 rootPane을 사용하는 것을 잊어 버렸습니다. 모든 것을 포함하고 국경 간의 모든 것을 포함하는 어떤 객체가 있어야한다는 것을 알고있었습니다. 어느 것을 기억하지 못했습니다! 감사! – Nathan
도움이 되니 기쁩니다 ... – MadProgrammer