/**
 * @(#)Text1.java
 *
 *
 * @author 
 * @version 1.00 2017/3/28
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class CalculadoraSimples2 extends JFrame implements ActionListener
{
	JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste;
	JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3;
	JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3;
	JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3;
	JPanel Painel_Sul1, Painel_Sul2, Painel_Sul3; 
	JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar, bt_Pi, bt_RaizN;
	JButton bt_Raiz, bt_Potencia, bt_Logaritmo, bt_Fatorial, bt_Sair, bt_1dividoporx; 
	JLabel lbl_Resultado;
	JTextField txt_Valor1, txt_Valor2;
    public CalculadoraSimples2() 
    {
    	setLayout(new BorderLayout());
    	getContentPane().setBackground(Color.orange);
    	getContentPane().add("East", Painel_Leste = new JPanel(new GridLayout(3,1)) );
    	Painel_Leste.add(Painel_Leste1 = new JPanel (new FlowLayout(FlowLayout.CENTER)));
    	 
    	Painel_Leste1.add(bt_Somar = new JButton("+"));
    	Painel_Leste1.add(bt_Diminuir = new JButton("-"));
    	
    	Painel_Leste.add(Painel_Leste2 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    	Painel_Leste1.add(bt_Dividir = new JButton("/"));
    	Painel_Leste1.add(bt_Multiplicar = new JButton("*"));
    	
    	Painel_Leste.add(Painel_Leste3  = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    	Painel_Leste3.add(bt_Limpar = new JButton("Limpar"));
    	getContentPane().add("West", Painel_Oeste = new JPanel(new GridLayout(3, 1)));
    	Painel_Oeste.add(Painel_Oeste1 = new JPanel (new FlowLayout(FlowLayout.RIGHT)));
    	Painel_Oeste1.add(new JLabel("Valor 1:"));
    	Painel_Oeste.add(Painel_Oeste2 = new JPanel (new FlowLayout(FlowLayout.RIGHT)));
    	Painel_Oeste2.add(new JLabel("Valor 2:"));
    	Painel_Oeste.add(Painel_Oeste3 = new JPanel (new FlowLayout(FlowLayout.RIGHT))); 
    	Painel_Oeste3.add(new JLabel("Resultado:"));    		   	
    	getContentPane().add("Center", Painel_Centro = new JPanel(new GridLayout(3, 1)));
    	Painel_Centro.add(Painel_Centro1 = new JPanel (new FlowLayout(FlowLayout.LEFT)));
    	Painel_Centro1.add(txt_Valor1 = new JTextField(20));
    	Painel_Centro.add(Painel_Centro2 = new JPanel (new FlowLayout(FlowLayout.LEFT)));
    	Painel_Centro2.add(txt_Valor2 = new JTextField(20));
    	Painel_Centro.add(Painel_Centro3 = new JPanel (new FlowLayout(FlowLayout.LEFT)));	
    	Painel_Centro3.add(lbl_Resultado = new JLabel ("Aguardando digitação de valores"));
 
    	//Nontando o painel ao Sul do JFrame
    	getContentPane().add("South", Painel_Sul = new JPanel (new GridLayout(3, 1)));
    	Painel_Sul.add(Painel_Sul1 = new JPanel (new FlowLayout(FlowLayout.CENTER)));
    	Painel_Sul1.add(new JLabel ("FUNÇÕES ESPECIAS"));
		Painel_Sul.add(Painel_Sul2 = new JPanel (new FlowLayout(FlowLayout.CENTER)));
		Painel_Sul2.add(bt_Pi = new JButton ("PI"));
    	Painel_Sul2.add(bt_Raiz = new JButton ("Raiz Quadrada"));
    	Painel_Sul2.add(bt_Potencia = new JButton ("^")); 
    	Painel_Sul2.add(bt_Logaritmo = new JButton ("Log")); 
    	Painel_Sul2.add(bt_Fatorial = new JButton ("n!"));
    	Painel_Sul2.add(bt_Sair =  new JButton ("Sair"));
    	Painel_Sul.add(Painel_Sul3 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    	Painel_Sul3.add(bt_1dividoporx = new JButton("1/x"));
    	Painel_Sul3.add(bt_RaizN 	   = new JButton("Raiz(n)"));
    	bt_1dividoporx.addActionListener(this);
    	bt_Somar.addActionListener(this);
    	bt_Diminuir.addActionListener(this);
    	bt_Dividir.addActionListener(this);
    	bt_Multiplicar.addActionListener(this);
    	bt_Limpar.addActionListener(this);
    	bt_Pi.addActionListener(this);
    	bt_Raiz.addActionListener(this);
    	bt_Potencia.addActionListener(this);
    	bt_Logaritmo.addActionListener(this); 
    	bt_Fatorial.addActionListener(this);
    	bt_Sair.addActionListener(this);  
    	bt_RaizN.addActionListener(this); 
    	this.setLocationRelativeTo(null);		
    	setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	setResizable(false);	
    		
    		
 	
    		
    	setTitle("CALCULADORA MELHORADA");
    	pack();
    	setVisible(true);
    	
    }
    public static void main (String args [])
    {
    	new CalculadoraSimples2();
    	
    }	
    
    public void actionPerformed (ActionEvent e)
    {
    	String Caixa1 = txt_Valor1.getText();
    	double Valor1 = 0, Valor2 = 0;
    	double  Resultado;
    	if(Caixa1.equals(""))
    		{/*NAO FAÇA NADA*/}
    	else
    		{Valor1 = Double.parseDouble((Caixa1));}			 
    	String Caixa2 = txt_Valor2.getText();
  
    	
    	if(Caixa2.equals(""))
    		{/*NAO FAÇA NADA*/}
    	else
    		{Valor2 = Double.parseDouble((Caixa2));}			 
  		if(e.getSource()==bt_1dividoporx)
  		{
  			Resultado = (1/Valor1);
  			lbl_Resultado.setText(""+Resultado);
  			
  		}
  		if(e.getSource()==bt_RaizN)
  		{
  			int Numero = Integer.parseInt(txt_Valor1.getText()); 
  			int Indice = Integer.parseInt(txt_Valor2.getText());
  			double Res = Math.pow(Numero, (1/Indice));
  			lbl_Resultado.setText(""+Res);
  		}
  		if(e.getSource()==bt_Raiz)
    	{
    		if ((Valor1==0))
    		{lbl_Resultado.setText("Digite o valore!!!");}	
			else
			{
				double Raiz = Math.sqrt(Valor1);
    			lbl_Resultado.setText(""+Raiz);
			}
    	}    
	
    	if(e.getSource()==bt_Potencia)
    	{
    		    if ((Valor1==0)||(Valor2==0))
    		    	{lbl_Resultado.setText("Digite os valores 1 e 2!!!");}	
	    	    else
    	    	{
    	    		double Potencia = Math.pow(Valor1, Valor2);
    	    		lbl_Resultado.setText(""+Potencia);
    	    	}		  

    	}	
    	
    	if(e.getSource()==bt_Logaritmo) 
    	{
    		if ((Valor1==0))
    	    	{lbl_Resultado.setText("Digite os valores 1 e 2!!!");}	
    	    else
    	    {
    	    		double log = Math.log10(Valor1);
    				lbl_Resultado.setText(""+log);
    	    }
    	}	
    	if(e.getSource()==bt_Fatorial)
    	{
    		if(Valor1==0)
    		{
    			
    			lbl_Resultado.setText("Digite o Valor1");
    		}
    		else
    		{
                int MaiorNumero = (int)	Valor1;
                int MenorNumero = (MaiorNumero -1);
				while(MenorNumero > 1)
				{
					MaiorNumero = (MaiorNumero * MenorNumero);
					MenorNumero = (MenorNumero - 1);
					
				}
				lbl_Resultado.setText(""+MaiorNumero);
    		}		
    	}	
    	if(e.getSource()==bt_Pi)
    	{
    		double pi = Math.PI;
    		lbl_Resultado.setText(""+pi);
    	}	
    	if(e.getSource()==bt_Sair)
    	{
    		System.exit(0);
    	}  	    	    	
    	if(e.getSource()==bt_Somar)
    	{
    	      
    		lbl_Resultado.setText(""+(Valor1+Valor2));		
    	}
        if(e.getSource()==bt_Diminuir)
    	{
    	      
    		lbl_Resultado.setText(""+ (Valor1 - Valor2));		
    	}
        if(e.getSource()==bt_Multiplicar)
    	{
    	      
    		lbl_Resultado.setText(""+(Valor1 * Valor2));		
    	}
        if(e.getSource()==bt_Dividir)
    	{
    	    if ((Valor1==0)||(Valor2==0))
    	    	{lbl_Resultado.setText("Digite os valores 1 e 2!!!");}	
    	    else
    	    	{lbl_Resultado.setText(""+(Valor1/Valor2));}		  
    				
    	}	
    	    	
    	if(e.getSource()==bt_Limpar)
    	{
    	      //Resultado = Valor1 + Valor2; 
    	    lbl_Resultado.setText("");
			txt_Valor1.setText("");
			txt_Valor2.setText("");
    		lbl_Resultado.setText("Aguardando digitação de valores");
     		if(txt_Valor2.getText().equals(""))
    			{/*NAO FAÇA NADA*/}
    		else
    			{txt_Valor2.setText(" ");}
    			
    
		
    	} 
    		   	
    		
    }
		
    
}