[英]Save JFrame content as a .png
I am currently developing a 2D game in Java (I use a JFrame).我目前正在 Java 开发一款 2D 游戏(我使用 JFrame)。 I would like to, when a certain button is pressed, save the content of the frame and store it in an image.
我想在按下某个按钮时保存框架的内容并将其存储在图像中。 For the moment, I use the following code:
目前,我使用以下代码:
Robot robot = new Robot();
Rectangle screenSize = new Rectangle(display.getFrame().getX() + 3, display.getFrame().getY() + 26, handler.getWidth(), handler.getHeight());
BufferedImage screenShot = robot.createScreenCapture(screenSize); ImageIO.write(screenShot,File("D:\\Programming\\First2DGame\\res\\textures\\Screenshot.png"));
The problem is that this takes a screenshot of a portion of the screen , and not of the frame itself .问题是这截取了屏幕的一部分,而不是框架本身。 This means that if, for example, I move my window to the bottom right of the screen, I receive a discord notification and then I press my button, the notification will be present in the screenshot.
这意味着,例如,如果我将我的 window 移到屏幕的右下角,我会收到 discord 通知,然后我按下我的按钮,该通知将出现在屏幕截图中。
I have already searched a bit, a I tried this code:我已经搜索了一下,我试过这段代码:
try
{
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
frame.paint(graphics2D);
ImageIO.write(image,"jpeg", new File("D:\\Programming\\First2DGame\\res\\textures\\Screenshot.png"));
}
catch(Exception e)
{
e.printStackTrace;
}
}
protected void paintComponent(Graphics g)
{
g.drawRect(50,50,50,50);
}
but the result is a white window:但结果是白色 window:
Do you perhaps know how to create a screenshot of only the frame ?您是否知道如何创建仅框架的屏幕截图?
I think the example code you showed is pretty much the right...我认为您显示的示例代码几乎是正确的...
Can you test it with the code below?你能用下面的代码测试一下吗? :D If you remove
contentPane.printAll(g2d)
As you showed, only the white window :D 如果您删除
contentPane.printAll(g2d)
如您所示,只有白色 window
public class ImageSaveTest extends JFrame {
public static void main(String[] args) {
new ImageSaveTest();
}
public ImageSaveTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setLayout(new GridBagLayout());
JLabel label = new JLabel("Hello! StackOverflow(Press Enter)");
label.setFont(new Font("Default", Font.BOLD, 15));
panel.add(label);
add(panel);
setVisible(true);
pack();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
try {
Container contentPane = getContentPane();
BufferedImage image = new BufferedImage(contentPane.getWidth(), contentPane.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
contentPane.printAll(g2d);
g2d.dispose();
// replace this path to your image
ImageIO.write(image, "jpeg", new File("Your Path/Print.png"));
} catch (IOException ex) {
// Exception Handling
}
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.