X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=MooToolbar.java;h=c1244f575e81b717e8db764427727b2d0362fd06;hb=c83e74facf762222fe4578f175408cc50d360518;hp=2415b3bbbbbd97af56e0807c2c59b53fb5865e3a;hpb=78520aa49e7b691386fed30abd902c3f56dcc3f0;p=moosique.git diff --git a/MooToolbar.java b/MooToolbar.java index 2415b3b..c1244f5 100644 --- a/MooToolbar.java +++ b/MooToolbar.java @@ -1,60 +1,138 @@ import javax.swing.*; +import java.awt.*; import java.awt.event.*; +import javax.sound.midi.*; -public class MooToolbar extends JToolBar { +/** + * The application's toolbar, with the most frequently used commands. + * + * @author Björn Lanneskog + */ +public class MooToolbar extends JToolBar { - public MooToolbar() { + private JButton rewind, playpause, stop, fastforward; + 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); - rewind = createButton("images/rewind.gif", "rewind"); - add(rewind); + /** + * Creates the toolbar. + */ + + 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"); + // Adds playback buttons + add(rewind); add(playpause); - - stop = createButton("images/stop.gif", "stop"); add(stop); - - fastforward = createButton("images/forward.gif", "fast forward"); add(fastforward); - meas = createProgressIndikator("meas"); - add(meas); - } - - private JButton createButton(String imageLocation, String toolTip) { - JButton button = new JButton (new ImageIcon(imageLocation)); - button.setToolTipText(toolTip); - return button; - } - - private JPanel createProgressIndikator(String name) { - JPanel panel = new JPanel(); - JLabel label = new JLabel(name); - JComboBox combo = new JComboBox(); - add(label); - add(combo); - return panel; - - + // Creates progress indicator + measure = createLabel("Msr", 10); + beats = createLabel("Beat", 10); + ticks = createLabel("Tick", 10); + measureValue = createLabel("1", 16); + beatsValue = createLabel("1", 16); + ticksValue = createLabel("1", 16); + 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() { + int pos = Moosique.getSequencer().getTickPosition(); + int ticksPerBeat = Moosique.getSequencer().getResolution(); + int beatsPerMeasure = 4; + measureValue.setText(pos / (beatsPerMeasure * ticksPerBeat)); + beatsValue.setText((pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat); + ticksValue.setText(pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat); + } + + /** + * 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; + } + + /** + * 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)); + label.setBorder(BorderFactory.createLineBorder(Color.black)); + 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"); + } 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"); + } else if (((JButton)e.getSource()).getToolTipText() == "Stop") { + Moosique.stop(); + playpause.setIcon(playIcon); + playpause.setToolTipText("Play"); + } } - - /*public void actionPerformed(ActionEvent e) { - String but = e.getActionCommand(); - - if (but == "rewind") { - - } else if (but == "images/play.gif") { - JOptionPane.showMessageDialog(null, "playing"); - - } else if (but == "stop") { - - } else if (but == "rewind") { - + + 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); } - }*/ - private JButton rewind; - private JButton playpause; - private JButton stop; - private JButton fastforward; - private JPanel meas; -} + } + + public void mouseReleased(MouseEvent e) {} + } +} \ No newline at end of file