After about 1-2 days of work, I finally created a Java Applet of a simple binary calculator which performs binary operations on positive numbers. The main aim was to solve using this calculator an expression like:
32+45/89*6 = 5.19101
from left-to-right, step by step i.e. computing two variables at a time.(so you enter 32 + 45 = and the result (77) then becomes your first operand)
The idea was not to give a demonstration of the calculator algorithm, but to show the use of the applet class and the basic components like buttons, text fields, etc. Thus, this can be the ideal code for anyone who is planning to make a small project using java applet class. The functions and the overall view and flow of the code is deliberately made very simple so that even those who are not-so-experienced with applets can understand.
I encourage the readers to go through the code and mail/leave a comment if you have doubts, suggestions or if you made an improvised version of the code!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.applet.Applet; | |
import java.awt.Button; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.TextField; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JOptionPane; | |
@SuppressWarnings("serial") | |
public class CalculatorApplet extends Applet implements ActionListener{ | |
char operator = ' '; | |
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20); | |
boolean firstVarOver = false; //checks whether the first variable entering is completed | |
boolean isBckspAble = true; //checks whether Back Space is allowed. Back Space is not allowed on the final result, as the result isnt editable, its computed. | |
float result; | |
int whichVar = 0; // whichVar will point the variable number currently processed. Since its a binary calculator, whichVar can be either 1 or 2 | |
String var[] = {"","",""}; // here variable is a string array because only string can be uploaded on the text field. | |
TextField textField = new TextField(""); | |
Button numberButton[] = new Button[10]; //There is an array of 10 numbers | |
Button operatorButton[] = new Button[4]; | |
Button specialButton[] = new Button[3]; | |
Button equal = new Button("="); | |
Button zero = new Button("0"); | |
String operatorArray[] = new String[]{"+", "-", "*", "/"}; | |
String specialArray[] = new String[] {"<-", "C", "."}; | |
public void init(){ | |
setLayout(null); | |
setSize(67*4, 53*6); // the values 67*53 are the dimensions of each button; chosen after trial-and-error | |
textField.setBounds(0,0,67*4,53); | |
textField.setEnabled(false); | |
add(textField); | |
setUpButtons(); | |
} | |
//this function will draw all the buttons on the screen and assign them labels | |
public void setUpButtons(){ | |
int x = -67; | |
int y = 53*2; | |
Button thisButton; | |
//add the number buttons | |
for (int i = 1; i < 10; i++) { | |
if(x >= 67*2){ //only 3 buttons in a row | |
y += 53; | |
x = -67; | |
} | |
thisButton = new Button(Integer.toString(i)); | |
thisButton.setBounds(x+67, y, 67, 53); | |
thisButton.addActionListener(this); | |
x += 67; | |
thisButton.setBackground(Color.green); | |
thisButton.setForeground(Color.red); | |
thisButton.setFont(font); | |
numberButton[i] = thisButton; | |
add(thisButton); | |
} | |
//add 0 | |
zero.setBounds(67*0, 53, 67, 53); | |
zero.setBackground(Color.green); | |
zero.setForeground(Color.red); | |
zero.addActionListener(this); | |
zero.setFont(font); | |
add(zero); | |
//add operator buttons | |
for (int i = 0; i < 4; i++) { | |
thisButton = new Button(operatorArray[i]); | |
thisButton.setBounds(67*i, 53*5, 67, 53); //it is the 5th row, and the operators are in the ith column | |
thisButton.addActionListener(this); | |
thisButton.setBackground(Color.blue); | |
thisButton.setForeground(Color.white); | |
thisButton.setFont(font); | |
operatorButton[i] = thisButton; | |
add(thisButton); | |
} | |
//add equal-to button | |
equal.setBounds(67*3, 53*2, 67, 53*3); //it is in the last column and is 3 columns worth in height | |
equal.setBackground(Color.red); | |
equal.setForeground(Color.white); | |
equal.addActionListener(this); | |
equal.setFont(font); | |
add(equal); | |
//add special buttons | |
for (int i = 0; i < 3; i++) { | |
thisButton = new Button(specialArray[i]); | |
thisButton.setBounds(67*(i+1), 53, 67, 53); | |
thisButton.addActionListener(this); | |
thisButton.setFont(font); | |
specialButton[i] = thisButton; | |
add(thisButton); | |
} | |
} | |
public void actionPerformed(ActionEvent event) { | |
String text = textField.getText(); | |
//check if its a number button | |
for (int i = 0; i < 10; i++) { | |
if(event.getSource() == numberButton[i]){ | |
String thisButtonText = numberButton[i].getLabel(); | |
//show the number on the text field | |
text += thisButtonText; | |
textField.setText(text); | |
if(!firstVarOver){ | |
var[1] += thisButtonText; | |
whichVar = 1; | |
return; | |
} | |
else{ | |
whichVar = 2; | |
var[2] += thisButtonText; | |
isBckspAble = true; | |
return; | |
} | |
} | |
else if(event.getSource() == zero){ | |
text += "0"; | |
textField.setText(text); | |
var[whichVar] += "0"; | |
return; | |
} | |
} | |
//number checking loop ends | |
//operator checking begins | |
for (int i = 0; i < 4; i++) { | |
if(event.getSource() == operatorButton[i]){ | |
operator = operatorButton[i].getLabel().charAt(0); | |
firstVarOver = true; // first variable is entered completely only after an operator is entered. | |
text += " " + operator + " "; | |
textField.setText(text); | |
} | |
} | |
//operator checking ends | |
//equal-to checking begins | |
if(event.getSource() == equal){ | |
text += " = "; | |
textField.setText(text); | |
try{ | |
compute(Float.parseFloat(var[1]), Float.parseFloat(var[2]), operator); | |
} | |
catch(NumberFormatException e){ | |
JOptionPane.showMessageDialog(null, "Whoops! Cannot compute! Need two operands!"); | |
restart(); | |
clear(); | |
} | |
} | |
//equal-to checking ends | |
//special button checking begins | |
char spcLabel; | |
for (int i = 0; i < 3; i++) { | |
if(event.getSource() == specialButton[i]){ | |
spcLabel = specialButton[i].getLabel().charAt(0); | |
switch(spcLabel){ | |
case 'C': clear(); break; | |
case '.': { | |
text += "."; | |
textField.setText(text); | |
var[whichVar]+= "."; | |
break; | |
} | |
case '<': { | |
if(isBckspAble){ | |
text = text.substring(0, text.length()-1); | |
textField.setText(text); | |
var[whichVar] = var[whichVar].substring(0, var[whichVar].length()-1); | |
} | |
break; | |
} | |
} | |
} | |
} | |
} | |
//this function will compute the result of the binary operation, i.e. this function is the calculator without the graphics | |
public void compute(float var1, float var2, char op) { | |
switch(op){ | |
case '+':{ | |
result = var1+var2; | |
break; | |
} | |
case '-':{ | |
result = var1-var2; | |
break; | |
} | |
case '*':{ | |
result = var1*var2; | |
break; | |
} | |
case '/':{ | |
result = var1/var2; | |
break; | |
} | |
default:{ | |
JOptionPane.showMessageDialog(null, "Oh snap! Some error occured!"); | |
clear(); | |
restart(); | |
} | |
} | |
String text = textField.getText(); | |
text += result; | |
textField.setText(text); | |
adjustForRecomputing();//after each computation, adjustment has to be done for the next computation | |
} | |
//this function makes the calculator ready for the next operation | |
public void adjustForRecomputing(){ | |
var[1] = Float.toString(result); | |
firstVarOver = true; | |
var[2] = ""; | |
whichVar = 2; | |
isBckspAble = false; | |
} | |
public void restart(){ | |
JOptionPane.showMessageDialog(null, "Reloading..."); | |
repaint(); | |
} | |
public void clear(){ | |
textField.setText("\n"); | |
firstVarOver = false; | |
whichVar = 1; | |
var[1] = ""; | |
} | |
} |
No comments:
Post a Comment