/* **************************************************** * Kelli Wiseth * kelli@alameda-tech-lab.com * CIS 255AX * Program name: Morse.java * Program Description: This program is a simple windowed application * that lets a user type in a short sentence or phrase and displays * the Morse code equivalent. * * Assignment #2 * 14 February 2005 **************************************************** */ import java.awt.*; // Container, FlowLayout import java.awt.event.*; // ActionEvent, ActionListener import javax.swing.*; // JApplet, JButton, JLabel, JTextField import java.util.*; // StringTokenizer, Pattern public class Morse extends JFrame { JButton exitButton; JLabel promptLabel; JTextField inputField; JTextArea outputArea; String inputWord = ""; String morseConversion = ""; char alphaNumeric; public static final String[] morseAlpha = {".-", "-...", "-.-.", "-..", ".", "..-.", //a-f index 0-5 "--.", "....", "..", ".---", "-.-", ".-..", //g-l index 6-11 "--", "-.", "---", ".--.", "--.-", ".-.", "...", //q-s index 12-18 "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; //t-z index 19-25 public static final String[] morseNumeric = {"-----", ".----", "..---", "...--", "....-", ".....", //0-5 index 0-5 "-....", "--...", "---..", "---."}; //6-9 index 6-9 // calling the superclass' constructor to setup and instantiate GUI public Morse() { // call JFrame constructor to set title bar string super( "Kelli Wiseth English-to-Morse Code Converter" ); // obtain content pane and set layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout(FlowLayout.LEFT, 10, 14) ); promptLabel = new JLabel( "Enter the word (or words) that you want converted to Morse code:"); container.add( promptLabel ); inputField = new JTextField( 35 ); inputField.setFont(new Font("Monospace", Font.PLAIN, 14)); inputField.addActionListener( new ActionListener() { // anonymous inner class for event handling // event handler for text field public void actionPerformed( ActionEvent event ) { outputArea.setText(""); String inputWord = inputField.getText().toUpperCase(); for (int ctr = 0; ctr < inputWord.length(); ctr++) { alphaNumeric = inputWord.charAt(ctr); if (Character.isDigit(alphaNumeric)) //return array index - 48 (0=48, 9=57) outputArea.append(morseNumeric[alphaNumeric-48] + " "); else if (Character.isLetter(alphaNumeric)) //return array index - 65 (A=65, Z=90) outputArea.append(morseAlpha[alphaNumeric-65] + " "); // morseConversion +=morseAlpha[alphaNumeric-65]; else if (alphaNumeric ==' ') //spaceband ASCII value is 32 outputArea.append(" "); //return 3 spaces; } outputArea.append("\n"); } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener container.add(inputField); outputArea = new JTextArea( 8, 48 ); outputArea.setFont(new Font("Monospaced", Font.BOLD, 14)); outputArea.setEditable( false ); container.add(new JScrollPane(outputArea)); setSize( 420, 300 ); // set the window size setVisible( true ); // show the window outputArea.setText(""); }// end constructor Morse public static void main(String args[]) { Morse application = new Morse(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // closing brace main method } // closing brace class Morse