Chapter 16

Using Images and Media

In This Chapter

arrow Displaying images in JavaFX components

arrow Adding sounds to your programs

arrow Displaying videos

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.

Using Images

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:

  import javafx.scene.image.*;

Using the Image class

To load an image from an external ...

Get JavaFX For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.