import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;


public class Calc implements ActionListener{ 
	
	static JTextArea textArea;
	
	static JFrame calculator; 
	static JPanel panel;
	static JPanel panel2;
	static JButton button1;
	static JButton button2;
	static JButton button3;
	static JButton button4;
	static JButton button5;
	static JButton button6;
	static JButton button7;
	static JButton button8;
	static JButton button9;
	static JButton button0;
	static JButton buttonadd;
	static JButton buttonsub;
	static JButton buttonmul;
	static JButton buttondiv;
	static JButton buttonsqr;
	static JButton buttoneq;
	static JButton buttonclear;
	static JButton buttonclose;
	static JButton buttonhelp;
	
	static String text;
	
	public static void main(String[] args) {
		Calc test = new Calc();
		
		calculator = new JFrame();
		
		
		calculator.setSize(300, 300);
		
		calculator.setTitle("Calculator ^^");
		
	    button1 = new JButton("1");
		button2 = new JButton("2");
		button3 = new JButton("3");
		button4 = new JButton("4");
		button5 = new JButton("5");
		button6 = new JButton("6");
		button7 = new JButton("7");
		button8 = new JButton("8");
		button9 = new JButton("9");
		button0 = new JButton("0");
		buttonadd = new JButton("+");
		buttonsub = new JButton("-");
		buttonmul = new JButton("*");
		buttondiv = new JButton("/");
		buttonsqr = new JButton("sqr");
		buttoneq = new JButton("=");
		buttonclear = new JButton("C");
		buttonclose = new JButton("close");
		buttonhelp= new JButton("help");
		
		textArea = new JTextArea(1,21);
		
		Container c= calculator.getContentPane();
		
		c.setLayout(new GridLayout(3, 1, 2, 2));
		
		c.setBackground(Color.black);
		
		
	
		 panel = new JPanel(new GridLayout(4,4));
		 panel2=new JPanel(new GridLayout(1,3));		
				
				
		
		button1.setName("1");
		button2.setName("2");
		button3.setName("3");
		button4.setName("4");
		button5.setName("5");
		button6.setName("6");
		button7.setName("7");
		button8.setName("8");
		button9.setName("9");
		button0.setName("0");
		buttonadd.setName("+");
		buttonsub.setName("-");
		buttonmul.setName("*");
		buttondiv.setName("/");
		buttonsqr.setName("sqr");
		buttoneq.setName("=");
		buttonclear.setName("C");
		buttonclose.setName("Close");
		buttonhelp.setName("help");
		
		
		panel.add(button1);
		panel.add(button2);
		panel.add(button3);
		panel.add(buttonsqr);
		
		panel.add(button4);
		panel.add(button5);
		panel.add(button6);
		panel.add(buttonmul);
		
		panel.add(button7);
		panel.add(button8);
		panel.add(button9);
		panel.add(buttondiv);
		
		panel.add(button0);
		panel.add(buttonadd);
		panel.add(buttonsub);
		panel.add(buttoneq);
		
		panel2.add(buttonclear);
		panel2.add(buttonclose);
		panel2.add(buttonhelp);
		

button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button0.addActionListener(this);
buttonadd.addActionListener(this);
buttonsub.addActionListener(this);
buttonmul.addActionListener(this);
buttondiv.addActionListener(this);
buttoneq.addActionListener(this);
buttonclear.addActionListener(this);
buttonsqr.addActionListener(this);
buttonhelp.addActionListener(new ActionListener(){*/

	
		c.add(textArea);
		c.add(panel);
		c.add(panel2);
		
		
		
		calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		calculator.setVisible(true);
		}
		
	

	public void actionPerformed(ActionEvent e) {
		
		 Double number1,number2,result;
		int addc=0,subc=0,multic=0,divc=0,squarec=0;
		
	  
	//	buttonhelp.addActionListener(new ActionListener(){
		JOptionPane.showMessageDialog(null, "This Program is Version 1.0\n"+"Teams Name : \n");
		
		Object source = e.getSource();
		
		
	if(source==buttonclear)
		{
		number1=0.0;
		number2=0.0;
		textArea.setText("");}

	
		if(source==button1){
		textArea.append("1");}

		if(source==button2){
		
		textArea.append("2");
		}

		if(source==button3){

		textArea.append("3");
		}
		if(source==button4)
		{
	    textArea.append("4");
		}
		if(source==button5)
		{
		textArea.append("5");
		}
		if(source==button6)
		{
		textArea.append("6");
		}
		if(source==button7)
		{
		textArea.append("7");
		}
		if(source==button8)
		{
		textArea.append("8");
		}
		if(source==button9)
		{
		textArea.append("9");
		}
		if(source==button0)
		{
		textArea.append("0");
		}

		if(source==buttonadd){
		number1=number_reader();
		textArea.setText("");
		addc=1;
		subc=0;
		multic=0;
		divc=0;
		squarec=0;
		}
		if(source==buttonsub){
		number1=number_reader();
		textArea.setText("");
		addc=0;
		subc=1;
		multic=0;
		divc=0;
		squarec=0;
		}
		if(source==buttonmul){
		number1=number_reader();
		textArea.setText("");
		addc=0;
		subc=0;
		multic=1;
		divc=0;
		squarec=0;
		}
		if(source==buttondiv){
		number1=number_reader();
		textArea.setText("");
		addc=0;
		subc=0;
		multic=0;
		divc=1;
		squarec=0;
		}
		if(source==buttonsqr){
		number1=number_reader();
		result=Math.pow(number1,2);
		textArea.setText(""+result);
		addc=0;
		subc=0;
		multic=0;
		divc=0;
		squarec=1;
		}
		if(source==buttoneq){
		number2=number_reader();
		 
		if(addc!=0){
		number1=number_reader();
		number2=number_reader();
	    result=number1+number2;
		textArea.setText(Double.toString(result));
		 
		}
		if(subc==1){
		number1=number_reader();
		number2=number_reader();
		result=number1-number2;
		textArea.setText(Double.toString(result));
		 
		}
		 
		if(multic==1){
			number1=number_reader();
		number2=number_reader();
		result=number1*number2;
		textArea.setText(Double.toString(result));
		 
		}
		 
		if(divc==1){
			number1=number_reader();
		number2=number_reader();
		result=number1/number2;
		textArea.setText(Double.toString(result));
		 }
		}}
		
		public double number_reader(){
		Double num1;
		String s;
		s=textArea.getText();
		num1=Double.valueOf(s);
		return num1;}
		
}
