2 * Sonitus - KnobPanel.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.gui;
20 import java.awt.GridBagConstraints;
21 import java.awt.GridBagLayout;
22 import java.awt.Insets;
23 import javax.swing.DefaultBoundedRangeModel;
24 import javax.swing.JLabel;
25 import javax.swing.JPanel;
26 import javax.swing.JSlider;
27 import javax.swing.event.ChangeEvent;
28 import javax.swing.event.ChangeListener;
30 import net.pterodactylus.sonitus.data.controller.Knob;
33 * A panel that displays a {@link Knob}. A knob panel is very similar to a
34 * {@link FaderPanel}, it just has added tick marks at the center of the
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class KnobPanel extends JPanel {
42 * Creates a new knob panel.
45 * The knob being controlled
47 public KnobPanel(final Knob knob) {
48 super(new GridBagLayout());
50 /* create knob labels. */
51 add(new JLabel("-1"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
52 add(new JLabel("1"), new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
55 JSlider slider = new JSlider(new DefaultBoundedRangeModel((int) (knob.value() * 65536), 0, -65536, 65536));
56 slider.addChangeListener(new ChangeListener() {
59 public void stateChanged(ChangeEvent changeEvent) {
60 knob.value(((JSlider) changeEvent.getSource()).getValue() / 65536.0);
63 if (knob.centered()) {
64 slider.setMajorTickSpacing(65536);
65 slider.setPaintTicks(true);
67 add(slider, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));