how to save panel as image in swing?

前端 未结 2 1241
青春惊慌失措
青春惊慌失措 2020-12-06 17:22

Hi i want to convert panel which contains components like label and buttons to image file.

I have done the following code. The image was saved. but the content of th

相关标签:
2条回答
  • 2020-12-06 17:39

    Tthis code works for me (in the 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"));
    

    Maybe you have used custom panels. If true, try to add super.paint(g) at the beginning of the paint methods of your panels.

    EDIT: You have to call saveImage after display the frame:

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

    EDIT 2: This is the saved image (is little because the layout, but is the proof that it should work):

    enter image description here

    I called the saveImage as last call in the main, and used a file in the user dir (new File("Screen.png")) as nIcE cOw said.

    0 讨论(0)
  • 2020-12-06 17:52

    Here try this example program, instead of using getGraphics() seems like you have to use createGraphics() for the BufferedImage you are about to make.

    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();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题