// Demo 17: MartianTranslatorApplet.java // A Martian To English and English To Martian Translator // A Java Applet // // by Charles W. Neville, 1998 // Copyright Charles W. Neville, 1998 // Those of you who remember the Trix Rabbit will remember the cereal box where // he went to Mars. All the kids (the Martian kids in this case) got Trix, but // the poor Trix Rabbit didn't get any at all, because, as usual, "Silly // Rabbit, Trix are for kids!" This was most unfair to our hero because // he made a great discovery on his trip to Mars: The Martian language is // simply English spelled backwards! Personally, I feel NASA should reward our // intrepid and learned Rabbit for his great discovery by immediately providing // him with a bowl of Trix. But in the meantime, we can exploit his discovery // by writing a Martian to English and English to Martian translator. // The idea behind our Martian to English translator is simple: Convert a // String entered by the user in a TextField to an ARRAY of chars. Then copy // the array of chars BACKWARDS to another array of chars, and convert it back // to a String. Finally, draw the string on the applet panel. // Note that because reversing a string twice gives you (a copy of) the // original string back, our translator works both ways: You can translate from // English to Martian as easily as you can translate from Martian to English. // Also, our translator gives us valuable practice with arrays and Strings. // The key work is done in the translate method. Finally, just to dress up the // user interface, we get an introduction to another AWT component, namely // Buttons, and we get an introduction to multiple event handling in the // actionPerformed method using the getActionCommand() method of an ActionEvent // object. import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class MartianToEnglishApplet extends Applet implements ActionListener { // User interface variables Label inputPrompt; // prompt on Applet Panel for TextField input TextField inputField; // where we enter either Martian or English // sentences. String outputPrompt; // where we say whether output is in Martian // or English Button martianButton; // press to translate from Martian to English Button englishButton; // press to translate from English to Martian // Translator variables char[ ] inputChars; // char array holding input characters char[ ] outputChars; // char array holding output (translated) characters String inputString; // String holding input string String outputString; // String holding output (translated) string // build Applet panel and initialize, as usual. public void init() { // create new instances of labels, textfields and buttons inputPrompt = new Label("Martian: "); inputField = new TextField(50); // 50 characters possible input englishButton = new Button("English"); martianButton = new Button("Martian"); // add components to Applet Panel add(inputPrompt); add(inputField); add(martianButton); add(englishButton); // Register TextField and Buttons as the ActionListeners // (ActionEvent processors) for this Applet inputField.addActionListener(this); martianButton.addActionListener(this); englishButton.addActionListener(this); outputPrompt = "English: "; // default is Martian to English translation outputString = ""; } // translate from Martian to English, or vice versa public void translate() { inputString = inputField.getText(); // get user's input string inputChars = inputString.toCharArray(); // convert to char array outputChars = new char[inputChars.length]; // create array // outputChars is inputChars backwards for(int i = 0; i < inputChars.length; i++) { outputChars[inputChars.length - 1 - i] = inputChars[i]; } outputString = String.valueOf(outputChars); } public void paint(Graphics g) { g.drawString(outputPrompt + outputString, 25, 100); } public void actionPerformed(ActionEvent e) { // method getActionCommand() returns the String value of the // ActionEvent e. In the case of a Button click, it returns the // the title string on the Button. // when comparing two strings for content equality, // ALWAYS use method equals NEVER use == if( e.getActionCommand().equals("Martian") ) { inputPrompt.setText("Martian: "); // set text string on label // to "Martian" outputPrompt = "English: "; // we will draw "English" on Applet panel } else if( e.getActionCommand().equals("English") ){ inputPrompt.setText("English: "); // set text string on label // to "English" outputPrompt = "Martian: "; // we will draw "Martian" on Applet panel } else { // we pressed Enter key while in input text field translate(); } repaint(); } }