将JPanel导出到图像 [英] Exporting a JPanel to an image
问题描述
所以我一直在尝试将我在JPanel上绘制的图像导出到图像中。我一直在使用这种方法:
So I've been trying to export an image that I've drawn on a JPanel into an image. I've been using this method:
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导出的像素完美图像?提前感谢。
I get an image in my intended location but I get a compressed version of what my JPanel shows. The same happens if I try to export a BMP as well. Is there a way to get a pixel-perfect image exported from the JPanel? Thanks in advance.
推荐答案
面板需要根据需要进行布局它的要求。如果面板尚未在屏幕上实现,它可能无法按预期的方式呈现
The panel needs to be laid out based on it's requirements. If the panel hasn't being realized on the screen yet, it may not render the way you expect it do
以下示例假定面板没有显示在屏幕上...
The following example assumes that the panel has not being displayed on the screen...
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
You should avoid calling paint
yourself, it can throw an exception if the component has not being realized on the screen, instead, use printAll
另外,如果你创建一个资源,你应该处理它;)
Also, if your create a resource, you should dispose of it ;)
更新
我做了这个简单的例子。屏幕截图位于顶部,jpeg位于左侧,png位于右侧。
I did this quick example. Screen shoot on top, jpeg on left, png on right.
jpeg为30kb,png为320kb
jpeg is 30kb and png is 320kb
我用它来创建它...
I used this to create it...
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);
}
}
}
我会确保你实际上正在查看正确的文件;)
I would make sure that you are actually looking at the correct files ;)
这篇关于将JPanel导出到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!