/************************************************************** / Circumference Applet / IKE / Started: 6/17/01 /**************************************************************/ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Circum extends Applet implements ActionListener { public TextField inputDiam, inputRad, inputResult; public Label labelTitle, labelDiam, labelRad, labelResult; public Font f; public Color newColor = new Color(120, 25, 151); public Button b1, b2; public double diam, circum, rad; public double pi = 3.14159265359; public Panel p1, p2, p3; public void init() { p1 = new Panel(new GridLayout(1, 1, 0, 0)); p2 = new Panel(new GridLayout(3, 2, 0, 0)); p3 = new Panel(new GridLayout(1, 2, 0, 0)); setLayout(new GridLayout(3, 1, 0, 0)); setBackground(Color.lightGray); setForeground(newColor); f = new Font("Comic Sans MS", Font.BOLD, 10); setFont(f); inputDiam = new TextField(6); inputDiam.setBackground(Color.white); inputRad = new TextField(6); inputRad.setEditable(false); inputRad.setBackground(Color.white); inputResult = new TextField(6); inputResult.setEditable(false); inputResult.setBackground(Color.white); labelTitle = new Label(" Circumference Calculator "); labelDiam = new Label("Enter the Diameter: "); labelRad = new Label("The Radius is: "); labelResult = new Label("The Circumference is: "); b1 = new Button("Calculate"); b1.addActionListener(this); b1.setBackground(newColor); b1.setForeground(Color.white); b2 = new Button("Clear"); b2.addActionListener(this); b2.setBackground(newColor); b2.setForeground(Color.white); p1.add(labelTitle); p2.add(labelDiam); p2.add(inputDiam); p2.add(labelRad); p2.add(inputRad); p2.add(labelResult); p2.add(inputResult); p3.add(b1); p3.add(b2); add(p1); add(p2); add(p3); } public void actionPerformed(ActionEvent e) { String str; str = e.getActionCommand(); if (str.equals ("Calculate")) { diam = Integer.parseInt(inputDiam.getText()); circum = diam * pi; rad = diam / 2; inputRad.setText(rad + ""); inputResult.setText(circum + ""); } else if (str.equals ("Clear")) { inputDiam.setText (""); inputRad.setText (""); inputResult.setText (""); } repaint(); } }