Java 将JPanel导出到图像

Java 将JPanel导出到图像,java,swing,png,jpanel,bufferedimage,Java,Swing,Png,Jpanel,Bufferedimage,所以我一直在尝试将我在JPanel上绘制的图像导出到一个图像中。我一直在使用这种方法: BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); paint(g); try { ImageIO.write(image, "png", new File([location goes here]

所以我一直在尝试将我在JPanel上绘制的图像导出到一个图像中。我一直在使用这种方法:

BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
paint(g);
try { ImageIO.write(image, "png", new File([location goes here]); } catch (IOException e) {}

我在我预定的位置得到了一个图像,但我得到了我的JPanel显示的压缩版本。如果我尝试导出BMP也会发生同样的情况。有没有办法从JPanel导出像素完美的图像?提前感谢。

面板需要根据其要求进行布局。如果面板还没有在屏幕上实现,它可能不会以您期望的方式呈现

以下示例假定面板未显示在屏幕上

setSize(getPreferredSize());
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
printAll(g);
g.dispose();
try { 
    ImageIO.write(image, "png", new File([location goes here]); 
} catch (IOException e) {
    e.printStackTrace();
}
您应该避免自己调用
paint
,如果在屏幕上未实现组件,它可能会引发异常,请使用
printAll

此外,如果您创建了一个资源,您应该处置它;)

已更新

我做了一个简单的例子。屏幕截图在顶部,jpeg在左侧,png在右侧

jpeg为30kb,png为320kb

我用这个来创造它

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PaintComponent {

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

    public PaintComponent() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPanel paintPane;

        public TestPane() {

            paintPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            paintPane.add(new JLabel("I'm a label"), gbc);
            paintPane.add(new JTextField("I'm a text field", 20), gbc);
            paintPane.add(new JLabel(new ImageIcon("some\pretty\picture")), gbc);

            setLayout(new BorderLayout());
            add(paintPane);

            JButton paint = new JButton("Capture");
            paint.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    BufferedImage image = new BufferedImage(paintPane.getWidth(), paintPane.getHeight(), BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = image.createGraphics();
                    paintPane.printAll(g);
                    g.dispose();
                    try {
                        ImageIO.write(image, "jpg", new File("Paint.jpg"));
                        ImageIO.write(image, "png", new File("Paint.png"));
                    } catch (IOException exp) {
                        exp.printStackTrace();
                    }
                }
            });
            add(paint, BorderLayout.SOUTH);

        }
    }
}

我会确保您实际查看的是正确的文件;)

如果您试图制作在窗口上看不到的面板图像,请退出。它将在面板上调用
doLayout()
,以确保组件正确显示

谢谢你的建议。不过,当我导出图像时,屏幕上会显示我的JPanel。此外,无论我以何种格式导出图像,它们的大小都是相同的(就内存而言)。这是意料之中的事吗?我想这并不意外。图像是你所期望的吗?是的,它只是与JPanel中显示的图像质量不一样。它对JPanel的所有内容都有效吗?例如,如果a有一个JLabel、一个JButton和另一个JPanel,它们是否都将被打印?@Jas如果您在父组件上调用
printAll
,那么是,它将打印所有子组件及其子组件