Mouse Drawn


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Mouse Drawn

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Posts
    2

    Mouse Drawn

    Hi
    I have developed a code through that I can do free hand drawing on JPanel.
    Now any one can tell me how to save this image as .BMP or .JPEG format.

    The MoseDrawn Code is following
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;



    class PointDet {
    Point p;
    Color c;
    boolean f;

    PointDet (Point p, Color c, boolean f) {
    this.p = p;
    this.c = c;
    this.f = f;
    }

    Point getPoint () {
    return p;
    }

    Color getColor () {
    return c;
    }

    boolean getFlag () {
    return f;
    }
    }


    class MouseDrawn extends JFrame {
    MouseDrawn () {
    setSize (800, 600);
    setTitle ("Niladri's Free Hand Drawing");

    getContentPane ().add (new ImageExPanel ());


    addWindowListener (new WindowAdapter () {
    public void windowClosing (WindowEvent e) {
    System.exit (0);
    }
    });

    show ();
    }

    public static void main (String [] args) {
    new MouseDrawn();
    }
    }

    /* MouseDrawn.java */
    class ImageExPanel extends JPanel implements MouseMotionListener, ActionListener {
    Vector v = new Vector ();
    Color c = Color.black;

    JButton colorB = new JButton ("Color");

    public void actionPerformed (ActionEvent e) {
    Object s = e.getSource ();

    if (s == colorB) {
    JColorChooser chooser = new JColorChooser ();
    c = chooser.showDialog (null, "Choose Color", Color.black);
    }
    }

    ImageExPanel () {
    addMouseListener (new MouseAdapter () {
    public void mousePressed (MouseEvent e) {
    v.add (new PointDet (e.getPoint (), c, true));
    repaint ();
    }
    });

    add (colorB);

    colorB.addActionListener (this);
    setBackground (Color.white);
    addMouseMotionListener (this);
    }

    public void paintComponent (Graphics g) {
    super.paintComponent (g);

    for (int i = 1; i < v.size (); ++i) {
    PointDet pdimin1 = (PointDet)v.get (i - 1);
    PointDet pdi = (PointDet)v.get (i);
    g.setColor (pdi.getColor ());

    if (!pdi.getFlag ())
    g.drawLine (pdimin1.getPoint ().x, pdimin1.getPoint ().y, pdi.getPoint ().x , pdi.getPoint ().y);
    else
    g.drawRect (pdi.getPoint ().x, pdi.getPoint ().y, 1, 1);
    }
    }

    public void mouseDragged (MouseEvent e) {
    v.add (new PointDet (e.getPoint (), c, false));
    repaint ();
    }

    public void mouseMoved (MouseEvent e) {}
    }
    Last edited by niladridey; 01-08-2006 at 03:56 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I have got the code for Image -> jpg, is that interesting ?
    eschew obfuscation

  3. #3
    Join Date
    Jan 2006
    Posts
    2
    Yes that will do also.
    Plz send the code to nearestniladri2003@gmail.com

    Thanks in advance.

Similar Threads

  1. Replies: 10
    Last Post: 09-26-2005, 08:43 PM
  2. Capturing Mouse Movements outside of screen resolution
    By Chris Eastwood in forum VB Classic
    Replies: 4
    Last Post: 03-28-2002, 01:26 PM
  3. Mouse Movement Playback
    By Eugene in forum VB Classic
    Replies: 4
    Last Post: 12-19-2000, 12:52 PM
  4. Mouse Movement Playback
    By Eugene in forum VB Classic
    Replies: 0
    Last Post: 12-19-2000, 11:13 AM
  5. Mouse Hook
    By Michael Culley in forum VB Classic
    Replies: 0
    Last Post: 12-10-2000, 07:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center