Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2007

Tip: Looking for answers? Try searching our database.

Updating an existing image (converting from java.io.File to java.awt.Image)

Thread view: 
Perma - 03 Apr 2007 15:30 GMT
Hi,
I am working on a TV application which is based on jdk 1.1.

here I draw an Image using:
Image myImage = this.getToolkit().getImage(imagePath);
(...and graphics.drawImage(myImage) in the paint() method)

The problem comes when I get an Event telling me that the image is
updated. From the event, I can get the source of the updated File (in
this case, an Image). So, I can obtain the updated File (which is an
Image) by doing:
File updatedFile = (File)updateEvent.getSource();

When I try to cast the File to an Image, I get a ClassCastException

If I try to re-instanciate the image after I get the update event (by
calling this.getToolkit().getImage(imagePath) again), I get an error
on the box I am working on. (indicating the resource is not
accessible)

I do the same thing with text files, and this works fine, because I
can do:
BufferedReader inFile = new BufferedReader(new InputStreamReader(new
FileInputStream(updateEvent.getSource()), "UTF8"));
...and read the updated text from the BufferedReader.

I guess I have to do something similar to "read" the file data to an
Image object and then repaint it. But I haven't figured out how to do
this...

So please if someone can help me; how can I make an Image of the File
object?

Regards, Per Magnus
Knute Johnson - 03 Apr 2007 17:11 GMT
> Hi,
> I am working on a TV application which is based on jdk 1.1.

Why would you use such an out of date version of the compiler?

> here I draw an Image using:
> Image myImage = this.getToolkit().getImage(imagePath);
> (...and graphics.drawImage(myImage) in the paint() method)

Since you are going to want to load the image again, use createImage()
instead of getImage().  See the docs.

> The problem comes when I get an Event telling me that the image is
> updated. From the event, I can get the source of the updated File (in
[quoted text clipped - 3 lines]
>
> When I try to cast the File to an Image, I get a ClassCastException

That's because a File is not an Image.

> If I try to re-instanciate the image after I get the update event (by
> calling this.getToolkit().getImage(imagePath) again), I get an error
[quoted text clipped - 15 lines]
>
> Regards, Per Magnus

Get a URL instead of the File and load it with createImage() again.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.