Re: How do I write an ImageIcon object into a file on my computer?
RedGrittyBrick wrote:
Knute Johnson wrote:
Maybe you need to explain it to someone with ADD as if I were a 10-year
old. Try it that way.
How do you write to a new file anything, like String content or Image
content or Excel spreadsheet content.. anything at all, into a new
file? How do you do it?
Phil
String data is written with a Writer, Object data is written with an
ObjectOutputStream and byte data is written with an OutputStream.
Where is your data coming from?
The web?
A file on a disk?
A Java program?
Is it a byte stream?
I shouldn't really jump in and answer the question for the OP but there
seems to be an impedance mismatch in the communications channel.
Oh please speak English!
AIUI the OP wants
a) Use HTTP to obtain an image from a website somewhere.
e.g. http://foo.example.com/img/frog.jpg
b) Write the obtained data to a local disk file in such a way that it
constitutes a valid image file that can be read by common image viewing
and editing software. e.g. to c:\frog.jpg as a valid JPEG image file.
No I'm sorry that is not it. This is what I want to do.
I have an application, IconMaker.java. Its function is to take an
image from HTTP and display it within my JPanel as an ImageIcon type,
that is, an "icon". It's not really an icon as Windows understands it,
and furthermore, it is not a real file on the system anywhere, so I
want to take that ImageIcon and somehow "magically" convert it into an
actual Windows .ico Icon.
This method in IconMaker creates the ImageIcon:
/**
* Returns an ImageIcon, or null if the path was invalid
*
* @param urlPath the String url path
* @return a new ImageIcon object or null
* @see ImageIcon
* @link
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/32d030195835a490/5ff39713bea3dc71?lnk=st&q=how+to+resize+an+ImageIcon+java&rnum=1&hl=en#5ff39713bea3dc71
*/
protected ImageIcon createImageIcon(String urlPath) {
try {
setURL(new URL(urlPath));
ImageIcon icon = new ImageIcon(this.getURL(), "Your new
icon");
// SIMPLE VERSION OF RESIZING
icon.setImage(icon.getImage().getScaledInstance(16, 16,
Image.SCALE_AREA_AVERAGING));
return icon;
} catch (Exception e) {
System.out.println("Unable to create icon:" +
e.getMessage());
return null;
}
}
I don't think he cares how the data is temporarily represented in Java.
Followups set to comp.lang.java.help only (dropping .programmer)