Re: Correcting error of write Image to file
bH wrote:
Thanks for your hint, but a problem error
is shown below.
I made an attempt to replace what I have originally with
your suggestion but it produces the error found below.
I was following the tutorial from:
http://java.sun.com/docs/books/tutorial/2d/images/saveimage.html
bH
import java.awt.image.BufferedImage;
.......
BufferedImage bi = null;
.......
try {
BufferedImage bi = GifModifWithTransparentBackgrnd;
// retrieve image
File outputfile = new File("images/cosmobX.gif");
ImageIO.write(bi, "gif", outputfile);
} catch (IOException e) {}
1 error found:
File: C:\Documents and Settings\bH\Desktop\
OpaqueToTransparent.java [line: 40]
Error: C:\Documents and Settings\bH\OpaqueToTransparent.java:40:
incompatible types
found : java.awt.Image
required: java.awt.image.BufferedImage
Well, yeah.
There are two problems with that. First, and the one that caused the compiler
to choke, is that you completely disregarded the type incompatibility. Java is
not a language where you just toss things into the program and magically they
figure out what you mean. You have to follow the rules.
One of the most fundamental, basic rules in Java is that you cannot cast
references between incompatible types. One type must be a supertype of the
other. If you are casting to a supertype, you are automatically fine, because
every instance of a subtype /is-an/ instance of its supertype already. But
NOT every instance of a supertype belongs to a particular subtype.
To cast "down" from supertype to subtype, you have to explicitly cast the
change with a "(<type>)" cast operator. This "downcast" always risks a
'ClassCastException', so you must either prevent it or catch it.
You left out the cast operator, and completely neglected to handle a possible
'ClassCastException'.
The other problem is that your (misnamed) 'GifModifWithTransparentBackgrnd'
might never be set to the correct image. It's hard to say with the
fragmentary snippet you share.
--
Lew