Java Reference
In-Depth Information
When you're dragging an image into the application, the
imageFiles
variable
will cache the absolute file path as a string instead of as the actual image file in order to
save memory space. If a user drags the same image file into the display area, the list
will contain duplicate strings representing the image file. As an image is being dis-
played, the
currentIndex
variable contains the index into the
imageFiles
list.
The
imageFiles
list points to the string representing the current image file. As the
user clicks the buttons to display the previous and next image, the
currentIndex
will decrement or increment, respectively. Next, let's walk through the code detailing
the steps for loading and displaying an image. Later, you will learn the steps for paging
through each image with the next and previous buttons.
Begin by instantiating an instance of the
javafx.scene.image.ImageView
class. The
ImageView
class is a graph node (
Node
) used to display an already loaded
javafx.scene.image.Image
object. Using the
ImageView
node will enable
you to create special effects on the image to be displayed without manipulating the
physical image. To avoid performance degradation when rendering many effects, you
can use numerous
ImageView
objects that reference a single
Image
object. Many
types of effects include blurring, fading, and transforming an image.
One of the requirements is preserving the displayed image's aspect ratio as the user
resizes the window. Here, you will simply call the
setPreserveRatio()
method
with a value of
true
to preserve the image's aspect ratio. Remember that because the
user resizes the window, you want to bind the width of the
ImageView
to the
Scene
's width to allow the image to be scaled. After setting up the
ImageView
, you
will want to pass it to an
HBox
instance (
pictureRegion
) to be put into the scene.
The following code creates the
ImageView
instance, preserves the aspect ratio, and
scales the image:
// image view
final ImageView currentImageView = new ImageView();
// maintain aspect ratio