// brad geilfuss - mas962 - ps1, p2 & p3 import java.awt.*; import java.util.*; import java.applet.*; import java.io.*; import java.net.*; public class translateTextPlus extends java.applet.Applet { Hashtable alphabet = new Hashtable(100); TextField textField; Button button; TextArea textArea; Font FieldFont = new Font("Helvetica", Font.PLAIN, 14); Font AreaFont = new Font("Courier", Font.PLAIN, 24); public void init(){ System.out.println("------------------------------"); System.out.println("b. geilfuss"); System.out.println("translateTextPlus update 9/12 2:30"); System.out.println("------------------------------"); readIn("http://acg.media.mit.edu/mas962/ps1/code.txt"); // read in hashtable readIn("http://acg.media.mit.edu/mas962/users/geilfuss/ps1/p3/code.txt"); // here is the setup. nothing special. GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); this.setLayout(gridbag); this.setBackground(Color.white); this.setForeground(Color.black); c.weightx=1.0; c.weighty=1.0; c.fill = GridBagConstraints.BOTH; c.gridwidth=GridBagConstraints.REMAINDER; textArea = new TextArea(); textArea.setEditable(false); gridbag.setConstraints (textArea, c); c.weighty=0.0; c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = GridBagConstraints.REMAINDER; button = new Button ("parse."); gridbag.setConstraints(button, c); textField = new TextField(20); gridbag.setConstraints (textField, c); add(textArea); add(textField); add(button); textArea.setFont(AreaFont); textField.setFont(FieldFont); } public boolean action (Event e, Object arg) { Object target = e.target; if (target==button) { textArea.setText(""); // erase text area String text = new String(textField.getText()); String translatedWord, Modifier=""; char[] letter = new char[1]; int lineLength = 0; int TAlength = this.size().width-40; int w; for(int i=0; iTAlength && w<(TAlength+1)) { textArea.appendText("\n"); lineLength = w; } if (w>TAlength) { translatedWord= breakWord(translatedWord, TAlength, lineLength-w); } textArea.appendText(translatedWord); } else { Modifier=Modifier+new String(letter); } } } return true; } public String breakWord (String WordIn, int w, int length) { char[] Letter = new char[1]; String Word=""; for (int j=0; jw){ Word+= "\n"; length=0; Word+=new String(Letter); } else { Word+=new String(Letter); } } return (Word); } public void readIn(String urlName) { try { URL is = new URL(urlName); StreamTokenizer st = new StreamTokenizer(is.openStream()); // this parser assumes no broken files! // therefore barring EOF, i'm going // to make TWO nextTokens calls and only test for the first. System.out.println("reading from:"+urlName); String letter; String word; while (st.nextToken() != st.TT_EOF) { st.toString(); letter = st.sval; st.nextToken(); st.toString(); word = st.sval; System.out.println("read:"+letter+","+word); alphabet.put(letter, word); } } catch (IOException e) { System.out.println("!"); } } }