Saving canvas

E

exylum

Hi. I have a problem with saving canvas to file. All over net there
are faqs on how to save a NEW canvas to file (after drawing on it). I
have a canvas, what should i do in order to save what's currently on
it to a file?
 
K

Knute Johnson

exylum said:
Hi. I have a problem with saving canvas to file. All over net there
are faqs on how to save a NEW canvas to file (after drawing on it). I
have a canvas, what should i do in order to save what's currently on
it to a file?

Do you want to save a canvas or some sort of image to the file? Canvas
is Serializable so you can write it to a file using ObjectOutputStream.
That may or may not allow you to recreate what was drawn on the
Canvas. If what you want is an image saved to a file, you should draw
on a BufferedImage and save that to the file using ImageIO. You can
display the BufferedImage on your Canvas.
 
E

exylum

Do you want to save a canvas or some sort of image to the file? Canvas
is Serializable so you can write it to a file using ObjectOutputStream.
That may or may not allow you to recreate what was drawn on the
Canvas. If what you want is an image saved to a file, you should draw
on a BufferedImage and save that to the file using ImageIO. You can
display the BufferedImage on your Canvas.

I've noticed a lot of examples in the web on how to save bufferedimage
to file, which, in answer to Your question, is my goal. I just wanted
to find out if there is any way of saving the image directly from
canvas. Thank You for Your answer.
 
R

Roedy Green

Hi. I have a problem with saving canvas to file. All over net there
are faqs on how to save a NEW canvas to file (after drawing on it). I
have a canvas, what should i do in order to save what's currently on
it to a file?
see http://mindprod.com/jgloss/jpegencoder.html
http://mindprod.com/jgloss/pngencoder.html
http://mindprod.com/jgloss/gifencoder.html
Also just save the data you used to generate the image using a
DataOutputStream of ObjectOutputStream. See
http://mindprod.com/applet/fileio.html for sample code.
For complete sample code that does this see
http://mindprod.com/products.html#MASKER

You will don't literally save the bits being viewed on a Canvas --
it's bits are in proprietary video-card format in the hardware REGEN
buffer. You repaint the Canvas with a different sort of Graphics
object that saves the bits in a more standard format to ordinary RAM.
 
K

Knute Johnson

exylum said:
I've noticed a lot of examples in the web on how to save bufferedimage
to file, which, in answer to Your question, is my goal. I just wanted
to find out if there is any way of saving the image directly from
canvas. Thank You for Your answer.

If the only drawing on your Canvas is done in the paint() method you
could use that method to draw onto the BufferedImage. The technique
would be to get a draw Graphics for the BufferedImage and then call
paint() with that Graphics. If your drawing is going to be from user
interaction then using the BufferedImage as your drawing surface and
displaying that to the user makes more sense. Of course in the first
case above, serializing the Canvas would work too.
 
R

Roedy Green

Of course in the first
case above, serializing the Canvas would work too.
I suspect that a serialised Canvas could not be reconstituted in a
different machine. I think it will be specific to the screen hardware.
I know Image has this problem.
 
K

Knute Johnson

Roedy said:
I suspect that a serialised Canvas could not be reconstituted in a
different machine. I think it will be specific to the screen hardware.
I know Image has this problem.

In a serialized object all that gets stored are the fields. The data to
recreate the drawing would have to be stored in fields in the Canvas.
The actual drawing surface would be lost. I need to play with this some
but I've got to go to work this week. If I have time I'll post
something from the road :). Interesting possibilities though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top