Java Reference
In-Depth Information
played. Located on the lower right of the application are buttons to stop, pause, and re-
sume play of audio media. (The button controls are shown in Figure 16-2 .) As the mu-
sic is playing, the user will also notice randomly colored balls bouncing around to the
music. Once the users are done listening to the music, they can quit the application by
clicking the white rounded close button located in the upper-right corner.
It is similar to Recipe 15-1, in which you learned how to use the drag-and-drop
desktop metaphor to load files into a JavaFX application. Instead of image files,
however, the user is accessing audio files. JavaFX currently supports the following au-
dio file formats: .mp3 , .wav , and .aiff .
Following the same look and feel, you will use the same style as Recipe 15-1. In
this recipe, you modify the button controls to resemble buttons, similar to many media
player applications. When the pause button is pressed, it will pause the audio media
from playing and toggle to the play button control, thus allowing the users to resume.
As an added bonus, the MP3 player will appear as an irregular shaped, semitransparent
window without borders that can also be dragged around the desktop using the mouse.
Now that you know how the music player will operate, let's walk through the code.
First, you need to create instance variables that will maintain state information for
the lifetime of the application. Table 16-1 describes all the instance variables used in
this music player application. The first variable is a reference to a media player ( Me-
diaPlayer ) object that will be created in conjunction with a Media object contain-
ing an audio file. Next, you create an anchorPt variable used to save the starting co-
ordinate of a mouse press when the users begin to drag the window across the screen.
When calculating the upper-left bounds of the application window during a mouse-
dragged operation, the previousLocation variable will contain the previous win-
dow's screen X and Y coordinates.
Table 16-1 . MP3 Player Application Instance Variables
Table 16-1 lists the MP3 player application's instance variables.
In previous chapters relating to GUIs, you saw that GUI applications normally con-
tain a title bar and windowed borders surrounding the scene. Here, I wanted to raise the
bar a little by showing you how to create irregularly shaped semitransparent windows,
 
 
Search WWH ::




Custom Search