Java Reference
In-Depth Information
Figure 9-9. Playing a robot movie in JavaFX
Everything you've learned up to this point about playing audio with Media and MediaPlayer also applies to
playing video with MediaView . There are only a few extra concepts that you need to know.
Controlling the Size of a MediaView
MediaView is just another node in the scene graph, so it is possible to scale its content using the usual transforms or
the scaleX and scaleY properties of the Node class. There are more convenient ways to get the job done when dealing
with MediaView s, however. The properties fitWidth and fitHeight can be used to make the MediaView stretch or
shrink to the desired size. Using only one of them at a time will make the MediaView scale only in that dimension while
the size in the other dimension remains unchanged.
If you want to maintain the movie's original aspect ratio as you scale it to fit your application, you can set the
preserveRatio property to true. If you specify a fit size in one dimension along with setting preserveRatio to true,
the other dimension will be automatically scaled to match the movie's original aspect ratio as it's being fit along your
specified dimension. For example, let's say our movie's size is originally 640 × 352 pixels, which gives an aspect ratio
of 640 ÷ 352, or approximately 1.82. If you specify a fitWidth of 400 and enable preserveRatio , the final height of the
movie will be changed from 352 pixels to 400 ÷ 1.82 = 220 pixels. Note that the aspect ratio is preserved because
400 ÷ 220 is also 1.82.
You can also choose to scale in both dimensions by specifying both a fitWidth and a fitHeight value along
with enabling preserveRatio . In this case the best fit for the movie will be calculated and the other dimension will be
scaled to preserve the original aspect ratio. For example, if the fit area and the movie are both wider than they are tall
(the common case), the movie will be scaled to fit horizontally and the height will be determined by using the aspect
ratio to calculate the height needed to match the new width.
One last property that is worth knowing about in relation to resizing a movie is the smooth property. This is a
Boolean property that controls the quality of the resulting image after the scale. If smooth is true, the scaling algorithm
will produce a higher quality image at the cost of some extra computation time. If false, the scaling will be done faster
but the result will not look as good. This lets the developer make a time versus quality trade-off. When making a movie
 
Search WWH ::




Custom Search