【问题标题】:how to save panel as image in swing?如何在摇摆中将面板保存为图像?
【发布时间】:2012-07-01 15:26:32
【问题描述】:

您好,我想将包含标签和按钮等组件的面板转换为图像文件。

我已经完成了以下代码。图像已保存。但面板的内容不可见或未保存。谁能告诉我如何保存面板及其组件。

代码:

package PanelToImage;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class sample extends JPanel {

public JPanel firstpanel;
public JPanel secondpanel;
JLabel label1, label2;
JButton button1, button2;

public sample() {
    firstpanel = new JPanel();
    firstpanel.setSize(400,300); 
    firstpanel.setBackground(Color.RED);
    secondpanel = new JPanel();
    secondpanel.setBackground(Color.GREEN);
    secondpanel.setSize(400,300); 

    label1 = new JLabel("label1");
    label2 = new JLabel("label2");
    button1 = new JButton("button1");
    button2 = new JButton("button2");

    firstpanel.add(label1);
    firstpanel.add(button1);

    secondpanel.add(label2);
    secondpanel.add(button2);

    saveImage(firstpanel);

    add(firstpanel);

    // add(secondpanel);
}

public static void main(String args[]) {

    JFrame frame = new JFrame();
    sample sam = new sample();
    frame.setContentPane(sam);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);

}

private void saveImage(JPanel panel) {
    BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
    panel.paint(img.getGraphics());
    try {
        ImageIO.write(img, "png", new File("E://Screen.png"));
        System.out.println("panel saved as image");

    } catch (Exception e) {
        System.out.println("panel not saved" + e.getMessage());
    }
}
}

【问题讨论】:

  • 查看 ComponentImageCapture.java 以显示可见组件 - 向下滚动以查看 另请参阅 和 Rob Camick 的课程,它从尚未显示的组件。在显示之前渲染组件的其他一些技巧可以在Why does the JTable header not appear in the image? 中看到
  • 在我看来,您为要创建的文件提供了错误的Path。由于您的程序不知道什么是Drive E,因此将创建的新File 必须相对于.class File 引用Relative Path,如..\..\E:\Screen.png,使其分为两个级别起来然后联系Drive E,这样的事情就可以了。使用我的答案或@Alberto 的答案创建的图像是在 .class 文件旁边创建的。

标签: java image swing panel graphics2d


【解决方案1】:

此代码对我有用(在JFrame 中):

Container c = getContentPane();
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(im.getGraphics());
ImageIO.write(im, "PNG", new File("shot.png"));

也许您使用过自定义面板。如果为 true,请尝试在面板的 paint 方法的开头添加 super.paint(g)

编辑:您必须在显示框架后调用saveImage

public static void main(String args[]) {
    ...
    frame.setSize(400, 300);
    sam.saveImage(sam.firstpanel);
}

编辑 2:这是保存的图像(因为布局不大,但证明它应该可以工作):

我在main 中调用了saveImage 作为最后一次调用,并使用了用户目录(new File("Screen.png"))中的一个文件,正如nIcE cOw 所说。

【讨论】:

  • 我没有使用绘画方法。我只是使用面板并使用它添加组件。但是将面板保存到图像时组件不可见。
  • 查看我的编辑。您必须在显示框架后调用 saveImage。
  • 查看 ComponentImageCapture.java 以显示可见组件 - 向下滚动查看 另请参阅 和 Rob Camick 的课程,该课程从尚未显示的组件。在显示之前渲染组件的其他一些技巧可以在Why does the JTable header not appear in the image? 中看到
  • 抱歉,我无法将面板转换为带有组件的图像。问题未解决。
  • 但是你有没有试过在setVisible之后打电话给saveImage?有效吗?
【解决方案2】:

在这里试试这个示例程序,而不是使用getGraphics() 似乎你必须使用createGraphics() 来实现你即将制作的BufferedImage

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class SnapshotExample
{
    private JPanel contentPane;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Snapshot Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBackground(Color.WHITE);
        JLabel label = new JLabel("This JLabel will display"
                        + " itself on the SNAPSHOT", JLabel.CENTER);
        contentPane.add(label);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        makePanelImage(contentPane);
    }

    private void makePanelImage(Component panel)
    {
        Dimension size = panel.getSize();
        BufferedImage image = new BufferedImage(
                    size.width, size.height 
                              , BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        panel.paint(g2);
        try
        {
            ImageIO.write(image, "png", new File("snapshot.png"));
            System.out.println("Panel saved as Image.");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {           
                new SnapshotExample().displayGUI();
            }
        });
    }
}

【讨论】:

  • 查看 ComponentImageCapture.java 以显示可见组件 - 向下滚动以查看 另请参阅 和 Rob Camick 的课程,它从尚未显示的组件。在显示之前渲染组件的其他一些技巧可以在Why does the JTable header not appear in the image? 中看到
  • @AndrewThompson :感谢您提供这些有价值的链接,我已经看到了最后一个问题,在我刚开始学习Painting in Swing 之前有一次,尽管当时整个事情都超出了我的想象。但这次希望我能够从这些例子中学到很多东西:-)。再次感谢。干杯
猜你喜欢
  • 1970-01-01
  • 2012-11-20
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 2011-08-10
  • 2011-08-01
相关资源
最近更新 更多