Java Reference
In-Depth Information
Unfortunately in version 1.2, the
Image
class does not offer event notification functions
to know when image is done loading. If your code relies on the actual size of the image to
be known, you must block with asynchronous loading (by setting
backgroundLoading =
false
) to wait for the image to download and get the size. Another way around is to specify
the size of the image yourself by specifying the dimensions (see next sub-section on
Image
resize and aspect ratio
).
Image resize and aspect ratio
Another feature supported by
Image
and
ImageView
is the automatic resizing of the image.
The
Image
class will attempt to resize the image when a value is provided for the properties.
width:Number
or
height:Number
.
ImageView
will attempt to do the same when the
properties
fitWidth:Number
and
fitHeight:Number
are specified. Both classes support
property
preserveRation:Boolean,
which forces the resize operation to maintain the
aspect ratio of the original image while resizing to the specified dimensions as shown next:
def imgView:ImageView = ImageView{
preserveRatio:true
fitWidth
: 200
};
The previous code will resize the image to a width of 200 pixels. Because the
preserveRatio
property is
true
, the height of the image is automatically calculated. This is useful especially if
you do not know the actual size of the image ahead of time (see previous section).
See also
F
Chapter 4—Creating a form with JavaFX controls
F
Introduction
F
Accessing media assets
Applying effects and transformations
to images
Now that you have learned how to load images, what can you do with them? Well, since
ImageView
is an instance of the
Node
class, your loaded images can receive the same
treatment you would ordinarily provide, shapes, for example. In this recipe, we are going to
extend the example from the previous recipe,
Loading and displaying images with ImageView
,
to add image manipulation functionalities.