• Post Reply Bookmark Topic Watch Topic
  • New Topic

save the image for the paint program  RSS feed

 
Miko Jing
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i having problem with save the draw image from my paint program.. after i save and i put it at desktop, when i open saved file of the draw it show me blank file .png.. any idea how to make it works?

this my drawwingArea java




And this is my buttonPanel java

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You open a writer and close it. You need to write the image data to the stream before closing it.
 
Miko Jing
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's




 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, is the problem now solved? ImageIO is indeed the quickest and easiest way to read and write images.

Note, in your starting post: a Writer is not suitable for writing an image file.

There are two kinds of interfaces and classes for I/O in Java: Streams (input and output streams) and Readers / Writers.

Streams are for reading and writing binary data (bytes). Image files, such as JPG and PNG files, are binary files.

Readers and Writers are for reading and writing text (characters, strings). They are a layer on top of streams, to convert bytes from and to characters by using a character encoding such as UTF-8 or ISO-8859-1.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I had to edit some of your post because the long lines make it difficult to read; you can see how it should be done
 
Miko Jing
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is still in same as the one that i updated.. but i have problem, the save function is works as png. once i saved the paint/drawing that i drawn, and i open back the file i saved, its empty..
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what exactly the problem is here. I think that you are saving the image before the paint(g2)
has been performed. Maybe one of our Swing experts could step in to clarify this.

There may be a way around. But first of all: this 'drawingArea', is that a JPanel?
If so, then an alternative is: create a BufferedImage, right from the start, draw
everything that must be drawn into that BI, and in your paintComponent() of 'drawingArea',
all you have to do is drawing this BI.
In that case, saving the image is simply writing this BI using ImageIO.write().

Greetings,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!