// Chloe Chao // Caps.java // October 20, 1997 import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; import java.io.*; public class Caps extends Applet { // Globals and constants private Choice choice; private TextArea textArea; private Button button; private Font font; private FontMetrics fontmetrics; private static final int UPPERCASE = 0; private static final int LOWERCASE = 1; public void init() { // Take care of the font info font = new Font("Helvetica", Font.PLAIN, 12); fontmetrics = Toolkit.getDefaultToolkit().getFontMetrics( font ); int gx, gy, cols, rows; gx = 540; gy = 382; cols = (int)((float)(gx-16)/fontmetrics.charWidth('A')) - 1; // subtract 16 for scrollbar rows = (int)((float)gy / fontmetrics.getHeight()) - 3; System.out.println("rows = " + rows + " cols = " + cols ); Panel p = new Panel(); p.setLayout(new GridLayout(0,2)); // Add the choice menu. makeChoice(); p.add("West", choice); // Add the apply button. makeButton(); p.add("East", button); validate(); // Use BorderLayout. BorderLayout b = new BorderLayout(); setLayout(b); // Add the textArea. makeTextArea(rows, cols); add( "North", textArea ); add( "South", p ); validate(); }// end init() public void paint( Graphics g ) { paintComponents(g); }// end paint() public boolean handleEvent(Event e) { if (e.target == button) { String toBeChanged = textArea.getText(); textArea.setText(change(toBeChanged, choice.getSelectedIndex() )); repaint(); return true; } else return false; } private String change( String changeme, int mode ) { switch( mode ) { case UPPERCASE: return changeme.toUpperCase(); case LOWERCASE: return changeme.toLowerCase(); default: return null; } } private void makeChoice() { choice = new Choice(); choice.addItem("to uppercase"); choice.addItem("to lowercase"); } private void makeButton() { button = new Button("do it."); } private void makeTextArea(int r, int c) { textArea = new TextArea("Write a JAVA program with a TextArea widget, a Choice widget, and a Button widget.", r, c); } }// end Caps