the Average applet again
// filename: AverageAppletWithFontAndColor.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class AverageAppletWithFontAndColor extends Applet implements ActionListener
{
public CustomAverage average; // instantiate an CustomAverage object called average
public TextField input1, input2, display;
public Label labelTitle, label1Input, label2Input, label3Display;
// new for this discussion
public Color newColor;
public Font newFont;
public CustomButton b1;
public int num1, num2;
public void init( )
{
// new for this discussion
newColor = new Color(125, 0, 255);
newFont = new Font("Helvetica", Font.BOLD + Font.ITALIC, 18);
b1 = new CustomButton("Push for Average", newColor, Color.yellow, newFont);
// much of this is unchanged from the previous discussion
average =new CustomAverage( );
input1 = new TextField(12);
input2 = new TextField(12);
display = new TextField(12);
input1.setText("0");
input2.setText("0");
display.setText("");
display.setEditable(false);
labelTitle = new Label("Average Calculator");
label1Input = new Label("First Number");
label2Input = new Label("Second Number");
label3Display = new Label("Computed Average");
b1.addActionListener(this);
add(labelTitle); add(label1Input);
add(input1); add(label2Input);
add(input2); add(b1);
add(label3Display); add(display);
}
public void actionPerformed(ActionEvent e)
{
display.setText
(""+ average.getAverage(Integer.parseInt(input1.getText()),
Integer.parseInt(input2.getText()) ));
}
}
more Helper class CustomLabel and CustomTextField
// filename: CustomLabel.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CustomLabel extends Label
{
private Color myBackgroundColor, myForegroundColor;
private String myLabel;
private Font myFont;
public CustomLabel(String str, Color fgColor, Color bgColor, Font ft)
{
super(str);
myLabel = str;
myBackgroundColor = bgColor;
myForegroundColor = fgColor;
myFont = ft;
setBackground(myBackgroundColor);
setForeground(myForegroundColor);
setFont(ft);
}
}
// filename: CustomTextField.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CustomTextField extends TextField
{
private Color myBackgroundColor, myForegroundColor;
private String myLabel;
private Font myFont;
private int mySize;
private boolean myCanEdit;
public CustomTextField(int size, String str, Color fgColor,
Color bgColor, Font ft, boolean canEdit)
{
super(size);
myLabel = str;
myBackgroundColor = bgColor;
myForegroundColor = fgColor;
myFont = ft;
mySize = size;
myCanEdit = canEdit;
setBackground(myBackgroundColor);
setForeground(myForegroundColor);
setFont(ft);
setText(str);
setEditable(myCanEdit);
}
}
the New Averaging Applet using Helper class
// filename: NewAverageApplet.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class NewAverageApplet extends Applet implements ActionListener
{
public CustomAverage average; // make a CustomAverage object called average
public CustomButton b1;
public CustomTextField input1, input2, display;
public CustomLabel LabelTitle, Label1input, Label2input, Label3display;
public Color newColor;
public Font newFont;
public int num1, num2;
public void init( )
{
newColor = new Color(125, 0, 255);
newFont = new Font("Helvetica", Font.BOLD + Font.ITALIC, 24);
average =new CustomAverage( );
b1 = new CustomButton("Push for Average", newColor,
Color.yellow, newFont);
input1 = new CustomTextField(12,"0", newColor, Color.yellow,
newFont, true);
input2 = new CustomTextField(12, "0", newColor,
Color.lightGray, newFont, true);
display = new CustomTextField(12, "", newColor, Color.red,
newFont, false);
LabelTitle = new CustomLabel("Average Calculator", newColor,
Color.pink, newFont );
Label1input = new CustomLabel("First Number", newColor,
Color.white, newFont);
Label2input = new CustomLabel("Second Number", newColor,
Color.orange, newFont);
Label3display = new CustomLabel("Computed Average", newColor,
Color.gray, newFont);
b1.addActionListener(this);
add(LabelTitle);
add(Label1input);
add(input1);
add(Label2input);
add(input2);
add(b1);
add(Label3display);
add(display);
}
public void actionPerformed(ActionEvent e)
{
display.setText(""+
average.getAverage(Integer.parseInt(input1.getText() ),
Integer.parseInt(input2.getText() ) ));
}
}