//Calculator Applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code=calc width=400 height=350>
</applet>
*/
public class calc extends Applet implements ActionListener // step1
{
double currentValue = 0;// currentValue stores the number currently being displayed
double previousValue = 0;// previousValue stores the number that was displayed before
boolean waitingForDigit = true;
char lastOperation = '=';
boolean computationError = false;
JTextField MainDisplay;
Font f1 = new Font("LCD", 1, 21);
Font f2 = new Font("DigifaceWide", 1, 30);
LayoutManager Layout;
JPanel pNum,pMast,pSym;
JButton b[];//button dec
public void init()
{
pNum=new JPanel();
pSym=new JPanel();
pNum.setBackground(Color.BLUE);
pSym.setBackground(Color.BLACK);
pMast=new JPanel();
pMast.setBackground(Color. BLACK);
MainDisplay=new JTextField();
MainDisplay.setFont(f2);
b=new JButton[23];
for(int i=0;i<=9;i++)
b[i]=new JButton(String.valueOf(i));
b[10] = new JButton("+/-");
b[11] = new JButton(".");
b[12] = new JButton("=");
b[13] = new JButton("/");
b[14] = new JButton("*");
b[15] = new JButton("-");
b[16] = new JButton("+");
b[17] = new JButton("sqrt");
b[18] = new JButton(" 1/x");
b[19] = new JButton("Sin");
b[20] = new JButton("cos");
b[21] = new JButton("tan");
b[22] = new JButton("C");
//seting color for buttons
for (int i = 0; i < b.length; i++)
{
b[i].setFont(f1);
if (i < 10)
b[i].setForeground(Color.blue);
else
b[i].setForeground(Color.red);
}
pNum.setLayout (new GridLayout(4, 3, 2, 2));
pSym.setLayout (new GridLayout(4, 3, 2, 2));
//Set the NUmbers
for (int i = 7; i <= 9; i++)
pNum.add(b[i]);
for (int i = 4; i <= 6; i++)
pNum.add(b[i]);
for (int i = 1; i <= 3; i++)
pNum.add(b[i]);
pNum.add(b[0]);//0
pSym.add(b[19]);//SIN
pSym.add(b[20]);//COS
pSym.add(b[21]);//TAN
pSym.add(b[17]);//sqrt
pSym.add(b[10]);//+-
pSym.add(b[18]);//1/x
pSym.add(b[16]);//+
pSym.add(b[15]);//sub
pSym.add(b[14]);// mul
pSym.add(b[13]);//division
pSym.add(b[22]);//C
pSym.add(b[12]);//=
for (int i = 0; i<=22; i++)
b[i].addActionListener(this);
pMast.setLayout(new BorderLayout(10,10));
pMast.add(pNum, BorderLayout.WEST);
pMast.add(pSym, BorderLayout.EAST);
pMast.add(MainDisplay,BorderLayout.NORTH);
this.setLayout(new BorderLayout(10,10));
add(pMast, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)//ae contains source event
{
String label = ae.getActionCommand(); // The button label
char first = label.charAt(0); // the first char of the label
String x;
if (first == 'C')
{
currentValue = 0;
waitingForDigit = true;
computationError = false;
} else if (label.equals("+/-"))
{
currentValue = -currentValue;
waitingForDigit = true;
} else if ('0'<=first && first <='9')
{
//int digit = first - '0'; // Convert to an int
x=String.valueOf(first);
int digit=Integer.parseInt(x);
if (waitingForDigit)
{
currentValue = digit;
waitingForDigit = false;
} else
currentValue=(currentValue*10)+digit;
} else if(first==' '||first=='S'||first=='s'||first=='c'||first=='t')
{
lastOperation = first;
computeLastOperation();
lastOperation = '=';
waitingForDigit = false;
} else
{ // +,-,*,/, or =
computeLastOperation();
lastOperation = first;
previousValue = currentValue;
waitingForDigit = true;
}
displayCurrentValue();
}
private void displayCurrentValue()
{
if (computationError)
MainDisplay.setText(" Error ");
else
MainDisplay.setText(Double.toString(currentValue));
}
private void computeLastOperation()
{
switch (lastOperation)
{
case '=':
break;
case 's':currentValue=Math.sqrt(currentValue);
break;
case '+':currentValue=previousValue + currentValue;
break;
case '-':currentValue=previousValue - currentValue;
break;
case '*':currentValue=previousValue * currentValue;
break;
case ' ':
if (currentValue==0)
computationError = true;
else
currentValue = 1 / currentValue;
displayCurrentValue() ;
break;
case 'S':currentValue=Math.sin(currentValue);
break;
case 'c':currentValue=Math.cos(currentValue);
break;
case 't':currentValue=Math.tan(currentValue);
break;
case '/':
if (currentValue==0)
computationError = true;
else
currentValue = previousValue / currentValue;
break;
}
}
}