/**
 *Criado pelo prof. André Aparecido da Silva para as aulas 22a 24 
 *do Curso Tecnico em Informática do Colégio Estadual do Paraná 
 *
 */
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;


public class Equacao2GrauNovaInterface extends JFrame implements ActionListener
{
		JPanel Painel_Oeste, Painel_Oeste1, Painel_Oeste2, Painel_Oeste3, Painel_Oeste4, Painel_Oeste5, Painel_Oeste6, Painel_Oeste7;
		JPanel Painel_Leste, Painel_Leste1, Painel_Leste2, Painel_Leste3, Painel_Leste4, Painel_Leste5, Painel_Leste6, Painel_Leste7;
		JPanel Painel_Sul;
		JTextField tf_ValorA, tf_ValorB, tf_ValorC;
		JTextField tf_ValorX1, tf_ValorX2;
		JButton bt_Resolver, bt_Nova, bt_Sair;
		public Equacao2GrauNovaInterface() 
    	{
    	
    			getContentPane().setLayout(new BorderLayout());
    			getContentPane().add("West", Painel_Oeste = new JPanel(new GridLayout(7, 1)));
    			Painel_Oeste.add(Painel_Oeste1 = new JPanel(new FlowLayout(FlowLayout.LEFT))); 
                Painel_Oeste1.add(new JLabel ("Equação do Segundo Grau"));
    			Painel_Oeste.add(Painel_Oeste2 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    			Painel_Oeste2.add(tf_ValorA = new JTextField(10));
    			Painel_Oeste2.add(new JLabel ("X²"));
    			Painel_Oeste.add(Painel_Oeste3 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
  				Painel_Oeste3.add(new JLabel ("+"));
    			
    			Painel_Oeste.add(Painel_Oeste4 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    			Painel_Oeste4.add(tf_ValorB = new JTextField(10));
    			Painel_Oeste4.add(new JLabel("X"));
    			Painel_Oeste.add(Painel_Oeste5 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    			Painel_Oeste5.add(new JLabel ("+"));
  
    			Painel_Oeste.add(Painel_Oeste6 = new JPanel(new FlowLayout(FlowLayout.CENTER)));  
    			Painel_Oeste6.add(tf_ValorC = new JTextField(10));	  			
    			Painel_Oeste.add(Painel_Oeste7 = new JPanel(new FlowLayout(FlowLayout.CENTER)));    			
   				Painel_Oeste7.add(new JLabel ("=0"));
   				getContentPane().add("East", Painel_Leste = new JPanel (new GridLayout(7, 1)));
   				Painel_Leste.add(Painel_Leste1 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
   				Painel_Leste1.add(new JLabel ("RAIZES DA ESQUAÇÃO"));
   				Painel_Leste.add(Painel_Leste2 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
   				Painel_Leste.add(Painel_Leste3 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
   				Painel_Leste3.add(new JLabel("x1 = "));
   				Painel_Leste3.add(tf_ValorX1 = new JTextField(10));
   				tf_ValorX1.setEditable(false);
   				Painel_Leste.add(Painel_Leste4 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
   				Painel_Leste.add(Painel_Leste5 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
   				Painel_Leste5.add(new JLabel("x2 = "));
   				Painel_Leste5.add(tf_ValorX2 = new JTextField(10));
				tf_ValorX2.setEditable(false);
   				Painel_Leste.add(Painel_Leste6 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
   				Painel_Leste.add(Painel_Leste7 = new JPanel(new FlowLayout(FlowLayout.CENTER)));
  		
    			    			
    			    			
    			    			
    			getContentPane().add("South", Painel_Sul = new JPanel(new FlowLayout(FlowLayout.CENTER)));    			
    			Painel_Sul.add(bt_Resolver = new JButton("Resolver a Equação"));
    			Painel_Sul.add(bt_Nova = new JButton("Efetuar Novo Cálculo"));
    			Painel_Sul.add(bt_Sair = new JButton("Sair"));
    			bt_Resolver.addActionListener(this);
    			bt_Nova.addActionListener(this); 
    			bt_Sair.addActionListener(this);    			
    			pack();
    			setResizable(false);
    			setVisible(true);	
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    			if(e.getSource()==bt_Sair)
    					{System.exit(0);}
    			if(e.getSource()==bt_Resolver)
    			{
    					System.out.print("Botao resolver clicado");
    					double a,  b, c;
    					a = Double.parseDouble(tf_ValorA.getText());
    					if(tf_ValorB.getText().equals(""))
    						{b = 0;}
    					else
    						{b = Double.parseDouble(tf_ValorB.getText());}
						///////////
						if(tf_ValorC.getText().equals(""))
    						{c = 0;}
    					else
    						{c = Double.parseDouble(tf_ValorC.getText());}
    				    double Delta = (b*b)-4*a*c;
    				    if(Delta < 0)
    				    {
    				    	tf_ValorX1.setText("Não há soluções com numeros reais");
    				    	tf_ValorX2.setText("Não há soluções com numeros reais");
    				    }
    				    else 
    				    {
    				    	double x1 = (-b+Math.sqrt((b*b)-4*a*c))/(2*a); 
							double x2 = (-b-Math.sqrt((b*b)-4*a*c))/(2*a); 
							tf_ValorX1.setText(""+x1);
    				    	tf_ValorX2.setText(""+x2);

    				    }
    					//if((tf_ValorA.getText().equals(" ")))
    					//{
    					//		JOptionPane.showMessageDialog(null, "Você precisa colocar um valor válido para A");
    					//}			
    			}	
    			if(e.getSource()==bt_Nova)
    			{
	  						tf_ValorA.setText("");
	  						tf_ValorB.setText("");
	  						tf_ValorC.setText("");
							tf_ValorX1.setText("");
							tf_ValorX2.setText("");
						
  					
    			}	
  				
    	}	
    	public static void main(String args [])
    	{
    			new Equacao2GrauNovaInterface();
    	}	
    
    
}