Java Reference
In-Depth Information
you type the spurious slash as in
url: "{__DIR__}/figures/myImage.gif" // INCORRECT
As a result, the runtime system cannot locate the image file because
__DIR__
already ends in a slash (
/
) character, and you end up with double slashes (
//
). This
issue has been raised on the JavaFX forums, and hopefully a fix will be imple-
mented to handle this automatically, but in the meantime be careful with this.
Now that we have an
Image
object, to view the image on the display, we need to
create a
javafx.scene.image.ImageView
. An
ImageView
is a scene graph node
that displays an image. Of course, one of its instance variables is
image
. Using
the image object we just created, we create an
ImageView
.
imageView = ImageView {
image: image = bind image
};
This creates a view that takes on the size of the underlying image. If the underlying
image is 100u100 pixels, the view will be sized accordingly. If you want to have
the image display with another size, you need to use the
fitWidth
and
fitHeight
instance variables. Also, if you want to preserve the original aspect ratio for the
image, set
preserveRatio
to true. Just like the
smooth
variable in the
Image
object,
ImageView
has a
smooth
variable. When
smooth
is set to true, scaling will favor
image quality over performance. On the other hand, when
smooth
is false, scaling
favors performance over quality. An example of doing this is shown in Listing 8.3.
Listing 8.3
ImageView Sizing
imageView = ImageView {
fitWidth: bind scene.width
fitHeight: bind scene.height - 25
preserveRatio
: true
smooth: true
image: image = bind image
};
To improve performance, the
ImageView
can be cached in memory as a bitmap.
Do this by setting the variable
cache
to true. Caching improves performance at
the expense of memory. However, on some platforms with graphical processing
units (GPU), caching may not provide any benefit to performance at all. We sug-
gest that you try it either way and see if it helps.
Putting this all together in an example,
SimpleImage
, that is available on the topic's
Web site, we produce the displays in the figures that follow. In
SimpleImage
, we
used an image from the US National Aeronautics and Space Administration
Search WWH ::
Custom Search