Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Learn how applets load network-based images asynchronously

Here's a close look at the way Java applets handle images in a network-based environment

  • Print
  • Feedback
Please indulge me while I let you in on a little secret (well, it's not really so little): The Java applet is a different sort of beast from the typical application that executes on your PC or workstation. For one thing, it doesn't sit quietly on your hard drive waiting for your attention; a Java applet sits on the other side of a network connection waiting for you (and many others) to pull it across the network. Once the applet has made the journey, it comes to life and begins to download the resources it needs. Unfortunately, all of this network activity results in one thing: delay. In contrast to an application on your hard drive, an applet must expect, and deal constructively with network delay.

For this reason, the Java class library was designed to support asynchronous loading of media, such as images. Asynchronous loading means that loading occurs out of step with the rest of the application -- in another thread -- in the background. In fact, what might be thought of as one operation, the loading of an image from across the network, actually occurs in two distinct stages, which often occur asynchronously. These stages are: image definition and the actual downloading of the image.

In this column, I will take a close look at the mechanics of image loading from the perspective of a Java applet or similar network-based Java application. I will also provide a step-by-step demonstration of how to load and draw images within an applet.

But first, let's take a look at the principle players.

Getting the picture with the Image class

An instance of the Image class represents image data. The image data does not need to reside locally; it may exist on another computer. In fact, it may not exist at all. An instance of the Image class is more like a reference to image data, than a container for image data.

Consider an image on a computer across a network. The creation of an instance of the Image class on a local computer does not cause the image data to be pulled across the network. This is true because instantiation of the Image class does not cause reconstruction of the image data. The Image class doesn't even provide methods for initiating image reconstruction. (This class does, however, provide access to an ImageProducer object that does provide such methods -- I will talk about image producers next month.)

Although the Image class doesn't store image data, it does provide methods for obtaining information about an image. The following methods return such information:

int getHeight(ImageObserver obs);

int getWidth(ImageObserver obs);
Object getProperty(String nm, ImageObserver obs);


The height and width values returned from the first two methods indicate the size of the image, in pixels. Properties, on the other hand, are image-format-specific pieces of information about an image, and are retrieved by name. The only property mentioned specifically in the Image class API is the comment property. It should contain a description of the image, or something similar. These methods return invalid values (the getHeight() and getWidth() methods both return -1 and the getProperty() method returns null) if the desired information is currently unavailable.

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?