// this is a simple
calculator Applet pogram..
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
public
class calc extends Applet implements ActionListener
{
int n=0,n1,p;
Label header,lb1,lb2,lb3,footer;
TextField tf1,tf2;
Button
b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4,e1,e2,e3,e4,f1,f2,f3,f4;
Font fo1,fo2;
Panel
head,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,foot;
Image img;
public void init()
{
img=getImage(getCodeBase(),"Winter.jpeg");
fo1=new Font("courier
New",Font.BOLD,50);
fo2=new Font("courier
New",Font.BOLD,25);
head=new Panel();
head.setForeground(Color.red);
head.setBackground(Color.pink);
head.setFont(fo1);
header=new Label("calculator");
head.add(header);
add(head);
p1=new Panel();
p1.setForeground(Color.red);
p1.setBackground(Color.BLACK);
p1.setFont(fo2);
tf1=new TextField(10);
p1.add(tf1);
add(p1);
p2=new Panel();
p2.setForeground(Color.red);
p2.setBackground(Color.DARK_GRAY);
p2.setFont(fo2);
b1=new Button("c ");
b2=new Button(". ");
b3=new Button("% ");
b4=new Button("/ ");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
add(p2);
p3=new Panel();
p3.setForeground(Color.red);
p3.setBackground(Color.DARK_GRAY);
p3.setFont(fo2);
c1=new Button("7 ");
c2=new Button("8 ");
c3=new Button("9 ");
c4=new Button("* ");
p3.add(c1);
p3.add(c2);
p3.add(c3);
p3.add(c4);
add(p3);
p4=new Panel();
p4.setForeground(Color.red);
p4.setBackground(Color.DARK_GRAY);
p4.setFont(fo2);
d1=new Button("4 ");
d2=new Button("5 ");
d3=new Button("6 ");
d4=new Button("- ");
p4.add(d1);
p4.add(d2);
p4.add(d3);
p4.add(d4);
add(p4);
p5=new Panel();
p5.setForeground(Color.red);
p5.setBackground(Color.DARK_GRAY);
p5.setFont(fo2);
e1=new Button("1 ");
e2=new Button("2 ");
e3=new Button("3 ");
e4=new Button("+ ");
p5.add(e1);
p5.add(e2);
p5.add(e3);
p5.add(e4);
add(p5);
p6=new Panel();
p6.setForeground(Color.red);
p6.setBackground(Color.DARK_GRAY);
p6.setFont(fo2);
f1=new Button("0 ");
f2=new Button(". ");
f3=new Button("+/-");
f4=new Button("= ");
p6.add(f1);
p6.add(f2);
p6.add(f3);
p6.add(f4);
add(p6);
p7=new Panel();
p7.setBackground(Color.DARK_GRAY);
add(p7);
p8=new Panel();
p8.setBackground(Color.DARK_GRAY);
add(p8);
p9=new Panel();
p9.setBackground(Color.DARK_GRAY);
add(p9);
p10=new Panel();
p10.setBackground(Color.DARK_GRAY);
add(p10);
GridLayout gr1=new GridLayout(12,1);
setLayout(gr1);
foot=new Panel();
foot.setForeground(Color.RED);
foot.setBackground(Color.PINK);
foot.setFont(fo1);
footer=new Label("calculatio
here");
foot.add(footer);
add(foot);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
d1.addActionListener(this);
d2.addActionListener(this);
d3.addActionListener(this);
d4.addActionListener(this);
e1.addActionListener(this);
e2.addActionListener(this);
e3.addActionListener(this);
e4.addActionListener(this);
f1.addActionListener(this);
f2.addActionListener(this);
f3.addActionListener(this);
f4.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawImage(img,0, 0,this);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==b1)
{
tf1.setText(" ");
}
else if (ae.getSource()==b2)
{
tf1.setText(".");
}
else if (ae.getSource()==b3)
{
tf1.setText("%");
n=(n*2)/100;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==b4)
{
p=3;
n1=n;
n=0;
}
else if (ae.getSource()==c1)
{
tf1.setText("");
n=(n*10)+7;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==c2)
{
tf1.setText("8");
n=(n*10)+8;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==c3)
{
tf1.setText("9");
n=(n*10)+9;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==c4)
{
p=2;
n1=n;
n=0;
}
else if (ae.getSource()==d1)
{
tf1.setText("4");
n=(n*10)+4;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==d2)
{
tf1.setText("5");
n=(n*10)+5;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==d3)
{
tf1.setText("6");
n=(n*10)+6;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==d4)
{
p=0;
n1=n;
n=0;
}
else if (ae.getSource()==e1)
{
tf1.setText("1");
n=(n*10)+1;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==e2)
{
tf1.setText("2");
n=(n*10)+2;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==e3)
{
tf1.setText("3");
n=(n*10)+3;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==e4)
{
p=1;
n1=n;
n=0;
}
else if (ae.getSource()==f1)
{
tf1.setText("0");
n=(n*10)+0;
tf1.setText(String.valueOf(n));
}
else if (ae.getSource()==f2)
{
tf1.setText(".");
}
else if (ae.getSource()==f3)
{
tf1.setText("+/-");
}
else if (ae.getSource()==f4)
{
switch(p)
{
case 0:{
try
{
int s=n1-n;
tf1.setText("");
tf1.setText(String.valueOf(s));
n=n1=0;
}
catch(NumberFormatException fe)
{
System.out.println("\n\n Input charcter is not support..");
}
}
break;
case 1:{
try
{
int s=n1+n;
tf1.setText("");
tf1.setText(String.valueOf(s));
n=n1=0;
}
catch(NumberFormatException fe)
{
System.out.println("\n\n Input charcter is not support..");
}
}
break;
case 2:{
try
{
int s=n1*n;
tf1.setText("");
tf1.setText(String.valueOf(s));
n=n1=0;
}
catch(NumberFormatException fe)
{
System.out.println("\n\n Input charcter is not support..");
}
}
break;
case 3:{
try
{
int s=n1/n;
tf1.setText("");
tf1.setText(String.valueOf(s));
n=n1=0;
}
catch(NumberFormatException fe)
{
System.out.println("\n\n Input charcter is not support..");
}
}
break;
}
}
}
}