Tuesday, November 25, 2008

Swing Calculator

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class Calc7Swing extends JApplet implements ActionListener
{
// ---------------------------------------------------------
// 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);
JTextArea total = new JTextArea(5, 25);
JLabel output = new JLabel(" ");
Container c = getContentPane();
double memVal = 0;
double curVal = 0;
char tempVal;
double result = 0;
//------------------------------
//--actionPerformed--
//------------------------------
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
char c = arg.charAt(0);
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.append(""+curVal+"\n");
total.append("=============\n");
total.append(""+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.append(padText(memstr,memstr.length()));
// Clear the output
curVal = 0;
output.setText(" ");

}
//------------------------------------------------------------------------------------
}


}
//-----------------------------

// ---------------------------------------------------------
// 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);
c.setLayout(new BorderLayout(5,5));
c.add( makeButtons());
//c.add(getContentPane());
//add("Center", makeButtons());

}

// ----------------------------------------------------------
// 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 JPanel makeButtons()
{
//output= new Label();
//total = new TextArea();
// Create 4 Panels [p1 through p4]
JPanel p = new JPanel(new BorderLayout(5,5));
JPanel p1 = new JPanel(new BorderLayout(5,5));
JPanel p2 = new JPanel(new BorderLayout(5,5));
JPanel p3 = new JPanel(new GridLayout(1,3,5,5));
JPanel p4 = new JPanel(new GridLayout(4,5,5,5));
JPanel p5 = new JPanel(new BorderLayout(5,5));
JButton backspaceBtn = new JButton("Backspace");
JButton ceBtn = new JButton("CE");
JButton cBtn = new JButton("C");
JButton n7Btn = new JButton("7");
JButton n8Btn = new JButton("8");
JButton n9Btn = new JButton("9");
JButton dBtn = new JButton("/");
JButton sqrtBtn = new JButton("sqrt");
JButton n4Btn = new JButton("4");
JButton n5Btn = new JButton("5");
JButton n6Btn = new JButton("6");
JButton mBtn = new JButton("*");
JButton modBtn = new JButton("%");
JButton n1Btn = new JButton("1");
JButton n2Btn = new JButton("2");
JButton n3Btn = new JButton("3");
JButton miBtn = new JButton("-");
JButton dxBtn = new JButton("1/x");
JButton bn0Btn = new JButton("0");
JButton rBtn = new JButton("+/-");
JButton pBtn = new JButton(".");
JButton plBtn = new JButton("+");
JButton eBtn = new JButton("=");

backspaceBtn.addActionListener(this);
ceBtn.addActionListener(this);
cBtn.addActionListener(this);
n7Btn.addActionListener(this);
n8Btn.addActionListener(this);
n9Btn.addActionListener(this);
dBtn.addActionListener(this);
sqrtBtn.addActionListener(this);
n4Btn.addActionListener(this);
n5Btn.addActionListener(this);
n6Btn.addActionListener(this);
mBtn.addActionListener(this);
modBtn.addActionListener(this);
n1Btn.addActionListener(this);
n2Btn.addActionListener(this);
n3Btn.addActionListener(this);
miBtn.addActionListener(this);
dxBtn.addActionListener(this);
bn0Btn.addActionListener(this);
rBtn.addActionListener(this);
pBtn.addActionListener(this);
plBtn.addActionListener(this);
eBtn.addActionListener(this);

// 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(backspaceBtn);
p3.add(ceBtn);
p3.add(cBtn);
// Add Number buttons to p4
p4.setForeground(new Color(0, 0, 235));
p4.setFont(new Font("Dialog", Font.PLAIN, 14));
p4.add(n7Btn);
p4.add(n8Btn);
p4.add(n9Btn);
p4.add(dBtn);
p4.add(sqrtBtn);
p4.add(n4Btn);
p4.add(n5Btn);
p4.add(n6Btn);
p4.add(mBtn);
p4.add(modBtn);
p4.add(n1Btn);
p4.add(n2Btn);
p4.add(n3Btn);
p4.add(miBtn);
p4.add(dxBtn);
p4.add(bn0Btn);
p4.add(rBtn);
p4.add(pBtn);
p4.add(plBtn);
p4.add(eBtn);
// 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));
JScrollPane scrollPane = new JScrollPane(total,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
p5.add("Center", scrollPane);
//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;
}

}