Java Reference
In-Depth Information
GridPane.setHalignment(spectrumBars[i], HPos.CENTER);
gp.add(spectrumBars[i], i, 0);
}
}
The
createSpectrumBars
method creates a new array of
SpectrumBar
controls, one for each equalizer band
that was previously created.
SpectrumBar
is a custom control class that extends
VBox
to arrange a series of
Rectangle
nodes in a vertical stack. The constructor parameters determine the maximum value of the bar, 100 in this case,
and the number of
Rectangle
nodes in the stack, which we have set to 20. The control has a
setValue
method
that determines how many of the bars are lit. For example, if we set a value of 50 (out of a maximum of 100), half of
the bars, 10 in this case, will be lit. The maximum width of each
SpectrumBar
is set to 44 to match the width of the
equalizer sliders. Each bar is then added to the
GridPane
layout in row zero where they are each centered horizontally
within their
GridPane
cell. Several examples of the control are shown in Figure
9-7
. We do not list the code for
SpectrumBar
because creating a custom control is not the focus of this chapter. If you would like to view the source
code for this control, it is in
SpectrumBar.java
in the AudioPlayer4 example project.
Figure 9-7.
A row of six
SpectrumBar
controls
Listing 9-24 also shows a new line in the
createEQInterface
method that instantiates a new instance of the
SpectrumListener
class. This class is an implementation of the
AudioSpectrumListener
interface. Its job is to listen
for spectrum data updates, calculate the magnitude of the frequencies that lie within each of the equalizer bands we
have defined, and update the corresponding
SpectrumBar
with this calculated value. Listing 9-25 shows the class and
its initialization code.
Listing 9-25.
The
SpectrumListener
Class
class SpectrumListener implements AudioSpectrumListener {
private final SpectrumBar[] bars;
private double minValue;
private double[] norms;
private int[] spectrumBucketCounts;
SpectrumListener(double startFreq, MediaPlayer mp, SpectrumBar[] bars) {
this.bars = bars;
this.minValue = mp.getAudioSpectrumThreshold();
this.norms = createNormArray();
int bandCount = mp.getAudioSpectrumNumBands();
this.spectrumBucketCounts = createBucketCounts(startFreq, bandCount);
}
Search WWH ::
Custom Search