Start New Discussion within our Software Development Community

Hi,

I wrote this program and I was wondering if there was a way to save the picture I'm creating using the graphics class. Here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class paint extends JFrame implements ActionListener
{
  private int pointCount = 0;

  private Point points[] = new Point[1000];

  JButton btn = new JButton("Sign in the above space and click here");

  public paint()
  {
    super("Painter");

    getContentPane().add(btn,
                         BorderLayout.SOUTH);
    btn.addActionListener(this);

    addMouseMotionListener(
     new MouseMotionAdapter() {
       public void mouseDragged(MouseEvent event)
       {
         if(pointCount < points.length)
         {
           points[pointCount] = event.getPoint();
           ++pointCount;
           repaint();
         }
       }
     }
    );

    setSize(300,150);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getSource() == btn)
    {
      /*
        I WANT TO BE ABLE TO SAVE THE SIGNATURE AS A .BMP, .GIF, OR .JPG FILE  
      */
    }
  }

  public void paint(Graphics g)
  {
    super.paint(g);
    for(int i=0; i < points.length && points[i] != null; i++)
      g.fillRect(points[i].x,points[i].y,4,4);
  }
  public static void main(String [] args)
  {
    paint app = new paint();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

If yo run it, you'll see that you can "paint" things. I was wondering if there's a way to save what I'm painting as a picture file.

Thank you for your help in advanced,
C++

This article has been dead for over six months. Start a new discussion instead.