Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading an Array of Images

 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Is this the right way to load an array of images?
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the right way to load an array of images?
So far, so good: you're on the right track. This instantiates the image array but does not actually load the image data. The Toolkit getImage and creatImage methods return immediately, ie, they load synchronously. What you want for image loading is to block program execution until all the images are fully–loaded. To do this we use the MediatTracker class. See the class api for example code and handy methods for checking on the loading status.
Also, while we're at it, check out the information for (the Toolkit method) getImage in the (Toolkit) Method Detail section: it explains why getImage may not be the best choice.
This is the older way to load images. We have two other, later options:
ImageIcon was introduced in j2se1.2 and uses the Toolkit and MediaTracker classes for loading images. One drawback of this appraoch is that it gives no indication/exception if the file is not found or the data is corrupted/not loaded. It's api does provide ways to find out about loading trouble/success.
In j2se 1.4 ImageIO was introduced and is the newest way to load images. It throws exceptions for loading trouble.
 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Craig.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic