0% found this document useful (0 votes)
288 views

Creating A Metronome in Java PDF

This document provides code for a Java program to create a metronome application with a user interface. The interface includes sliders to set beats per minute (BPM) and duration in seconds. It also includes a button to trigger the generateBeats method, which is left for the user to implement. This method will produce beats based on the BPM and duration set by the user. The document provides the full code to set up the interface, and explains that the focus is on implementing the generateBeats method.

Uploaded by

Marco Testa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
288 views

Creating A Metronome in Java PDF

This document provides code for a Java program to create a metronome application with a user interface. The interface includes sliders to set beats per minute (BPM) and duration in seconds. It also includes a button to trigger the generateBeats method, which is left for the user to implement. This method will produce beats based on the BPM and duration set by the user. The document provides the full code to set up the interface, and explains that the focus is on implementing the generateBeats method.

Uploaded by

Marco Testa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

A

Chapter 3 JAVA EXERCISE


Creating a Metronome in Java

Implement a Java program for a metronome that allows the user to


generate a number of beats per minute for a specified duration in
seconds. The user interface should look like the following figure,
which uses button and slider objects. So that you can focus on the
generateBeats method, which requires that you understand and
implement concepts relating to tempo, we are providing the Java
code for the user interface.

Digital Sound & Music PROGRAMMING EXERCISE


Creating a Metronome in Java Page 1
This material is based on work supported by the National Science Foundation under CCLI Grant DUE 0717743, Jennifer Burg PI, Jason Romney, Co-PI.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import javax.swing.border.TitledBorder;

public class Metronome extends JFrame {

public static void main(String[] args) {

Runnable app = new Runnable() {


public void run() {
Metronome t = new Metronome();
t.setVisible(true);
}
};
SwingUtilities.invokeLater(app);
}

public Metronome() {
super("Metronome App");
// code to use the operating system look.
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println(e);
}

final JSlider sBPM = new JSlider(JSlider.VERTICAL, 20, 200, 120);


sBPM.setPaintLabels(true);
sBPM.setPaintTicks(true);
sBPM.setMajorTickSpacing(20);
sBPM.setMinorTickSpacing(10);
java.util.Hashtable labelSlider = new java.util.Hashtable();
labelSlider.put(new Integer(200), new JLabel(" 200 Prestissimo"));
labelSlider.put(new Integer(180), new JLabel(" 180 Presto"));
labelSlider.put(new Integer(160), new JLabel(" 160"));
labelSlider.put(new Integer(140), new JLabel(" 140"));
labelSlider.put(new Integer(130), new JLabel(" 130 Allegro"));
labelSlider.put(new Integer(120), new JLabel(" 120"));
labelSlider.put(new Integer(100), new JLabel(" 100 Moderato"));
labelSlider.put(new Integer(90), new JLabel(" 90 Andante"));
labelSlider.put(new Integer(80), new JLabel(" 80"));
labelSlider.put(new Integer(70), new JLabel(" 70 Adagio"));
labelSlider.put(new Integer(60), new JLabel(" 60"));
labelSlider.put(new Integer(50), new JLabel(" 50 Lento"));
labelSlider.put(new Integer(40), new JLabel(" 40"));
labelSlider.put(new Integer(20), new JLabel(" 20"));
sBPM.setLabelTable(labelSlider);
sBPM.setPaintLabels(true);
sBPM.setPreferredSize(new Dimension(150, 400));
sBPM.setBorder(new TitledBorder("Beats per minute (BPM)"));

final JSlider sDuration = new JSlider(JSlider.VERTICAL, 0, 120, 5);


sDuration.setPaintLabels(true);
sDuration.setPaintTicks(true);
sDuration.setMajorTickSpacing(20);
sDuration.setMinorTickSpacing(10);
sDuration.setPreferredSize(new Dimension(70, 400));

Digital Sound & Music PROGRAMMING EXERCISE


Creating a Metronome in Java Page 2
This material is based on work supported by the National Science Foundation under CCLI Grant DUE 0717743, Jennifer Burg PI, Jason Romney, Co-PI.
sDuration.setBorder(new TitledBorder("Seconds"));

JPanel pMain = new JPanel(new BorderLayout());


pMain.add(sBPM, BorderLayout.EAST);
pMain.add(sDuration, BorderLayout.CENTER);

JButton bBeats = new JButton("Listen to Beats");

bBeats.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
generateBeats(sBPM.getValue(), sDuration.getValue());
}
});

JPanel panel = new JPanel(new BorderLayout());


panel.add(bBeats, BorderLayout.WEST);
pMain.add(panel, BorderLayout.NORTH);
pMain.setBorder(new javax.swing.border.EmptyBorder(5, 3, 5, 3));
getContentPane().add(pMain);
pack();
setLocation(0, 20);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

public void generateBeats(double bpm, int duration) {


/***Fill in code here.***/

Digital Sound & Music PROGRAMMING EXERCISE


Creating a Metronome in Java Page 3
This material is based on work supported by the National Science Foundation under CCLI Grant DUE 0717743, Jennifer Burg PI, Jason Romney, Co-PI.

You might also like