Java Reference
In-Depth Information
its size and the hardware used. Therefore the aforementioned problems might not
occur on some systems. For more on the mechanism of asynchronous loading
consult Chapter 20.
To solve this problem one uses the concept of a
media tracker
. This can be
used to monitor the loading process, wait for the picture to be fully loaded and
only then display it. Class
MediaTracker
can be found in the AWT library. A
media tracker can monitor the loading process of many images. To distinguish the
different images, every image receives an identification (ID) number. We use the
following constructor and methods:
MediaTracker(Component comp)
addImage(Image picture,
int
id)
waitForID(
int
id)
waitForID(
int
id,
int
msec)
MediaTracker(Component comp)
creates a media tracker which monitors images
loaded for component
comp
.
addImage(Image picture,int id)
adds
picture
to those images monitored by
the media tracker. The picture gets identification number
id
.
waitForID(int id)
waits for the loading process of the image with identification
number
id
to finish. The application waits at this point until the picture is fully
loaded. The statement has to be embedded into a
try-catch
block because
it throws an
InterruptedException
if the loading process fails (e.g. due a
network failure). The programmer can implement appropriate reactions for
this case.
waitForID(int id,int msec)
waits for the loading process of the image with
identification number
id
to finish or for
msec
milliseconds to pass, whichever
happens first. Also this statement has to be embedded into a
try-catch
block.
In method
addImageAndTrack
of class
ImagePanel
we implement this concept.
The identification numbers are increased from one picture to the next to guaran-
tee uniqueness. The
repaint
method is called only when the image is fully loaded.
Class
ImagePanelTrackerFrame
is like
ImagePanelFrame
except that it uses
addImageAndTrack
instead of
addImage
.Itcan be downloaded from the topic's
home page.
15.4.2
Modifying images
Up to now, we displayed the images 'as they are', i.e. every pixel in the image
file was shown on the screen. Pictures loaded into
Image
variables can also be re-
scaled or reflected or only a part of them can be displayed. To this end it suffices to
use method
drawImage
from class
Graphics
with an appropriate set of arguments.
Recall that only part of an image might be displayed if
drawImage
is used while
the image is not yet fully loaded.
Search WWH ::
Custom Search