将JPanel保存为.jpg / .png图像

时间:2013-03-22 07:52:02

标签: java swing jpanel bufferedimage paintcomponent

我创建了一个Java Applet,用于保存用户在JPanel中绘制的图像。当我保存在输出图像上时,我只有JPanel的背景,用户绘制的线条消失了。任何解决方法的提示?在这个代码行已经由程序绘制,但错误仍然存​​在。

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

public class MarkPen extends Applet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener {

     private int x = 0;
     private int y = 0;
     private int prevx  = 0;
     private int prevy  = 0;

     private JPanel drawPanel = new JPanel();
     private JButton saveButton = new JButton("SaveImage");

     public void init()
     {

        setLayout(new BorderLayout());

        drawPanel.setBackground(Color.red);
        drawPanel.addMouseMotionListener(this);
        drawPanel.addMouseListener(this);
        drawPanel.add(new Comp());
        drawPanel.setDoubleBuffered(true);
        add(drawPanel, "Center");

        saveButton.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e)
                {
                 BufferedImage image = new BufferedImage(drawPanel.getWidth(), drawPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
                 Graphics2D graphics2D = image.createGraphics(); 

                 drawPanel.paint(graphics2D);
                 try{
                     ImageIO.write(image,"jpeg", new File("C:/.../Example.jpeg"));
                     }
                 catch(Exception ex){
                      ex.printStackTrace();
                     }
                 }
        });

        add(saveButton, "South");

     }

    public void setGraphicalDefaults(MouseEvent e){}

    public class Comp extends JComponent{
        public void paintComponent(Graphics g) {
            g  = drawPanel.getGraphics();
            g.setColor(Color.black);
            g.drawLine(0,0,100,100);
        }
     }

     public void mouseDragged(MouseEvent e){}
     public void mousePressed(MouseEvent e){}
     public void actionPerformed(ActionEvent e){}   
     public void mouseReleased(MouseEvent e){}
     public void mouseEntered(MouseEvent e){}
     public void mouseExited(MouseEvent e){}
     public void mouseMoved(MouseEvent e){}
     public void mouseClicked(MouseEvent e){}
     public void adjustmentValueChanged(AdjustmentEvent arg0){}

}

感谢您的帮助,我解决了它,我发布了下面的工作代码。

工作代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class MarkPen extends JApplet {

    private static final long serialVersionUID = 1L;

    public static class DrawPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private List<Point> points = new ArrayList<Point>();

        public DrawPanel() {
            setBackground(Color.WHITE);

            addMouseMotionListener(new MouseAdapter() {

                @Override
                public void mouseDragged(MouseEvent e) {
                    points.add(e.getPoint());
                    repaint();
                }
            });

            addMouseListener(new MouseAdapter(){

                @Override
                public void mousePressed(MouseEvent e){
                    Point p = null;
                    points.add(p);
             }
            });

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }

        @Override
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            Point p1 = null;
            Point p2 = null;
            g.setColor(Color.black);
            for (Point p : points) {
                p2 = p1;
                p1 = p;
                if (p1 != null && p2 != null) {
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                }
            }
        }
    }

    protected void initUI() {
        setLayout(new BorderLayout());

        JButton saveButton = new JButton("SaveImage");
        JButton clearButton = new JButton("Clear");
        final DrawPanel drawPanel = new DrawPanel();
        JPanel buttonsPanel = new JPanel();

        buttonsPanel.setLayout(new BorderLayout());
        setSize(1000, 305);
        add(drawPanel, "Center");

        saveButton.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e)
                {
                 BufferedImage image = new BufferedImage(drawPanel.getWidth(), drawPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
                 Graphics2D graphics2D = image.createGraphics(); 

                 drawPanel.paint(graphics2D);
                 try{
                     ImageIO.write(image,"png", new File("C:/.../Desktop/Example.png"));
                     }
                 catch(Exception ex){
                      ex.printStackTrace();
                     }
                 }
        });

       clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                drawPanel.points.clear();
                repaint();
            }
        });

        buttonsPanel.add(clearButton, BorderLayout.WEST);
        buttonsPanel.add(saveButton, BorderLayout.EAST);
        add(buttonsPanel, BorderLayout.SOUTH);

        validate();
    }

    @Override
    public void init() {
        super.init();
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    initUI();
                }
            });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

  • 您的问题是使用drawPanel.getGraphics();

  • 此方法可以为Save JPanel into Image创建临时对象,而不是在Image中显示JPanel

  • getPreferredSize

  • 中覆盖public class Comp extends JComponent{drawImage

  • paintComponent

  • 在您要替换当前绘画的情况下使用super.paintComponent

例如

class Comp extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bi, 0, 0, this);
    }
}

答案 1 :(得分:1)

  

这是我第一次使用BufferedImage,我有问题了解它是如何工作的

Custom Painting ApproachesDrawOnImage示例是使用BufferedImage的示例,尽管理解其他示例也没有什么坏处。