且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JPanel转换为图像

更新时间:2023-12-05 11:15:28

从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.