import javax.swing.*;
-import java.awt.event.*;
import java.awt.*;
+import java.awt.event.*;
import javax.sound.midi.*;
-import javax.swing.Icon;
/**
- * Moosiques GUI representing a toolbar, with the most frequently used commands.
+ * The application's toolbar, with the most frequently used commands.
*
* @author Björn Lanneskog
*/
-public class MooToolbar extends JToolBar implements ActionListener {
+public class MooToolbar extends JToolBar {
private JButton rewind, playpause, stop, fastforward;
- private JLabel measure, beats, ticks, measurevalue, beatsvalue, ticksvalue;
- private Image pauseimage, playimage;
+ private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue;
+ private ImageIcon playIcon, pauseIcon;
+ private MooMouseAdapter mouseAdapter;
+ public static final Color bgColor = new Color(192, 224, 255);
/**
- * Constructs a toolbar
+ * Creates the toolbar.
*/
- public MooToolbar() {
-
- rewind = createButton("images/rewind.gif", "rewind");
- add(rewind);
+ public MooToolbar() {
+ // setAlignmentX(LEFT_ALIGNMENT);
+ setFloatable(false);
+ mouseAdapter = new MooMouseAdapter();
+
+ // Creates playback buttons
+ rewind = createButton("images/rewind.gif", "Rewind");
+ playpause = createButton("images/play.gif", "Play");
+ stop = createButton("images/stop.gif", "Stop");
+ fastforward = createButton("images/forward.gif", "Fast forward");
+ playIcon = new ImageIcon("images/play.gif");
+ pauseIcon = new ImageIcon("images/pause.gif");
- playpause = createButton("images/play.gif", "play/pause");
+ // Adds playback buttons
+ add(rewind);
add(playpause);
-
- stop = createButton("images/stop.gif", "stop");
add(stop);
-
- fastforward = createButton("images/forward.gif", "fast forward");
add(fastforward);
- JPanel panel = new JPanel();
- panel.setMaximumSize(new Dimension(100,22));
- panel.setLayout(new GridLayout(2,4));
- add(panel);
-
- panel.add(new JPanel());
-
- measure = createProjIndLabel("Mrs",10);
- panel.add(measure);
-
- beats = createProjIndLabel("Beat",10);
- panel.add(beats);
-
- ticks = createProjIndLabel("Tick",10);
- panel.add(ticks);
-
- panel.add(new JPanel());
-
- measurevalue = createProjIndLabel("1",10);
- panel.add(measurevalue);
-
- beatsvalue = createProjIndLabel("1",10);
- panel.add(beatsvalue);
-
- ticksvalue = createProjIndLabel("1",10);
- panel.add(ticksvalue);
-
- playimage = Toolkit.getDefaultToolkit().getImage("images/play.gif");
- pauseimage = Toolkit.getDefaultToolkit().getImage("images/pause.gif");
-
- }
-
- /**
- * creates a JButton for the toolbar
- * @param imagelocation the imagelacation of the the image displayed in the button
- * @param tooltip the tooltip associated with this button
- * @return button the button to be attached to the toolbar
- */
- private JButton createButton(String imagelocation, String tooltip) {
- JButton button = new JButton (new ImageIcon(imagelocation));
- button.setToolTipText(tooltip);
- button.addActionListener(this);
- return button;
- }
-
- /**
- * creates JLabels for the progressindikator, attached to the toolbar
- * @param title the titel displayed on the label
- * @param fontsize the fontzise of the text displayed on the label
- * @param titelvalue the label that is a part of the progressindikator
- */
- private JLabel createProjIndLabel(String title, int fontsize){
- JLabel titelvalue = new JLabel(title,JLabel.CENTER);
- titelvalue.setFont(new Font("Times New Roman",
- Font.PLAIN ,fontsize));
- return titelvalue;
- }
-
- // int pos = Moosique.getSequencer().getTickPosition();
- // int measures = pos / (4 * Moosique.getSequence.get)
- // int beats = (pos - measures * 4 * 96) / 96;
- // int ticks = (pos - measures*4*96 - beats*96);
-
-
- public void actionPerformed(ActionEvent e) {
+ // Creates progress indicator
+ measure = createLabel("Msr", 10);
+ beats = createLabel("Beat", 10);
+ ticks = createLabel("Tick", 10);
+ measureValue = createLabel("1", 16);
+ measureValue.setBorder(BorderFactory.createLineBorder(Color.black));
+ beatsValue = createLabel("1", 16);
+ beatsValue.setBorder(BorderFactory.createLineBorder(Color.black));
+ ticksValue = createLabel("1", 16);
+ ticksValue.setBorder(BorderFactory.createLineBorder(Color.black));
+ JPanel spacenorth = new JPanel();
+ spacenorth.setBackground(bgColor);
+ JPanel spacesouth = new JPanel();
+ spacesouth.setBackground(bgColor);
+
+ // Creates progress indicator panel and adds components
+ JPanel progIndPanel = new JPanel();
+ progIndPanel.setMaximumSize(new Dimension(120,27));
+ progIndPanel.setLayout(new GridLayout(2,4));
+ progIndPanel.add(spacenorth);
+ progIndPanel.add(measure);
+ progIndPanel.add(beats);
+ progIndPanel.add(ticks);
+ progIndPanel.add(spacesouth);
+ progIndPanel.add(measureValue);
+ progIndPanel.add(beatsValue);
+ progIndPanel.add(ticksValue);
+ add(progIndPanel);
+
+ }
+
+ /**
+ * Updates the progress indicator.
+ */
+ public void updateProgInd() {
+ long pos = Moosique.getSequencer().getTickPosition();
+ int ticksPerBeat = Moosique.getSequence().getResolution();
+ int beatsPerMeasure = 4;
+ long measures = pos / (beatsPerMeasure * ticksPerBeat);
+ long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat;
+ long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat;
+ measureValue.setText(Long.toString(measures));
+ beatsValue.setText(Long.toString(beats));
+ ticksValue.setText(Long.toString(ticks));
+ }
+
+ /**
+ * Creates a button with the specified image and tooltip.
+ * @param imagelocation the imagelacation of the the image displayed in the button
+ * @param tooltip the tooltip associated with this button
+ * @return button the button to be attached to the toolbar
+ */
+ private JButton createButton(String imagelocation, String tooltip) {
+ JButton button = new JButton (new ImageIcon(imagelocation));
+ button.setToolTipText(tooltip);
+ button.addMouseListener(mouseAdapter);
+ return button;
+ }
- if (((JButton)e.getSource()).getToolTipText() == "rewind") {
- //få in rewindmetoden
- } else if (((JButton)e.getSource()).getToolTipText() == "play/pause") {
- if (Moosique.getSequence().isRunning()) {
- Moosique.pause();
- playpause.getIcon().setImage(playimage);
- } else {
- Moosique.play();
- playpause.getIcon().setImage(pauseimage);
- }
-
- } else if (((JButton)e.getSource()).getToolTipText() == "stop") {
- Moosique.stop();
-
- } else if (((JButton)e.getSource()).getToolTipText() == "fastforward") {
- // få in fastforwardmetoden
-
+ /**
+ * Creates labels with the specified text and font size.
+ * @param title the titel displayed on the label
+ * @param fontsize the fontzise of the text displayed on the label
+ */
+ private JLabel createLabel(String title, int fontSize){
+ JLabel label = new JLabel(title,JLabel.CENTER);
+ label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));
+ return label;
+ }
+
+ class MooMouseAdapter extends MouseAdapter {
+ public void mouseClicked(MouseEvent e) {
+ if (((JButton)e.getSource()).getToolTipText() == "Play") {
+ Moosique.play();
+ playpause.setIcon(pauseIcon);
+ playpause.setToolTipText("Pause");
+ while(Moosique.getSequencer().isRunning()) {
+ Moosique.getGUI().update();
+ }
+ } else if (((JButton)e.getSource()).getToolTipText() == "Pause") {
+ Moosique.pause();
+ playpause.setIcon(playIcon);
+ playpause.setToolTipText("Resume");
+ } else if (((JButton)e.getSource()).getToolTipText() == "Resume") {
+ Moosique.resume();
+ playpause.setIcon(pauseIcon);
+ playpause.setToolTipText("Pause");
+ while(Moosique.getSequencer().isRunning()) {
+ Moosique.getGUI().update();
+ }
+ } else if (((JButton)e.getSource()).getToolTipText() == "Stop") {
+ Moosique.stop();
+ playpause.setIcon(playIcon);
+ playpause.setToolTipText("Play");
}
}
-
-}
+
+ public void mousePressed(MouseEvent e) {
+ if (((JButton)e.getSource()).getToolTipText() == "Rewind") {
+ Moosique.rewind(96);
+ } else if (((JButton)e.getSource()).getToolTipText() == "Fast forward") {
+ Moosique.forward(96);
+ }
+ }
+
+ public void mouseReleased(MouseEvent e) {}
+ }
+}
\ No newline at end of file