Java Reference
In-Depth Information
@Override
protected Node initView() {
final GridPane gp = new GridPane();
gp.setPadding(new Insets(10));
gp.setHgap(20);
RowConstraints middle = new RowConstraints();
RowConstraints outside = new RowConstraints();
outside.setVgrow(Priority.ALWAYS);
gp.getRowConstraints().addAll(outside, middle, outside);
return gp;
}
private void createEQInterface() {
final GridPane gp = (GridPane) getViewNode();
final MediaPlayer mp = songModel.getMediaPlayer();
createEQBands(gp, mp);
}
// To be continued...
}
Here we have the first portion of our new view. As with all of our custom
AbstractView
classes, we need to
override the
initView
method so that we can create and return the view's “root” node. The equalizer view eventually
shows the live audio spectrum above the equalizer controls. The
GridPane
is once again the natural choice for
displaying content that consists of several rows of nodes. The top row contains our spectrum display, the middle row
contains a set of labels identifying the center frequency of each
EqualizerBand
, and the bottom row consists of a
row of sliders to adjust the gain of each
EqualizerBand
. We therefore create two row constraints to control the sizing
behavior of these rows. Finally, the new
GridPane
instance is returned so that it can become the view's root node.
Because
initView
is called from the superclass constructor, it is called before the rest of
EqualizerView
's
constructor runs. Therefore, by the time that
createEQInterface
is called in the constructor,
initView
will already
have completed. This is why we can call the
getViewNode
method in
createEQInterface
to retrieve the
GridPane
that
was just created in
initView
. The
createEQInterface
method also retrieves the current
MediaPlayer
instance from
songModel
and passes them both to the
createEQBands
method, which creates the
EqualizerBand
instances and the
Slider
controls that are used to manipulate the gain of each band. This method is shown in Listing 9-23.
Listing 9-23.
Creating Equalizer Bands
private void createEQBands(GridPane gp, MediaPlayer mp) {
final ObservableList<EqualizerBand> bands =
mp.getAudioEqualizer().getBands();
bands.clear();
double min = EqualizerBand.MIN_GAIN;
double max = EqualizerBand.MAX_GAIN;
double mid = (max - min) / 2;
double freq = START_FREQ;
Search WWH ::
Custom Search