Java Reference
In-Depth Information
application is initializing. At the beginning of the function
main
, three variables called
seq1
,
seq2
, and
seq3
are created. Each of these three variables holds an
ImageSequence
created by a call to
MediaLoader.imageSequence
.
MediaLoader
is a class that is used to manage different types of media in an application. The function
imageSequence
takes a path, a count, and a flag specifying if the function should return before all of the
images are loaded. Passing true to the function
imageSequence
tells the
MediaLoader
to not wait for all of
the images to load. Having the first calls to
imageSequence
return immediately allows us to set up the
scene without waiting for a background thread to load all of the images. This improves the user
experience, as the window pops up much sooner.
Since the three
ImageSequences
are not fully loaded when the application starts, we want to let the
user know that the application is loading and give her some sense of progress. The
Timeline
named
checkProgress
is used to check if the
ImageSequences
are fully loaded by checking the progress of each
ImageSequence
every .7 seconds. If they are not loaded, the
Label
progressText
is updated to let the user
know the current progress. If all of the
ImageSequences
are loaded, then the three buttons are enabled.
The
Timeline
checkProgress
knows that the
ImageSequences
are loaded if the sum of their progress is 300
percent—that's 100 percent per sequence.
Once the buttons are enabled, they can be pressed. Pressing each button sets a different
ImageSequenceView
to be displayed in the scene. For example, in Listing 7-1 the function
asteroid
creates
a new
ImageSequenceView
and makes sure the
Timeline
anim
is started. You should note that each of these
functions creates a new
ImageSequenceView
, but its
imageSequence
attribute is set by a call to
MediaLoader
.
This is done to illustrate a helpful pattern. Since we know
MediaLoader
will only load a particular
ImageSequence
once, and if we rely on
MediaLoader
to always get an
ImageSequence
, then we know for sure
that our application is only loading each
ImageSequence
once. Granted, in such a simple application this
is not really required, but in more complex applications it is very useful.
Before we explore how the
ImageSequence
and
ImageSequenceView
classes are implemented, let's
explore in more detail the class
MediaLoader
.
Listing 7-2.
MediaLoader
def instance:MediaLoader = MediaLoader{};
public function image(classpath:String, background:Boolean):Image{
instance.getImage(classpath, background);
}
public function imageSequence(classpathBase:String,imageCount:Integer,
background:Boolean):ImageSequence{
return instance.getImageSequence(classpathBase, imageCount, background);
}
public class MediaLoader {
var imageMap:Map = new HashMap();
var sequenceMap:Map = new HashMap();
public function getImage(classpath:String, background:Boolean):Image{
var image:Image = imageMap.get(classpath) as Image;
if (image == null){
image = Image{
url: this.getClass().getResource(classpath).toString();
smooth: true