将面板保存为图像

时间:2014-08-23 10:33:19

标签: java image swing jpanel

我的面板保存为jpeg图像,但图纸不可见。 这是我的代码

BufferedImage img = new BufferedImage(painting.panel.getWidth(), painting.panel.getHeight(), BufferedImage.TYPE_INT_RGB);
   Graphics2D g1 = img.createGraphics();
   painting.panel.paintAll(g1);
   try {
        ImageIO.write(img, "jpeg", new File(fd.getDirectory()+"\\"+fd.getFile()+".jpeg"));
        System.out.println("panel saved as image");
   } catch (Exception e) {
        System.out.println("panel not saved" + e.getMessage());
   }

但是当我打开文件时,图纸不可见。

1 个答案:

答案 0 :(得分:2)

对我来说就像一个魅力(请查看下面提供的基本示例)。

常见的错误是忘记

  • 在根组件上设置大小
  • 执行绘制组件的布局(通过在根组件及其所有后代上调用doLayout()

最后,使用 paint(Graphics) 而非 paintAll(Graphics) ,如果您的组件未展示,则可能无效。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestPrint {

    private static final int MY_WIDTH = 400;
    private static final int MY_HEIGHT = 400;
    private static final int STEPS = 20;

    protected static void initUI() throws MalformedURLException {
        Random r = new Random();
        final List<Color> colors = new ArrayList<Color>();
        for (int i = 0; i < STEPS; i++) {
            colors.add(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
        }
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(java.awt.Graphics g) {
                super.paintComponent(g);
                for (int i = 0; i < STEPS; i++) {
                    g.setColor(colors.get(i));
                    g.fillRect(0 + (getWidth() / STEPS * i / 2), 0 + (getHeight() / STEPS * i / 2), getWidth() - (getWidth() / STEPS * i),
                            getHeight() - getHeight() / STEPS * i);
                }
            };

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(MY_WIDTH, MY_HEIGHT);
            }
        };
        panel.setSize(panel.getPreferredSize());
        layoutRecursively(panel);
        BufferedImage bi = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
        panel.paint(bi.getGraphics());
        File file = new File("test.png");
        try {
            ImageIO.write(bi, "png", file);
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void layoutRecursively(Component component) {
        component.doLayout();
        if (component instanceof Container) {
            Container container = (Container) component;
            for (int i = 0; i < container.getComponentCount(); i++) {
                layoutRecursively(container.getComponent(i));
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    initUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

我们得到如下图片:

enter image description here