将JPanel转换为图像 [英] Convert JPanel to image

查看:90
本文介绍了将JPanel转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将JPanel(尚未显示)转换为BufferedImage?

Is there a way to convert a JPanel (that has not yet been displayed) to a BufferedImage?

谢谢,

Jeff

推荐答案

从BufferedImage中你可以创建一个图形对象,你可以使用它来调用绘图JPanel,类似于:

From the BufferedImage you can create a graphics object, which you can use to call paint on the JPanel, something like:

public BufferedImage createImage(JPanel panel) {

    int w = panel.getWidth();
    int h = panel.getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.paint(g);
    g.dispose();
    return bi;
}

您可能需要确保先设置面板的大小。

You may need to make sure you set the size of the panel first.

这篇关于将JPanel转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆