我希望截取程序中当前发生的情况的屏幕截图,以便在暂停菜单的后台使用。我找到了代码:
BufferedImage x = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
它确实可以截图并且效果很好,问题是它还包括我的窗口边框,这绝对不应该包括在内。我在网上窥探过,但没有看到一个功能可以截取边框内的屏幕截图。或者,找到顶部、底部和侧面边框的厚度也可以,但我还没有找到任何相关信息。
最好的方法是什么?
最佳答案
您应该 try catch 内容,而不是捕获帧边界,例如......
JRootPane rootPane = frame.getRootPane();
Rectangle bounds = new Rectangle(rootPane.getSize());
bounds.setLocation(rootPane.getLocationOnScreen());
BufferedImage contentsImage = bot.createScreenCapture(bounds);
这也会捕获菜单栏,如果您只想要物理内容,您应该使用 frame.getContentPane()
而不是 frame.getRootPane()
例如...
原始框架
捕获结果(全帧/根 Pane )
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();
}
}
}
}
奇怪的是,您几乎可以将这种方法用于任何您想要的组件...
关于java - 捕获无边框屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393643/