import java.awt.*; import java.applet.Applet; public class TextInput extends Applet { TextField textField; TextArea textArea; Button bt; Parser parse; FontMetrics fm; int taWidth, remainingSpace, halfRemSpace; boolean crack, stepsUp, stepsDown; int leftin, uc, lc, space, bspace, tab, stepsCount; public void init() { textField = new TextField(20); textArea = new TextArea(5, 20); textArea.setEditable(false); bt = new Button(" ... > zap < ... "); Color nblue = new Color(0, 55, 255); bt.setBackground(nblue); bt.setForeground(nblue); textArea.setBackground(Color.white); //Add Components to the Applet. GridBagLayout gridBag = new GridBagLayout(); setLayout(gridBag); GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(textField, c); add(textField); c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(bt, c); add(bt); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; gridBag.setConstraints(textArea, c); add(textArea); validate(); // width of applet! taWidth = this.size().width - 50; remainingSpace = taWidth; halfRemSpace = taWidth/2; //init the parser parse = new Parser(); parse.start(this); parse.addExtraChars(); fm = textArea.getFontMetrics(textArea.getFont()); leftin = 0; space = 0; uc = 0; lc = 0; bspace = 0; tab = 0; stepsCount = 0; crack = false; stepsUp = false; stepsDown = false; } public boolean action(Event evt, Object arg) { Object target = evt.target; if (target == bt) { //reset stuff textArea.setText(new String("")); remainingSpace = taWidth; leftin = 0; space = 0; uc = 0; lc = 0; bspace = 0; tab = 0; crack = false; stepsUp = false; stepsDown = false; stepsCount = 0; String text = textField.getText(); int i = 0; while(text.length() > i) { char c[] = new char[1]; c[0] = text.charAt(i); String key = new String(c); key = key.toLowerCase(); String obj = parse.p2Parse(key); wrap(obj); textField.selectAll(); i = i+1; } } return true; } private void wrap(String obj) { if(obj != null) { obj = preProc(obj); int thisWord = fm.stringWidth(obj); if(thisWord > remainingSpace){ textArea.appendText("\n"); remainingSpace = taWidth; } pad(); textArea.appendText(obj); remainingSpace = remainingSpace - thisWord; } } private String preProc(String obj) { if(uc > 0) { obj = obj.toUpperCase(); uc = uc - 1; } if(lc > 0) { obj = obj.toLowerCase(); lc = lc - 1; } if(space > 0) { int t = obj.length(); char foo[] = new char[(space + 1)*t]; char bar[] = new char[t]; bar = obj.toCharArray(); int it = 0; while(it < t){ int temp2 = it*(space+1); foo[temp2] = bar[it]; int temp3 = 0; while(temp3 < space) { foo[temp2+temp3+1] = ' '; temp3 = temp3 +1; } it = it + 1; } obj = new String(foo); } if(tab > 0) { int tabtemp = tab; while(tabtemp > 0) { obj = obj + "\t"; tabtemp = tabtemp - 1; } } if(bspace > 0) { int tempbsp = bspace; while(tempbsp > 0) { obj = obj + " "; tempbsp = tempbsp -1; } } if(crack == true) { if((remainingSpace >= halfRemSpace) && ((remainingSpace - fm.stringWidth(obj)) <= halfRemSpace)) { obj = obj + " "; } } return obj; } private void pad() { if (remainingSpace == taWidth) { int foo = leftin + stepsCount; String sp = new String(" "); while(foo > 0) { textArea.appendText(sp); remainingSpace = remainingSpace - fm.stringWidth(sp); foo = foo -1; } if(stepsUp) stepsCount = (stepsCount + 1) % 100 ; if(stepsDown) stepsCount = (stepsCount - 1) % 100; } } }