Java Reference
In-Depth Information
The last function in Listing 7-3 is called
progress
and is used to calculate the total progress of each
Image
in this
ImageSequence
. The progress of an image refers to how far along it is in its loading. For an
Image
, a progress of 0.0 means it is not loaded, and a progress of 100.0 means it is loaded and ready to be
displayed. I think that the values should have ranged from 0.0 to 1.0, as that would be more consistent
with the rest of the JavaFX API. But since it is out of my power to change this, I decided to make the
progress
function on
ImageSequence
consistent with that pattern. It returns a value with the range 0.0 to
100.0—just like
Image
. Taking advantage of the
progress
function enabled the
Timeline
checkProgress
from Listing 7-2 to indicate the percentage of images that were loaded.
The last class to look at is
ImageSequenceView
, which is used to actually put the images on the screen.
Listing 7-4 shows the details.
Listing 7-4.
ImageSequenceView
public class ImageSequenceView extends Group{
public-init var imageSequence:ImageSequence;
public var currentImage:Integer on replace {
updateImage()
}
var lastImage:Integer = 0;
init{
for (i in [0..imageSequence.imageCount-1]){
var image = imageSequence.images[i];
var imageView = ImageView{
image: image;
translateX: image.width/-2.0
translateY: image.height/-2.0
visible: false;
}
insert imageView into content;
}
currentImage = 0;
updateImage();
}
function updateImage():Void{
currentImage = currentImage mod imageSequence.imageCount;
content[lastImage].visible = false;
var currentImageView = content[currentImage];
currentImageView.visible = true;
lastImage = currentImage;
}
}