gpt4 book ai didi

java - 如何将窗口内容保存为图像

转载 作者:搜寻专家 更新时间:2023-11-01 01:29:48 24 4
gpt4 key购买 nike

我想将 JPanel 的内容捕获为图像,以便我可以完全按照用户看到的那样存储它。有没有一种相对简单的方法可以用 swing 做到这一点?

最佳答案

从这里http://forums.oracle.com/forums/thread.jspa?threadID=2156176&tstart=0

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SavePaint extends JPanel
{

public SavePaint()
{
JFrame frame = new JFrame("TheFrame");
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);

try
{
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
frame.paint(graphics2D);
ImageIO.write(image,"jpeg", new File("/home/deniz/Desktop/jmemPractice.jpeg"));
}
catch(Exception exception)
{
//code
}
}

protected void paintComponent(Graphics g)
{
g.drawRect(50,50,50,50);
}

public static void main(String[] args)
{
new SavePaint();
}
}

关于java - 如何将窗口内容保存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com