Saving Image object to disk

hello guys,

i have this trouble in writing image object to disk. i am having an applet where i have declared an image object , i draw a figure in this image, so far so good. then i have created a button 'save to disk' which calls the method saveImage() upon being clicked. this method is required to save image object (of size around 800 X 600) to disk as jpeg file, how do i go about doing it?

here's the parts of code that are in connection with this problem:

publicclass man7extends Appletimplements ActionListener,MouseListener,MouseMotionListener{

.

.

.

Image img;

Graphics imgGraphics;

. . .

publicvoid actionPerformed(ActionEvent evt){

if(evt.getSource()==draw){// drawing the image for first time

.

.

img = createImage(xSize, ySize);

imgGraphics = img.getGraphics();

drawMyImage();

.

.

}

if(evt.getSource()==save){

saveImage();

showStstus("saved");

}

}

publicvoid update(Graphics g){

g.drawImage(img, 0, 0,this);// update() called many times as per user's actions

.

.

}

void drawMyImage(){

..

for( .. )//some loops to draw each pixel

imgGraphics.drawLine(x,y,x,y);

..

}

public saveImage(){

/*

here's where i need help, to write the object img to disk as a jpeg file

*/

}

}// end of class man7

i have tried many stuff none of which worked so i have not posted any (unsuccessful ) code in saveImage() method

please help,

thanks in advance

Ishwar

[2676 byte] By [Kulkarnia] at [2007-11-27 2:39:20]
# 1

http://java.sun.com/docs/books/tutorial/essential/io/fileio.html

Read up on java io and be careful when you say you want to save an object vs you want to save an image file because they are two totally different things. An image is a file and an object is an instance of a class.

Oh and another thing in an applet you do not have access to a users file systems due to security.

Message was edited by:

alien08o8

alien08o8a at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 2
Image writing: a one liner.javax.imageio.ImageIO.write(bufferedImage, "jpeg", file);Image writing from an applet: be prepared to cry bitter, salty tears...
DrLaszloJamfa at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 3
But i am not using BufferedImage image here i am using instance of class 'Image', so how can i convert my image object to BufferedImage type, so that i can use the method u mentioned?
Kulkarnia at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 4
I would use a BufferedImage throughout. To create one, don't createImage(w, h), instead:bufferedImage = getGraphicsConfiguration().createCompatibleImage(w, h);
DrLaszloJamfa at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 5

> Oh and another thing in an applet you do not have

> access to a users file systems due to security.

>

u seem to be right, 'cause when i tried writing it using buffered image ( i dont remember the exact procedure i followed as it was long ago) i got some "access denied ("java.io.FilePermission C:\myImage.jpg delete) " error .. .

so what do i do to solve this problem ?

Kulkarnia at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 6
> so what do i do to solve this problem ?Either use a signed applet, or abandon applet and write a normal application -- applet sux.
DrLaszloJamfa at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 7

made the following changes :

BufferedImage img; // in declaration

..

..

img = getGraphicsConfiguration().createCompatibleImage(xSize, ySize);

imgGraphics = img.getGraphics(); // in 'actionperformed()' method

..

and wrote this in saveImage() method

void saveImage(){

try {

File file = new File("C:\\newimage.jpg");

javax.imageio.ImageIO.write(img, "jpeg", file);

}

catch (Exception e) {

showStatus(e.getMessage());

}

}

the stutus message still shows that "access denied (java.io.fielpermission.. ) " error .. :-(

how do i make a signed applet, just give me a link if u have i will try to figure out the way to do it..

thanks for so quick and sincere responses

ishwar

Kulkarnia at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 8
I've never written an applet, myself, but there is a forum dedicated to signed applets: http://forum.java.sun.com/forum.jspa?forumID=63Again, I'm not sure applets are *ever* the way to go.
DrLaszloJamfa at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 9

> Again, I'm not sure applets are *ever* the way to go.

They certainly aren't the way to go for most of the people posting questions about them here. I don't generally approve of burning books but if we could just get hold of all those Java books published about 1999 with "Chapter 1: Your First Applet" in the table of contents you might find me looking the other way for a moment.

DrClapa at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...
# 10
You need to change the security policy on the computer running the applet. This involves making a new policy file. (There are extensive tutorials and tech tips about this.)However, I have to agree with other posters: give up on applets and change what you are doing into an
Tavea at 2007-7-12 3:01:16 > top of Java-index,Java Essentials,New To Java...