6

所以我一直在尝试将我在 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 导出像素完美的图像?提前致谢。

4

2 回答 2

11

面板需要根据其要求进行布局。如果面板尚未在屏幕上实现,则它可能无法按照您期望的方式呈现

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

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

        }
    }
}

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

于 2013-07-17T03:00:36.270 回答
2

如果您尝试制作在窗口上不可见的面板图像,请查看Screen Image。它将doLayout()在面板上调用以确保正确显示组件。

于 2013-07-17T03:13:42.603 回答