以两种方式获取BufferedImage

时间:2014-09-16 08:55:06

标签: java swing

我很好奇两个摆动BufferedImage从给定和可见组件中获取方法。 在某种程度上,我将(Component.paintAll)组件绘制到BufferedImage,在第二种方式中绘制图像(Graphics.drawImage)。

它的行为方式有趣:

  1. 在绘制时会产生空灰色图像(方法saveBufferedImageByDraw
  2. 在绘画上会产生适当的组件图像(方法saveBufferedImageByPaint
  3. 对于简短的解释,我将不胜感激。

    以下是示例源代码(要运行,您需要设置正确的OUTPUT_DIRECTORY):

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.GraphicsEnvironment;
    import java.awt.HeadlessException;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class SaveTest extends JFrame {
    
        private static final String PAINT_STATUS = "Paint status : ";
    
        private static final String DRAW_STATUS = "Draw status : ";
    
        private static final File OUTPUT_DIRECTORY = new File(".....");
    
        private JLabel messageDraw;
    
        private JLabel messagePaint;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new SaveTest();
                }
            });
        }
    
    
        public SaveTest() throws HeadlessException {
            super();
            initialize();
        }
    
        private void initialize() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            messageDraw = new JLabel(DRAW_STATUS);
            messagePaint = new JLabel(PAINT_STATUS);
            JButton button = new JButton("Create images");
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    create(SaveTest.this);
                }
            });
    
            JPanel messagesPane = new JPanel();
            messagesPane.setBorder(BorderFactory.createTitledBorder("Save statuses"));
            messagesPane.setPreferredSize(new Dimension(400, 100));
    
            messagesPane.setLayout(new BoxLayout(messagesPane, BoxLayout.PAGE_AXIS));
            messagesPane.add(Box.createHorizontalGlue());
            messagesPane.add(messageDraw);
            messagesPane.add(messagePaint);
    
            JPanel buttonsPane = new JPanel();
            buttonsPane.setLayout(new BoxLayout(buttonsPane, BoxLayout.LINE_AXIS));
            buttonsPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            buttonsPane.add(button);
    
    
            Container contentPane = getContentPane();
            contentPane.add(messagesPane, BorderLayout.CENTER);
            contentPane.add(buttonsPane, BorderLayout.PAGE_END);
    
            this.pack();
            this.setVisible(true);
        }
    
    
        /**
         * Creates the image of current component.
         *
         * @param component the component
         */
        public void create(Component component) {
            if (component != null && component.isVisible()
                    && !GraphicsEnvironment.isHeadless()) {
    
                try {
                    saveBufferedImageByPaint(component);
                    messagePaint.setText(PAINT_STATUS + "OK");
                } catch (Exception e) {
                    messagePaint.setText(PAINT_STATUS + "ERROR : " + e.getMessage());
                }
                try {
                    saveBufferedImageByDraw(component);
                    messageDraw.setText(DRAW_STATUS + "OK");
                } catch (Exception e) {
                    messageDraw.setText(DRAW_STATUS + "ERROR : " + e.getMessage());
                }
            } else {
                messagePaint.setText(PAINT_STATUS + "ERROR : invalid component state");
                messageDraw.setText(DRAW_STATUS + "ERROR : invalid component state");
            }
        }
    
        private void saveBufferedImageByDraw(Component component) {
            BufferedImage image = (BufferedImage) component.createImage(component.getWidth(), component.getHeight());
            Graphics2D g2 = image.createGraphics();
            g2.drawImage(image, 0, 0, component);
            g2.dispose();
            saveImage(image, "DRAW");
        }
    
        private void saveBufferedImageByPaint(Component component) {
            BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = image.createGraphics();     
            component.printAll(g2);
            g2.dispose();
    
            saveImage(image, "PAINT");
        }
    
    
        public static void saveImage(BufferedImage image, String prefix) {
            try {
                File f = new File(OUTPUT_DIRECTORY, prefix + ".png");
                ImageIO.write(image, "PNG", f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

0 个答案:

没有答案