|
-
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.
Similar Threads
-
Replies: 10
Last Post: 09-26-2005, 08:43 PM
-
By Chris Eastwood in forum VB Classic
Replies: 4
Last Post: 03-28-2002, 01:26 PM
-
By Eugene in forum VB Classic
Replies: 4
Last Post: 12-19-2000, 12:52 PM
-
By Eugene in forum VB Classic
Replies: 0
Last Post: 12-19-2000, 11:13 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks