In This Chapter
Displaying images in JavaFX components
Adding sounds to your programs
So far in this book, all the JavaFX applications have been pretty boring. They’ve had plenty of labels, text fields, combo boxes, and the like, but no pictures, sounds, or movies!
This chapter remedies that situation. You find out how to incorporate graphic images (that is, pictures — not necessarily images of a graphic nature) into your JavaFX applications. Just to make things interesting, I show you how to throw in sound effects and music, as well as video, too.
An image is a file that contains a picture. Java supports pictures in several formats, including .jpg, .png, .gif, and .bmp. To incorporate images into your applications, you need to use two classes: Image and ImageView. The Image class represents an image in memory, whereas the ImageView class is a Node that you can add to a scene graph to display an Image on the screen.
Both of these classes are in the package javafx.scene.image, so you need to add the following statement to your programs:
To load an image from an external ...