O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

402
|
Chapter 10, Audio
#78 Provide Audio Controls During Playback
HACK
units represented by the control (like “dB” for a gain-related control, or
“frames per second” for one that is timing-related).
Anyways, you’ve probably figured out that you can build a GUI by using
Swing to provide a view to these
Control
s. In fact, it’s fairly straightforward
to take a factory approach to handing out
JComponent
s based on
Control
s, as
shown in Table 10-1.
The
CompoundControl can’t be handled as easily because you can’t know how
far you’ll have to recursively dig into its hierarchy of controls, nor how to
represent them in relation to one another. For what it’s worth, JavaSound
does not define any
CompoundControl.Type constants, and it’s not clear that
any
CompoundControls exist in J2SE; the Javadoc only speaks in theory of
supporting something like a graphic equalizer, which would need to be a
CompoundControl.
Theory to Practice
To handle the different types of controls that can be encountered, one nice
approach would be to build a factory: you hand it a
Control, and it gives you
back a
JComponent that you can add to your GUI. Example 10-12 shows a
simple implementation of this.
Table 10-1. Controls and their Swing representation
Control type Swing representation
BooleanControl JCheckBox
EnumControl JComboBox
FloatControl JSlider
Example 10-12. Factory to generate Swing widgets for JavaSound controls
import javax.sound.sampled.*;
public class ControlComponentFactory {
private ControlComponentFactory() {super( );}
public static JComponent getComponentFor (Control control) {
System.out.println (control.getType().getClass( ));
if (control instanceof BooleanControl)
return new BooleanControlComponent ((BooleanControl) control);
else if (control instanceof FloatControl)
return new FloatControlComponent ((FloatControl) control);
return new JLabel ("unsupported");
}
}
Provide Audio Controls During Playback #78
Chapter 10, Audio
|
403
HACK
You might notice that I haven’t written a GUI class for EnumControl. That’s
because in my testing, the array of
Controls returned by getControl( ) has
never contained an
EnumControl
, so I don’t have a good way to test it. After
looking at the implementation of the
boolean
and
float
cases, I’ll discuss
how an
EnumControlComponent
would work.
The easier case is the
BooleanControl
, used for controls like Mute, which
turns off the sound (but remembers the previous volume, so when un-
muted, the sound is as loud as it was before). Example 10-13 shows the
implementation of the
BooleanControlComponent
.
As you can see, this is practically trivial. The GUI contains a single
JCheckBox, whose value you set to the value of the control so that it is in a
proper state when first shown. Then, when the user clicks it, you just call
the
BooleanControl’s setValue( ) method.
Supporting a
FloatControl is a lot harder. As you might expect, the way to
represent a range of floating-point values is with a
JSlider; the user can slide
left to reduce the value and right to increase it. To ensure the GUI’s useful-
ness, you can put
JLabels on the left and right of the slider to show what the
minimum and maximum values mean. For example, on a Pan control,
which adjusts placement of stereo sound, the minimum value of -1.0 is Left
and the maximum value of 1.0 is Right.
What makes it hard is handling a mapping of arbitrary floating-point values to
a
JSliders int-based range. Complicating things is the fact that FloatControls
can and do use very different ranges for their values, often spanning nega-
tive and positive values, and operating in both small and vast ranges of
Example 10-13. Swing widget for a JavaSound BooleanControl
import javax.sound.sampled.*;
public class BooleanControlComponent extends JPanel
implements ActionListener {
BooleanControl control;
JCheckBox box;
public BooleanControlComponent (BooleanControl c) {
control = c;
box = new JCheckBox ( );
box.setSelected (control.getValue( ));
add (box);
box.addActionListener (this);
}
public void actionPerformed (ActionEvent ae) {
control.setValue (box.isSelected( ));
}
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required