// Play Middle C song using the system's beep() sound - RJM Programming - September, 2014
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.math.*;

public class JSound extends JFrame implements ActionListener {
  JLabel ourlabel; 
  Icon ourspeaker;
  JPanel ourpane;
  
  public JSound() {
    setSize(700,100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ourpane=new JPanel();
    ourspeaker = new ImageIcon("./images/sound.gif");
    ourlabel = new JLabel(ourspeaker);
    JButton oursource = new JButton("Play the Middle C song.");
    oursource.addActionListener(this);
    ourpane.add(ourlabel, BorderLayout.WEST);
    ourpane.add(oursource, BorderLayout.EAST);
    setTitle("Middle C Song ... 2/4 time ... C = crotchet, M = minum; L = left hand, R = right hand");
    setContentPane(ourpane);
    ourpane.setBackground( Color.YELLOW );
    setVisible(true); 
  }
  
  public void actionPerformed(ActionEvent oure) {
      long gaps[] = { 1000, 1000, 2000, 1000, 1000, 2000, 1000, 1000, 1000, 1000,  1000, 1000, 2000 };
      String sgaps[] = { "Mid", "dle ", "C, ", "Mid", "dle ", "C, ", "Left ", "hand, ", "Right ", "hand, ",  "Mid", "dle ", "C",
 "Mid(C)", "dle(C) ", "C,(M) ", "Mid(C)", "dle(C) ", "C,(M) ", "Left(C) ", "hand,(C) ", "Right(C) ", "hand,(C) ",  "Mid(C)", "dle(C) ", "C(M)",
 "Mid(R)", "dle(R) ", "C,(R) ", "Mid(L)", "dle(L) ", "C,(L) ", "Left(L) ", "hand,(L) ", "Right(R) ", "hand,(R) ",  "Mid(R)", "dle(R) ", "C(L)" };
      for (int j=0; j<3; j++) {
       setTitle("");
       for (int i=0; i<gaps.length; i++) {   // sound out speaker
        Toolkit.getDefaultToolkit().beep(); 
        setTitle(getTitle() + sgaps[i + j * gaps.length]);
        try { Thread.currentThread().sleep(gaps[i]); 
        } catch (InterruptedException ourie) { System.out.println("Sleep has been interrupted."); }
       }
      }
  }

  public static void main(String args[]) { new JSound(); }
}

