Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have this code, and i use drawPad. how to save the image that I have draw in the drawPad as png file?

Java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


public class UserInterface 
{
    public static void main(String[] args) 
        {
                JFrame projectFrame = new JFrame();
                projectFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                JPanel projectPanel = new JPanel();
                JPanel btnPanel = new JPanel();
                JButton encode = new JButton("ENCODE");
                JButton decode = new JButton("DECODE");
                
                encode.addActionListener(new ButtonListener());
                decode.addActionListener(new ButtonListener());
    
                projectFrame.setSize(600,500);
                projectFrame.setTitle("ONLINE SIGNATURE STEGANOGRAPHY");  
                projectFrame.setResizable(true);
                projectPanel.setLayout(new FlowLayout());
                
                JLabel stego = new JLabel("ONLINE SIGNATURE STEGANOGRAPHY SYSTEM");
                stego.setFont(new Font("Tahoma", Font.BOLD, 20));
                 
                projectFrame.add(projectPanel);
                projectFrame.add(btnPanel, BorderLayout.PAGE_END);
                projectPanel.add(stego);
                btnPanel.add(encode);
                btnPanel.add(decode);
                projectFrame.setVisible(true);
        }
        
        static class ButtonListener implements ActionListener   
 {         
  public void actionPerformed(ActionEvent ae) 
  {
   if(ae.getActionCommand().equals("ENCODE"))
   {
    new encodePane();
   }
   if(ae.getActionCommand().equals("DECODE"))
   {
    new decodePane();
   }
     
  }
 }
}
        

        class encodePane
        {

           public encodePane() 
            {
  final JFrame encodeFrame = new JFrame();
                Container content = encodeFrame.getContentPane();
                content.setLayout(new BorderLayout());
                JPanel encodePanel = new JPanel();
                JPanel buttonPanel = new JPanel();
                
                final PadDraw drawPad = new PadDraw();
                content.add(drawPad, BorderLayout.CENTER);
                
                JButton cancelBtn = new JButton("CANCEL");
                cancelBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                encodeFrame.dispose();
                }
                });
  
                JButton proceedBtn = new JButton("PROCEED");
                proceedBtn.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e){
                     new encode1Pane();
                     encodeFrame.dispose();
                     }   
                });  
                
                JButton clearButton = new JButton("CLEAR");
                //creates the clear button and sets the text as "Clear"
                clearButton.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                                drawPad.clear();
                        }
                });
                
                JLabel Sign = new JLabel("Sign here:", SwingConstants.CENTER);
                Sign.setVerticalAlignment(SwingConstants.TOP);
                                                              
               // proceedBtn.addActionListener(new Button1Listener());
                                
                encodeFrame.setTitle("ENCODE SECTION");
                encodeFrame.setLocationRelativeTo(null);   
                encodeFrame.setSize(600,500); 
                encodeFrame.setResizable(true);
                encodePanel.setLayout(new FlowLayout());
                
                encodePanel.add(Sign);
                                                 
                encodeFrame.add(encodePanel, BorderLayout.NORTH);
                encodeFrame.add(buttonPanel, BorderLayout.SOUTH);
                                                
                buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
                buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
                buttonPanel.add(Box.createHorizontalGlue());
                buttonPanel.add(clearButton);
                buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
                buttonPanel.add(cancelBtn);
                buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
                buttonPanel.add(proceedBtn);
                
                Sign.setFont(new Font("Serif", Font.ITALIC, 14));
                encodeFrame.setVisible(true);
 }
           
         
        /*static class Button1Listener implements ActionListener 
  {   
   public void actionPerformed(ActionEvent ae)  
   {
    if(ae.getActionCommand().equals("PROCEED"))
    {
                                    new encode1Pane();
                                }
                                                               
                        }
  }*/
       }

       class PadDraw extends JComponent{

            Image image;
            Graphics2D graphics2D;
            int currentX, currentY, oldX, oldY;

            public PadDraw(){
                setDoubleBuffered(false);
                addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e){
                        oldX = e.getX();
                        oldY = e.getY();
                    }
                });
            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    currentX = e.getX();
                    currentY = e.getY();
                    if(graphics2D!=null)
                        graphics2D.drawLine(oldX, oldY, currentX, currentY);
                    repaint();
                    oldX = currentX;
                    oldY = currentY; 
                }
            });
            }
       public void paintComponent(Graphics g){
           if(image==null){
               image = createImage(getSize().width, getSize().height);
               graphics2D = (Graphics2D)image.getGraphics();
               graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
               clear();
               
               }
           g.drawImage(image,0,0,null);
       }
       
       public void clear(){
           graphics2D.setPaint(Color.white);
           graphics2D.fillRect(0,0,getSize().width, getSize().height);
           graphics2D.setPaint(Color.black);
           repaint();
       }

}

       class encode1Pane
       {
           public encode1Pane()
           {
              final JFrame encode1Frame = new JFrame();
              JPanel Enc = new JPanel();
              JPanel enc = new JPanel();
              JPanel Btn = new JPanel();
              JButton prcd = new JButton ("ENCODE");
              JLabel sec = new JLabel ("Your message");
              final JTextField secText = new JTextField ("");
              secText.setPreferredSize(new Dimension (500, 100));
              JLabel name = new JLabel ("Set the file name");
              final JTextField name2 = new JTextField ();
              name2.setPreferredSize(new Dimension (200, 20));
              JButton cncl = new JButton ("CANCEL");
              cncl.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                new encodePane();
                encode1Frame.dispose();
                }
                });
              
                            
              encode1Frame.setTitle("ENCODE SECTION");
              encode1Frame.setLocationRelativeTo(null);   
              encode1Frame.setSize(600,500);
              encode1Frame.setResizable(true);
                            
              Enc.add(sec);
              Enc.add(secText);
              secText.setBounds(30, 50, 200, 25);
              enc.add(name);
              enc.add(name2);
              name2.setBounds(30, 50, 200, 25);
              encode1Frame.add(Btn, BorderLayout.SOUTH);
              encode1Frame.add(Enc, BorderLayout.NORTH);
              encode1Frame.add(enc, BorderLayout.CENTER);
              encode1Frame.setVisible(true);   
                            
              Btn.setLayout(new BoxLayout(Btn, BoxLayout.LINE_AXIS));
              Btn.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
              Btn.add(Box.createHorizontalGlue());
              Btn.add(cncl);
              Btn.add(Box.createRigidArea(new Dimension(10, 0)));
              Btn.add(prcd);
                
              
              sec.setFont(new Font("Serif", Font.ITALIC, 14));
              name.setFont(new Font("Serif", Font.ITALIC, 14));
              //cncl.addActionListener(new Button2Listener());
              prcd.addActionListener(new Button2Listener());
               
       }
           
           static class Button2Listener implements ActionListener 
  {   
   public void actionPerformed(ActionEvent ae)  
   {
    if(ae.getActionCommand().equals("Proceed"))
    {
      //new encodeOutput();
    }    
   }
                 }
       }

            
        class decodePane 
            {
               public decodePane() 
                {
                    JFrame decodeFrame = new JFrame();
                    decodeFrame.add(new UploadImage());
                           
                }}
Posted
Comments
lindam88 8-Mar-13 21:54pm    
only .svg were available, :((

Use File > Save > Save Image to save the current view as an image file to your computer's hard drive. When you save an image, a Save dialog box appears and you can locate a folder on your computer to save the image to, just as you would for any document you might save. The image is saved with all visible placemarks, borders, or other Layer information visible in the 3D viewer.
 
Share this answer
 
You have to save the graphis object.
Here is an example
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900