Java Reference
In-Depth Information
try
{
mt.waitForID(1);
}
catch (InterruptedException ie)
{
assert false;
}
setPreferredSize(new Dimension(image.getWidth(null),
im-
age.getHeight(null)));
this.image = image;
}
}
ImageCanvas declares an Image field that stores a reference to the image to
be displayed. It also overrides the paint() method to invoke drawImage() . This
method does nothing when the Image argument is the null reference; this is the case
when paint() is called before the user selects an image. null is passed as the
ImageObserver argument because the image is completely loaded at this point, as
you will discover.
The setImage() methodiscalledtoloadtheimage,setitspreferredsizetoinflu-
ence Listing 7-4 's sp.doLayout(); method call, and save the Image argument in
the Image field so that it can be referenced from a subsequent paint() call, which
happens in response to doLayout() .
Image loading is accomplished by using the java.awt.MediaTracker class.
MediaTracker declaresa void addImage(Image image, int id) method
thataddsan Image objecttoalistof Image objectsbeingtracked.Theassociated id
value is later used by MediaTracker 's void waitForID(int id) method to
startloadingtheidentified Image objects,andwaituntilalltheseimageshavefinished
loading.
After waitForID() returns, the image is completely loaded and its width and
height are available. This information is obtained in subsequent getWidth() and
getHeight() calls. Although these calls require an image observer, which could
be specified by passing this as an argument (because Component implements
ImageObserver ), doing so isn't necessary because the image is loaded.
Search WWH ::




Custom Search