Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes save drawing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "save drawing" Watch "save drawing" New topic
Author

save drawing

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i have a program that draws stuff. rectangles,ovals, lines, on a JPanel. is there an easy way to save said drawing as a .gif or even a .bmp ?


SCJP
Visit my download page
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
You can paint the JPanel onto a BufferedImage and then use ImageIO to save the buffered image.

Screen Image does all this for you.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

thanks ill check it out
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i am still working on this but many thanks to Rob. i now have a menu with open and save as MenuItems.i use a FileChooser to display the open and save dialogs. the open works fine and im sure the save will be easy also. but i am wondering about exceptions, in particular IllegalArgumentException which ImageIO claims to throw. i tried to use the following code to open a .txt file and got no exception. it didnt display the file either.


i noticed the dialogs had choice of "all files" only. i imagine there is a way to restrict file extensions, but handling the IllegalArgumentException seems easier.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Randall Twede wrote:i noticed the dialogs had choice of "all files" only. i imagine there is a way to restrict file extensions, but handling the IllegalArgumentException seems easier.


Read the API for JFIleChooser and follow the link to the tutorial on How to Use File Choosers where you will find examples for Filtering the List of Files.

luck, db
There are no new questions, but there may be new answers.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i will probably check that out just to learn. i figured out what i was doing wrong though. ImageIO throw the IllegalArgument exception if the input is null. if the file is one it cant read the it RETURNS null. so instead of catching an exception i had to see if the BufferedImage was null.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

this is kind of off the topic, but now i am having a problem with JOptionPane. another program i wrote had this line which works fine:
JOptionPane.showMessageDialog( mainWindow,"Example program", "About this Program", JOptionPane.PLAIN_MESSAGE );
now i am trying this which displays a blank modal dialog with title Error which i cant close
String message = "This program can only open image files, like gif or jpg";
JOptionPane.showMessageDialog( this, message, "Error", JOptionPane.ERROR_MESSAGE );

i dont see what i am doing different to cause this
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

... displays a blank modal dialog with title Error which i cant close


To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
 
 
subject: save drawing
 
Similar Threads
save and load JPEG image from disk
AWT objectives for the exam
K J Reddy..Pls suggest me
2d Scrolling
To qualify for the drawing,