3 import java.awt.event.*;
4 import javax.sound.midi.*;
7 * The application's toolbar, with the most frequently used commands.
9 * @author Björn Lanneskog
11 public class MooToolbar extends JToolBar {
13 private JButton rewind, playpause, stop, fastforward;
14 private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue;
15 private ImageIcon playIcon, pauseIcon;
16 private MooMouseAdapter mouseAdapter;
17 public static final Color bgColor = new Color(192, 224, 255);
20 * Creates the toolbar.
24 // setAlignmentX(LEFT_ALIGNMENT);
26 mouseAdapter = new MooMouseAdapter();
28 // Creates playback buttons
29 rewind = createButton("images/rewind.gif", "Rewind");
30 playpause = createButton("images/play.gif", "Play");
31 stop = createButton("images/stop.gif", "Stop");
32 fastforward = createButton("images/forward.gif", "Fast forward");
33 playIcon = new ImageIcon("images/play.gif");
34 pauseIcon = new ImageIcon("images/pause.gif");
36 // Adds playback buttons
42 // Creates progress indicator
43 measure = createLabel("Msr", 10);
44 beats = createLabel("Beat", 10);
45 ticks = createLabel("Tick", 10);
46 measureValue = createLabel("1", 16);
47 beatsValue = createLabel("1", 16);
48 ticksValue = createLabel("1", 16);
49 JPanel spacenorth = new JPanel();
50 spacenorth.setBackground(bgColor);
51 JPanel spacesouth = new JPanel();
52 spacesouth.setBackground(bgColor);
54 // Creates progress indicator panel and adds components
55 JPanel progIndPanel = new JPanel();
56 progIndPanel.setMaximumSize(new Dimension(120,27));
57 progIndPanel.setLayout(new GridLayout(2,4));
58 progIndPanel.add(spacenorth);
59 progIndPanel.add(measure);
60 progIndPanel.add(beats);
61 progIndPanel.add(ticks);
62 progIndPanel.add(spacesouth);
63 progIndPanel.add(measureValue);
64 progIndPanel.add(beatsValue);
65 progIndPanel.add(ticksValue);
71 * Updates the progress indicator.
73 public void updateProgInd() {
74 long pos = Moosique.getSequencer().getTickPosition();
75 int ticksPerBeat = Moosique.getSequence().getResolution();
76 int beatsPerMeasure = 4;
77 long measures = pos / (beatsPerMeasure * ticksPerBeat);
78 long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
79 long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
80 measureValue.setText(Long.toString(measures));
81 beatsValue.setText(Long.toString(beats));
82 ticksValue.setText(Long.toString(ticks));
86 * Creates a button with the specified image and tooltip.
87 * @param imagelocation the imagelacation of the the image displayed in the button
88 * @param tooltip the tooltip associated with this button
89 * @return button the button to be attached to the toolbar
91 private JButton createButton(String imagelocation, String tooltip) {
92 JButton button = new JButton (new ImageIcon(imagelocation));
93 button.setToolTipText(tooltip);
94 button.addMouseListener(mouseAdapter);
99 * Creates labels with the specified text and font size.
100 * @param title the titel displayed on the label
101 * @param fontsize the fontzise of the text displayed on the label
103 private JLabel createLabel(String title, int fontSize){
104 JLabel label = new JLabel(title,JLabel.CENTER);
105 label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
106 label.setBorder(BorderFactory.createLineBorder(Color.black));
110 class MooMouseAdapter extends MouseAdapter {
111 public void mouseClicked(MouseEvent e) {
112 if (((JButton)e.getSource()).getToolTipText() == "Play") {
114 playpause.setIcon(pauseIcon);
115 playpause.setToolTipText("Pause");
116 } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
118 playpause.setIcon(playIcon);
119 playpause.setToolTipText("Resume");
120 } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
122 playpause.setIcon(pauseIcon);
123 playpause.setToolTipText("Pause");
124 } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
126 playpause.setIcon(playIcon);
127 playpause.setToolTipText("Play");
131 public void mousePressed(MouseEvent e) {
132 if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
134 } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
135 Moosique.forward(96);
139 public void mouseReleased(MouseEvent e) {}