/* IKE Rectangle Information Revamped on 6/17/01 */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class RectInfo extends Applet implements ActionListener { // variables public TextField input1, input2; public TextArea display1; public Button b1, b2; public Label lab1, lab2; public void init() { setBackground(Color.blue); input1 = new TextField(3); input1.setBackground(Color.white); input2 = new TextField(3); input2.setBackground(Color.white); display1 = new TextArea("", 1, 35, display1.SCROLLBARS_NONE); display1.setEditable(false); display1.setBackground(Color.white); b1 = new Button("Area"); b1.setBackground(Color.green); b2 = new Button("Perimeter"); b2.setBackground(Color.green); lab1 = new Label("Height"); lab1.setForeground(Color.white); lab2 = new Label("Width"); lab2.setForeground(Color.white); add(lab1); add(input1); add(lab2); add(input2); add(b1); add(b2); add(display1); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent ae) { int num1, num2, answer = 0; String str; // check for nothing in textField str = input1.getText(); if (str.equals("")) num1 = 0; else num1 = Integer.parseInt(str); str = input2.getText(); if (str.equals("")) num2 = 0; else num2 = Integer.parseInt(str); str = ae.getActionCommand(); if (str.equals ("Area")) { answer = num1 * num2; display1.setText("The area of this rectangle is: " +Integer.toString(answer)); } else { answer = 2 * num1 + 2 * num2; display1.setText("The perimeter of this rectangle is: " +Integer.toString(answer)); } repaint(); } public void paint(Graphics g) { int num1, num2; String str; // check for nothing in textField str = input1.getText(); if (str.equals("")) num1 = 0; else num1 = Integer.parseInt(str); str = input2.getText(); if (str.equals("")) num2 = 0; else num2 = Integer.parseInt(str); g.setColor(Color.green); g.fillRect(2, 75, num2, num1); } }