Java Reference
In-Depth Information
Figure 7-7.
Path in NetBeans
To load the
Image
, the function
getImage
first checks a local
Map
named
imageMap
to see if the image
was loaded in the past. If the
Image
was loaded, it is simply returned. If the
Image
was not loaded, a new
Image
is created based on the values passed into the function and stored in the
Map
imageMap
before it is
returned.
The pattern of loading items only once as seen in the function
getImage
is used again in the function
getImageSequence
. It first checks to see if an appropriate
ImageSequence
was created and hence stored in
the
Map
sequenceMap
. If the
ImageSequence
was already loaded, it is then returned. If it was not previously
created, a new
ImageSequence
map is created, stored, and returned.
When this pattern is implemented in Java, care must be taken to make sure that two threads don't
ask for the same resource in such a way as to cause the image to be loaded twice. With JavaFX this is not
strictly required, as the JavaFX event thread is effectively single threaded—though it should be noted
that someone could write a Java method that could access the
MediaLoader
in a multi-threaded way. But
as long as you only call these methods from JavaFX, you will be OK.