File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saveing Graphics to files

 
hennie louw
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i had an idea to load image files into an image[] and then save the image[] by useing the ObjectOutputStream wraped in the FileOutputStrem but there is a problem, the image class does not implement the Serializable interface, if some one knows of a work around for me please help
here follows my code
/**************************************************
* CODED BY HENNIE LOUW (LIZZERD) ON 18 JULY 2001 *
**************************************************/

/************************************************************************
* What I attemted to do in this class was to make a utility that loads *
* gif Images into a Image[] and then saves it into a file useing the *
* ObejctOutputSteam into the file of your choice eg [ ball.ani ] *
************************************************************************/

import java.io.*;
import java.awt.*;
import java.applet.*;
public class SaveAnimation extends Component implements Serializable
{

Image[] loadedImage;
ImageWritingClass imageAnimation;

/*****************************************************************************
* This constructor creates a complex data type that has a complex data type *
* with the Image[] in it *
*****************************************************************************/
public SaveAnimation(String fileName, int totalNumber, String ext)
{
imageAnimation = new ImageWritingClass(fileName, totalNumber, ext);
}


/******************************************************************************
* this Constructor was medw to be use when you make an object in your applet *
* to call the loudFromFile() method *
******************************************************************************/

public SaveAnimation(){ }
/************************************************************************
* This method Saves the image[] into the file *
* NOTE you need to specify the filename and extension *
************************************************************************/

public void saveToFile(String saveFileName, String ext)
{
try
{
FileOutputStream fileOutput = new FileOutputStream(saveFileName+"."+ext);
try
{
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(imageAnimation);
objectOutput.flush();
fileOutput.close();
}
catch(IOException e){ System.out.println(e.toString()); }
}
catch(FileNotFoundException e){ System.out.println(e.toString()); }
}//end of saveToFile


/********************************************************************
* this Method reads the saved image[] file and retruns a image[] *
* NOTE you need to specify the filename and extension *
********************************************************************/

public Image[] loadFromFile(String loadFileName, String ext)
{
try
{
FileInputStream fileInput = new FileInputStream(loadFileName+"."+ext);

try
{
ObjectInputStream objectInput= new ObjectInputStream(fileInput);
try
{
imageAnimation = (ImageWritingClass)(objectInput.readObject());
}catch(ClassNotFoundException e){ System.out.println(e); }
fileInput.close();
}
catch(IOException e){System.out.println(e);}
}
catch(FileNotFoundException e){System.out.println(e);}
loadedImage = imageAnimation.getImage();
return(loadedImage);

}//end of loadAnimation

}//end of class SaveAnimation
/**************************************************
* CODED BY HENNIE LOUW (LIZZERD) ON 18 JULY 2001 *
**************************************************/

import java.awt.*;
import java.io.*;
public class ImageWritingClass extends Component implements Serializable
{

/**********************************************************************************
* The constructor loads all the frames witch iamge files into a image array *
* The file name is passed to the construtor and the total number of frames, *
* the differnt gif's that are loaded sould all have the same filename with the *
* frame number at the end starting at one *
**********************************************************************************/

Image[] imageAnimation;
MediaTracker frameTracker = new MediaTracker(this);
Toolkit imageTool;

public ImageWritingClass(String fileName, int totalNumber, String ext)
{
try
{
imageTool = getToolkit();
imageAnimation = new Image[totalNumber];
for(int frameNumber = 1; frameNumber < totalNumber; frameNumber++)
{
imageAnimation[frameNumber - 1] = imageTool.getImage(fileName+frameNumber+"."+ext);
frameTracker.addImage(imageAnimation[frameNumber -1], 0);
}
frameTracker.waitForID(0);
}catch(InterruptedException e){ System.out.println(e); }

}// End of SaveAnimation

public Image[] getImage()
{
return(imageAnimation);
}
}//end of class
 
Swamy Vatti
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using javax.swing.ImageIcon instead of Image.
ImageIcon implements serializable.
 
hennie louw
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Swamy Vatti:
Try using javax.swing.ImageIcon instead of Image.
ImageIcon implements serializable.

Thanks for the advice, this will help me not pull out my hair trying to subclass the image Class and implementing the serilaibe interface
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hennie,

I apologize for the length of this postin advance... but this is one great util!

I have played around with it and have fixed some of the problems I saw with it ( please don't be offended Hennie... most of them were not "problems" per se... just ways that I prefer to format and document my code... )

Unfortunately, I don't think using ImageIcon is the best choice... especially since you were talking about using this with applets... ImageIcon is part of Swing, so the user would have to download the Java2Plugin to run a applet using this.

Fortunately, I found a workaround( But, man, I had to strain some neurons! )... I made the ImageWritingClass not even save images, instead it saves integer arrays that represent the pixel values of the images. I used the PixelGrabber and MemoryImageSource classes from the java.awt.image package to translate images and pixels back and forth.

Some other major stuff I fixed up was adding a method that loads files from a URL instead of a filename... most browsers don't like you trying to access a file directly. ( At least the crappy version of IE4 that I have here at work didn't... ) Also, I was trying to put all this in inner classes and make it "self-contained" but for some reason, IE hated trying to load an inner class that had been serialized, so I had to take all the inner class stuff out.

This is what I have ended up with... It doesn't look great, but it actually works! I have tried to comment most of the things I have added so people can understand what is going on... if anyone has any questions, just post them...








Hope that this helps some people out! I know that it helped me out... now I have to go find something to use this in...

-Nate ( Just call me Snake... )

( Sorry... couldn't help it... I just saw "Escape From L.A." a few days ago... )
[This message has been edited by Nathan Pruett (edited July 20, 2001).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic