Tuesday, November 25, 2008

Awt Calculator

import java.awt.*;
import java.applet.*;
public class Calc7 extends Applet
{
// ---------------------------------------------------------
// Attributes:
// TextArea total displays running total of calculations
// Label output shows current number being entered
// doubles memVal and curVal are used for calculations
// ---------------------------------------------------------
TextArea total = new TextArea("", 5, 25, TextArea.SCROLLBARS_VERTICAL_ONLY);
Label output = new Label();
double memVal = 0;
double curVal = 0;
char tempVal;
double result = 0;
// ---------------------------------------------------------
// The init() method sets the layout and color.
// It then calls the makeButtons() to do the actual layout
// for the calculator
// ---------------------------------------------------------
public void init()
{
setLayout(new BorderLayout(5,5));
setBackground(Color.lightGray);
add("Center", makeButtons());

}
//}
// ---------------------------------------------------------
// The action() method handles the button presses. Since
// this is a Java 1.0 applet, all button actions go through
// the action() method.
// ---------------------------------------------------------
public boolean action(Event ev, Object o)
{
// Local variables
String arg = (String) o;
// Key that was pressed
char c = arg.charAt(0);
// First char of key caption
String s = output.getText();
// Current value of output
// "Special" keys operate on current value
if (arg.equals("Backspace")) backSpace(s);
else if (arg.equals("C")) clearAll();
else if (arg.equals("CE")) clearEntry();
else if (arg.equals("sqrt")) setCurVal(Math.sqrt(curVal));
else if (arg.equals("1/x")) setCurVal(1.0/curVal);
else if (arg.equals("+/-")) setCurVal(-curVal);
//-----------------------------------------------------------------------------------
else{
// Digit keys are always added to current value
if (c >= '0' && c <= '9') setCurVal(s + c);

// Decimal point added only if not already in output
else if (c == '.')
{
if (s.indexOf(c) < 0) setCurVal(s + c);
}
else if (c == '=')
{
switch (tempVal)
{
case '/': result = memVal/curVal;break;
case '*': result = memVal*curVal;break;
case '-': result = memVal-curVal;break;
case '+': result = memVal+curVal;break;
case '%': result = memVal%curVal;break;
}
String memstr = "" + result + " \n";
total.appendText(""+curVal+"\n");
total.appendText("=============\n");
total.appendText(""+memstr);
//total.enable();
curVal = 0;
output.setText("");

}
// Handle all of the operator keys
else
{
switch (c)
{
case '/': memVal = curVal; tempVal = '/';break;
case '*': memVal = curVal; tempVal = '*';break;
case '-': memVal = curVal; tempVal = '-';break;
case '+': memVal = curVal; tempVal = '+';break;
case '%': memVal = curVal; tempVal = '%';break;
}

// Display results on the TextArea named total
String memstr = "" + curVal + " " + c + " \n";
total.appendText(padText(memstr,memstr.length()));
// Clear the output
curVal = 0;
output.setText("");

}
//------------------------------------------------------------------------------------
}
return true;
}
// ----------------------------------------------------------
// These are utility methods
// ----------------------------------------------------------

private String padText(String s, int size)
{
String temp = " " + s;
return temp.substring(temp.length() - size);
}
// Handles the backspace key
private void backSpace(String s)
{
if(s.length()>0)
{
s = s.substring(0, s.length()-1);
setCurVal(s);
}
}
// Handles the 'C' [Clear All] key
private void clearAll()
{
total.setText("");
output.setText("");
curVal = memVal = 0;
}
// Handles the 'CE' [Clear Entry] key
private void clearEntry()
{
output.setText("");
curVal = 0;
}
// Sets the current value, using a String
private void setCurVal(String s)
{

output.setText(s);
try{
curVal = (new Double(s)).doubleValue();
}catch(NumberFormatException ex)
{
}
}
// Sets the current value, using a number
private void setCurVal(double newValue)
{
curVal = newValue;
output.setText(""+newValue);
}
// ---------------------------------------------------------
// This lays out the appearance of the applet, using
// BorderLayout and GridLayout
// The colors used for the buttons may, or may not, appear
// on your copy, depending upon your Web browser
// ---------------------------------------------------------

private Panel makeButtons()
{

// Create 4 Panels [p1 through p4]
Panel p = new Panel(new BorderLayout(5,5));
Panel p1 = new Panel(new BorderLayout(5,5));
Panel p2 = new Panel(new BorderLayout(5,5));
Panel p3 = new Panel(new GridLayout(1,3,5,5));
Panel p4 = new Panel(new GridLayout(4,5,5,5));
Panel p5 = new Panel(new BorderLayout(5,5));
// Add Backspace, CE, and C buttons to p3
p3.setForeground(new Color(232,0,0));
p3.setFont(new Font("Dialog", Font.PLAIN, 12));
p3.add(new Button("Backspace"));
p3.add(new Button("CE"));
p3.add(new Button("C"));
// Add Number buttons to p4
p4.setForeground(new Color(0, 0, 235));
p4.setFont(new Font("Dialog", Font.PLAIN, 14));
p4.add(new Button("7"));
p4.add(new Button("8"));
p4.add(new Button("9"));
p4.add(new Button("/"));
p4.add(new Button("sqrt"));
p4.add(new Button("4"));
p4.add(new Button("5"));
p4.add(new Button("6"));
p4.add(new Button("*"));
p4.add(new Button("%"));
p4.add(new Button("1"));
p4.add(new Button("2"));
p4.add(new Button("3"));
p4.add(new Button("-"));
p4.add(new Button("1/x"));
p4.add(new Button("0"));
p4.add(new Button("+/-"));
p4.add(new Button("."));
p4.add(new Button("+"));
p4.add(new Button("="));
// Add and initialize output and total
// [Label and TextArea for output]
output.setBackground(Color.white);
output.setForeground(Color.black);
output.setFont(new Font("Courier", Font.BOLD, 18));
total.setBackground(new Color(255, 255, 128));
total.setForeground(Color.black);
total.setFont(new Font("Courier", Font.BOLD, 14));
p5.add("Center", total);
p5.add("South", output);
// Hook up the panels
p2.add("North", p3);
p2.add("Center", p4);
// Add some spacing around outside
p1.add("North", p5);
p1.add("Center", p2);
p.setFont(new Font("Helvetica", Font.PLAIN, 6));
p.add("North", new Label(" "));
p.add("East", new Label(" "));
p.add("West", new Label(" "));
p.add("South", new Label(" "));
p.add("Center", p1);

return p;
}

}