将 JPanel 转换为图像

发布于 2024-08-03 20:08:38 字数 67 浏览 2 评论 0原文

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

谢谢,

杰夫

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

thanks,

Jeff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心如荒岛 2024-08-10 20:08:38

从 BufferedImage 中,您可以创建一个图形对象,您可以使用它来调用 JPanel 上的绘制,如下所示:

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;
}

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

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.

×纯※雪 2024-08-10 20:08:38

基本上我正在构建一个组件
需要写入图像
但不显示

ScreenImage 解释了如何执行您想要的操作。


ScreenImage.java 的相关部分(稍作编辑)。 layoutComponent 强制所有按钮出现在图像中。

/**
 * @return Renders argument onto a new BufferedImage
 */
public BufferedImage createImage(JPanel panel, int width, int height) {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    panel.setSize(width, height); // or panel.getPreferedSize()
    layoutComponent(panel);
    panel.print(g);
    g.dispose();
    return bi;
}

private void layoutComponent(Component component) {
    synchronized (component.getTreeLock()) {
        component.doLayout();

        if (component instanceof Container) {
            for (Component child : ((Container) component).getComponents()) {
                layoutComponent(child);
            }
        }
    }
}

Basically I'm building a component
that needs to get written to an image
but not displayed

ScreenImage explains how to do what you want.


Relevant section of ScreenImage.java (slightly edited). layoutComponent forces all buttons appear in the image.

/**
 * @return Renders argument onto a new BufferedImage
 */
public BufferedImage createImage(JPanel panel, int width, int height) {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    panel.setSize(width, height); // or panel.getPreferedSize()
    layoutComponent(panel);
    panel.print(g);
    g.dispose();
    return bi;
}

private void layoutComponent(Component component) {
    synchronized (component.getTreeLock()) {
        component.doLayout();

        if (component instanceof Container) {
            for (Component child : ((Container) component).getComponents()) {
                layoutComponent(child);
            }
        }
    }
}
素年丶 2024-08-10 20:08:38

Tom的回答基本上是正确的,但不建议直接调用paint(),因为它是同步调用,可能会中断swing线程上的其他操作。我们应该使用 print() 而不是使用 paint()

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.print(g);
    g.dispose();
    return bi;
}

The answer from Tom is basically correct, but invoke paint() directly is not recommended, as it is a synchronous call and can interrupt with other operation on the swing thread. Instead of using paint(), we should use print() instead

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.print(g);
    g.dispose();
    return bi;
}
司马昭之心 2024-08-10 20:08:38

看一下BasicTableUI。单元格渲染器绘制在图像上而不显示,然后绘制在可见的表格组件上。

Take a look at BasicTableUI. The cell renderer is drawn on image without showing and then drawn on visible table component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文