JavaApplet: Basic Maths

 

 

Here is the BasicMaths.java source code

import java.applet.*;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.*;

 

public class BasicMaths extends Applet implements ActionListener {
       Button okButton;
       TextField num1;
       TextField num2;
       CheckboxGroup radioGroup;
       Checkbox radio1;
       Checkbox radio2;
       Checkbox radio3;
       double cal=0;
       double number1=0;
       double number2=0;    
      
      
       public void init()
       {
              // Now we will use the FlowLayout
              setLayout(new FlowLayout());
              okButton = new Button("Compute");
              num1 = new TextField("0",5);
              num2 = new TextField("0",5);
              radioGroup = new CheckboxGroup();
              radio1 = new Checkbox("Add", radioGroup,false);
              radio2 = new Checkbox("Minus", radioGroup,false);
              radio3 = new Checkbox("Divide", radioGroup,false);
              add(okButton);
              add(num1);
              add(num2);
              add(radio1);
              add(radio2);
              add(radio3);
             
              // Attach actions to the components
              okButton.addActionListener(this);
             
       }
      
       public void paint(Graphics g)
       {
              //Convert text input to Double
              number1=Double.parseDouble(num1.getText());
              number2=Double.parseDouble(num2.getText());
             
              if (radio1.getState()){ //ADD
                     cal= number1+number2;
                     g.drawString(String.valueOf(cal),20,100);
              }else if (radio2.getState()){ //MINUS
                     cal= number1-number2;
                     g.drawString(String.valueOf(cal),20,100);
              }else if (radio3.getState()){ //DIVIDE
                     cal= number1/number2;
                     g.drawString(String.valueOf(cal),20,100);
              }else{ //When none is select
                     cal= 0;
                     g.drawString(String.valueOf(cal),20,100);
              }
             
       }
      
       public void actionPerformed(ActionEvent evt) // Handle the Button Click event
       {
              if (evt.getSource() == okButton)
              {     
                     repaint();
              }
       }

      
}