Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: How to capture signature using Canvas and save in png format?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to capture signature using Canvas and save in png format?

    I have done this, but i have problem to save the image? can someone help me?

    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());
     
                    }}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Where are you trying to write the image to a file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Quote Originally Posted by Norm View Post
    Where are you trying to write the image to a file?
    sorry, this is my new code. when i run, it save blank black image. i want to save the drawing on the canvas. please help me.

    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.colorchooser.*;
    import javax.swing.event.*;
    import java.awt.geom.Line2D;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import javax.imageio.ImageIO;
     
     
    public class PaintIt extends JFrame implements ActionListener{
     
     
     
    public BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
    //JPanel canvas = new JPanel();
     
        JPanel buttonPanel = new JPanel();
        Point lastPos = null;
        Point startPos = null;
        Point finishPos = null;
        Graphics g;
        JButton save = new JButton("Save");
        JButton cancel = new JButton("Cancel");
        JButton clear = new JButton("Clear");
        JButton proceed = new JButton("Proceed");
        JPanel canvas = new JPanel();
        int changer = 1;
        String path="";
     
        public PaintIt () {
     
     
            setLocation(100,100);
            setSize(600,500);
            setTitle("ENCODE SECTION");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            canvas.setBackground(Color.WHITE);
     
            clear.addActionListener(this);
            clear.setActionCommand("clear");
     
            save.addActionListener(this);
            save.setActionCommand("Save");
     
     
            cancel.addActionListener(this);
            cancel.setActionCommand("Cancel");
     
            proceed.addActionListener(this);
            proceed.setActionCommand("Proceed");
     
            //add buttons here
     
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
            buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
            buttonPanel.add(Box.createHorizontalGlue());
            buttonPanel.add(save);
            buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
            buttonPanel.add(clear);
            buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
            buttonPanel.add(cancel);
            buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
            buttonPanel.add(proceed);
     
            //set the look
            getContentPane().add(canvas, BorderLayout.CENTER);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            setVisible(true);
     
            g = canvas.getGraphics();
            g.setColor(Color.BLACK);
     
            canvas.addMouseMotionListener(new MouseMotionListener () {                      
                public void mouseDragged (MouseEvent m) {
                    Point p = m.getPoint() ;
                    if (changer==1){
                    g.drawLine(lastPos.x, lastPos.y, p.x, p.y) ;
                    }           
                    lastPos = p ;
     
                }   
                public void mouseMoved (MouseEvent m) {}
            });
     
            canvas.addMouseListener(new MouseListener () {
                public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
                public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
                public void mouseReleased(MouseEvent e) {
                lastPos = null; 
                finishPos = e.getPoint(); 
                startPos = null;}
                public void mouseEntered(MouseEvent e) {}
                public void mouseExited(MouseEvent e) {}
                });
     
     
        }
     
     
     
        public void actionPerformed(ActionEvent e) {
            if("clear".equals(e.getActionCommand())) {
                repaint();
            }
            if("Save".equals(e.getActionCommand())) {
                captureCanvasImage myCanvas = new captureCanvasImage();
                myCanvas.capture();
            }
            if("Cancel".equals(e.getActionCommand())) {
                dispose();
            }
            if("Proceed".equals(e.getActionCommand())) {
                new encode1Pane();
            }}
     
     
        class captureCanvasImage {
            public void capture(){
     
            Graphics g = image.createGraphics();
            canvas.paint(g);
            int w = canvas.getWidth();
            int h = canvas.getHeight();
            //BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            //g.dispose();
            try
            {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                g.dispose();
                ImageIO.write(image, "png", new File("original.png"));
            }
            catch(IOException ioe)
            {
                System.out.println("Panel write help: " + ioe.getMessage());
            }
        }
     
                }
     
     
        /*
        @Override
        public void invalidate() {     
        super.invalidate(); 
     
        this.paint(this.getGraphics()); 
        }
    */
     
        public static void main (String [] args) {
            PaintIt p = new PaintIt();
            p.setVisible(true);
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    The code does not compile because of missing class definitions.

    How does the code get what was written on the canvas so it can be written to a file?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    i can compile it. nothing problem

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    copy the source to a new folder away from the other files and try compiling it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    try this. i already done that, nothing problem. my problem is how to save the image i draw in the canvas.

     import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.colorchooser.*;
    import javax.swing.event.*;
    import java.awt.geom.Line2D;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import javax.imageio.ImageIO;
     
     
    public class PaintIt extends JFrame implements ActionListener{
     
     
     
    public BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    //JPanel canvas = new JPanel();
     
        JPanel buttonPanel = new JPanel();
        JPanel sign = new JPanel();
        JLabel signlabel = new JLabel("Sign here :");
        Point lastPos = null;
        Point startPos = null;
        Point finishPos = null;
        Graphics g;
        JButton save = new JButton("Save");
        JButton cancel = new JButton("Cancel");
        JButton clear = new JButton("Clear");
        JButton proceed = new JButton("Proceed");
        JPanel canvas = new JPanel();
        int changer = 1;
        String path="";
     
        public PaintIt () {
     
     
            setLocation(100,100);
            setSize(600,500);
            setTitle("ENCODE SECTION");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            canvas.setBackground(Color.WHITE);
     
            clear.addActionListener(this);
            clear.setActionCommand("clear");
     
            save.addActionListener(this);
            save.setActionCommand("Save");
     
     
            cancel.addActionListener(this);
            cancel.setActionCommand("Cancel");
     
            proceed.addActionListener(this);
            proceed.setActionCommand("Proceed");
     
            //add buttons here
     
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
            buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
            buttonPanel.add(Box.createHorizontalGlue());
            buttonPanel.add(save);
            buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
            buttonPanel.add(clear);
            buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
            buttonPanel.add(cancel);
            buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
            buttonPanel.add(proceed);
     
            sign.add(signlabel);
            signlabel.setFont(new Font("Serif", Font.ITALIC, 14));
     
            //set the look
            getContentPane().add(canvas, BorderLayout.CENTER);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            getContentPane().add(sign, BorderLayout.PAGE_START);
            setVisible(true);
     
            g = canvas.getGraphics();
            g.setColor(Color.BLACK);
     
            canvas.addMouseMotionListener(new MouseMotionListener () {                      
                public void mouseDragged (MouseEvent m) {
                    Point p = m.getPoint() ;
                    if (changer==1){
                    g.drawLine(lastPos.x, lastPos.y, p.x, p.y) ;
                    }           
                    lastPos = p ;
     
                }   
                public void mouseMoved (MouseEvent m) {}
            });
     
            canvas.addMouseListener(new MouseListener () {
                public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
                public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
                public void mouseReleased(MouseEvent e) {
                lastPos = null; 
                finishPos = e.getPoint(); 
                startPos = null;}
                public void mouseEntered(MouseEvent e) {}
                public void mouseExited(MouseEvent e) {}
                });
     
     
        }
     
     
     
        public void actionPerformed(ActionEvent e) {
            if("clear".equals(e.getActionCommand())) {
                repaint();
            }
            if("Save".equals(e.getActionCommand())) {
                captureCanvasImage myCanvas = new captureCanvasImage();
                myCanvas.capture();
            }
            if("Cancel".equals(e.getActionCommand())) {
                dispose();
            }
            if("Proceed".equals(e.getActionCommand())) {
                //new encode1Pane();
            }}
     
     
        class captureCanvasImage {
            public void capture(){
     
            Graphics g = image.createGraphics();
            canvas.paint(g);
            int w = image.getWidth();
            int h = image.getHeight();
            //BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            //g.dispose();
            try
            {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                g.dispose();
                ImageIO.write(image, "png", new File("original.png"));
            }
            catch(IOException ioe)
            {
                System.out.println("Panel write help: " + ioe.getMessage());
            }
        }
     
                }
     
     
        /*
        @Override
        public void invalidate() {     
        super.invalidate(); 
     
        this.paint(this.getGraphics()); 
        }
    */
     
        public static void main (String [] args) {
            PaintIt p = new PaintIt();
            p.setVisible(true);
        }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    how to save the image i draw in the canvas.
    You need to save the points used to draw the lines in some container like an arraylist so you can you use them when you want to save what was drawn on the jpanel.

    Your method of drawing the image of lines is wrong. You need to override the component's paintComponent method and draw the lines there using what you have saved in the arraylist.
    If you move the frame with the image past the side of the monitor, the drawn lines are erased. Or if you minimize and restore the window the image is gone.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    i dont get it, can you give some example?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    There are lots of code examples here on the forum. Do a search for paintComponent for example.

    Also look at the tutorial:
    http://docs.oracle.com/javase/tutori...ing/index.html
    Last edited by Norm; June 3rd, 2012 at 08:00 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    take a look at this, i have paint component. but how I can save the image?

    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;
    import javax.imageio.ImageIO;
     
     
    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);
     
               }}
     
           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());
     
                    }}

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Can make a small simple program that compiles, executes and shows the problem? The posted code has endcode and decode classes and other stuff that is not part of the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    this is the code example same as i do. how to add save function to it?

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
     
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
     
    public class Main {
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        final DrawPad drawPad = new DrawPad();
        frame.add(drawPad, BorderLayout.CENTER);
        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            drawPad.clear();
          }
        });
        frame.add(clearButton, BorderLayout.SOUTH);
        frame.setSize(280, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
     
    }
    class DrawPad extends JComponent {
      Image image;
      Graphics2D graphics2D;
      int currentX, currentY, oldX, oldY;
      public DrawPad() {
        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();
      }
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Write the image in the image variable to a file.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    is it in here
     if (image == null
    ?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Is that the variable named: image? That is where the image has been drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    i dont get it. i'm new to java. can u give example? or help me add in my code?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    The first code you posted has code in it to write an image to a file. Copy the code from that program.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    like this? but i it have error

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
     
    public class Main {
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        final DrawPad drawPad = new DrawPad();
        frame.add(drawPad, BorderLayout.CENTER);
        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            drawPad.clear();
          }
        });
        JButton save = new JButton("Save");
        save.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
                    if("Save".equals(e.getActionCommand())) {
                captureCanvasImage myCanvas = new captureCanvasImage();
                myCanvas.capture();
                    }}});
     
        frame.add(clearButton, BorderLayout.SOUTH);
        frame.setSize(280, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
     
    }
    class DrawPad extends JComponent {
      Image image;
      Graphics2D graphics2D;
      int currentX, currentY, oldX, oldY;
      public DrawPad() {
        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 captureCanvasImage {
            public void capture(){
            Graphics g = image.createGraphics();
            canvas.paint(g);
            //myCanvas = new Canvas( mBitmap );
            int w = image.getWidth();
            int h = image.getHeight();
     
            //BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            //g.dispose();
            try
            {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                g.dispose();
                ImageIO.write(image, "png", new File("original.png"));
     
            }
            catch(IOException ioe)
            {
                System.out.println("Panel write help: " + ioe.getMessage());
            }
        }
     
                }

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    it have error
    When you get errors, you need to copy and paste here the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    my error is in here. at the canvas, image n so on. getWidth, getHeight. what should i replace to that?

     public void capture(){
            Graphics g = image.createGraphics();
            canvas.paint(g);
            //myCanvas = new Canvas( mBitmap );
            int w = image.getWidth();
            int h = image.getHeight();
     
            //BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            //g.dispose();
            try
            {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                g.dispose();
                ImageIO.write(image, "png", new File("original.png"));
     
            }
            catch(IOException ioe)
            {
                System.out.println("Panel write help: " + ioe.getMessage());
            }
        }

  22. #22
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    my error is in here. at the canvas, image n so on. getWidth, getHeight. what should i replace to that?

     public void capture(){
            Graphics g = image.createGraphics();
            canvas.paint(g);
            //myCanvas = new Canvas( mBitmap );
            int w = image.getWidth();
            int h = image.getHeight();
     
            //BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            //g.dispose();
            try
            {
                BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                g.dispose();
                ImageIO.write(image, "png", new File("original.png"));
     
            }
            catch(IOException ioe)
            {
                System.out.println("Panel write help: " + ioe.getMessage());
            }
        }

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Please post the full text of the error message.

    What is the method you posted supposed to do? The image already exists in the code in post#13
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    this the error,paint.jpg

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to capture signature using Canvas and save in png format?

    Please copy and paste here the full text of the error message. Images are no good because you can not copy text from them to do a find for what is referred to in the error message.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    A screen shot of your IDE does not help me.
    Last edited by Norm; June 3rd, 2012 at 09:56 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to save Image into a package by JButton save?
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2012, 07:02 PM
  2. Need help to save Canvas content to a file
    By piulitza in forum AWT / Java Swing
    Replies: 3
    Last Post: December 27th, 2011, 11:20 AM
  3. Why does not forum allow signature?
    By hns1984 in forum The Cafe
    Replies: 0
    Last Post: October 20th, 2011, 11:04 AM
  4. Audio capture (TargetDataLine) latency
    By Pikkurotta in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 14th, 2011, 04:20 PM
  5. Call with a different signature
    By brajesh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2010, 02:05 PM