I made a Morse code translator in Java in which a user inputs a word/words into a text field and the program converts it to Morse code and beeps the Morse code. I have a feeling that my code could be much better, and that it is not efficient enough. Please tell me if there is anything wrong with my code, including efficiency and coding conventions.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MorseCode extends JFrame implements ActionListener{
char[] alpha = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0', ' ' };
String[] dottie = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|" };
public static void play(String filename)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(filename)));
clip.start();
}
catch (Exception exc)
{
exc.printStackTrace(System.out);
}
}
JTextField wordField;
JTextField morseField;
JButton convertButton;
public MorseCode() {
getContentPane().setLayout(null);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel lblEnglish = new JLabel("English");
getContentPane().add(lblEnglish);
lblEnglish.setBounds(10, 11, 46, 14);
wordField = new JTextField();
getContentPane().add(wordField);
wordField.setBounds(10, 36, 264, 71);
wordField.setColumns(10);
convertButton = new JButton("Convert");
getContentPane().add(convertButton);
convertButton.addActionListener(this);
convertButton.setBounds(98, 118, 89, 23);
JLabel lblMorseCode = new JLabel("Morse Code");
getContentPane().add(lblMorseCode);
lblMorseCode.setBounds(10, 161, 98, 14);
morseField = new JTextField();
getContentPane().add(morseField);
morseField.setBounds(10, 186, 264, 64);
morseField.setColumns(10);
}
public static void main(String[] args) {
new MorseCode();
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == convertButton) {
String input = wordField.getText();
String morse = "";
char[] inputArray = input.toCharArray();
for (int i = 0; i < inputArray.length; i++) {
for (int j = 0; j < alpha.length; j++) {
if(inputArray[i] == alpha[j]) {
morse += dottie[j] + " ";
if(alpha[j] == ' ') {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
play("C:/Users/Dylan Black/Desktop/Dylan B./Morse Code/src/" + alpha[j] + ".wav");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
morseField.setText(morse);
}
}
}
Conversion method:
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == convertButton) {
String input = wordField.getText();
String morse = "";
char[] inputArray = input.toCharArray();
for (int i = 0; i < inputArray.length; i++) {
for (int j = 0; j < alpha.length; j++) {
if(inputArray[i] == alpha[j]) {
morse += dottie[j] + " ";
if(alpha[j] == ' ') {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
play("C:/Users/Dylan Black/Desktop/Dylan B./Morse Code/src/" + alpha[j] + ".wav");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
morseField.setText(morse);
}
Play method:
public static void play(String filename)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(filename)));
clip.start();
}
catch (Exception exc)
{
exc.printStackTrace(System.out);
}
}
Constructor:
public MorseCode() {
getContentPane().setLayout(null);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel lblEnglish = new JLabel("English");
getContentPane().add(lblEnglish);
lblEnglish.setBounds(10, 11, 46, 14);
wordField = new JTextField();
getContentPane().add(wordField);
wordField.setBounds(10, 36, 264, 71);
wordField.setColumns(10);
convertButton = new JButton("Convert");
getContentPane().add(convertButton);
convertButton.addActionListener(this);
convertButton.setBounds(98, 118, 89, 23);
JLabel lblMorseCode = new JLabel("Morse Code");
getContentPane().add(lblMorseCode);
lblMorseCode.setBounds(10, 161, 98, 14);
morseField = new JTextField();
getContentPane().add(morseField);
morseField.setBounds(10, 186, 264, 64);
morseField.setColumns(10);
}
tone(frequency, duration, volume)
from stackoverflow.com/a/6700039/638028 . This gives you the flexibility to vary the output speed at runtime, and saves you having to keep all these wav files around. \$\endgroup\$