import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrameCollatz2 extends JFrame implements ActionListener 
{
	JTextArea ta_Mostra_Calculos; 
	JScrollPane Painel_Central;	
	JTextField tf_Numero;	
	JPanel Painel_Norte, Painel_Sul, Painel_Sul1, Painel_Sul2;
	JButton bt_Testar, bt_Limpar, bt_Sair;  	
    public FrameCollatz2()
    {
    		getContentPane().setLayout(new BorderLayout());
    		getContentPane().add("North", Painel_Norte = new JPanel(new FlowLayout(FlowLayout.CENTER)));
    		Painel_Norte.add(new JLabel("TESTE COLLATZ"));
        	getContentPane().add("Center", Painel_Central = new JScrollPane (ta_Mostra_Calculos = new JTextArea(30, 30)));
        	
        
        	getContentPane().add("South", Painel_Sul = new JPanel(new GridLayout(2, 1)));
        	Painel_Sul.add(Painel_Sul1 = new JPanel (new FlowLayout(FlowLayout.CENTER)));
        	Painel_Sul1.add(new JLabel("NUMERO PARA TESTAR:"));
        	Painel_Sul1.add(tf_Numero = new JTextField(15));
        	Painel_Sul.add(Painel_Sul2 = new JPanel (new FlowLayout(FlowLayout.CENTER)));
        	Painel_Sul2.add(bt_Testar = new JButton("TESTAR"));
        	Painel_Sul2.add(bt_Limpar = new JButton("LIMPAR"));
        	Painel_Sul2.add(bt_Sair = new JButton("SAIR"));
        	bt_Testar.addActionListener(this);
        	bt_Limpar.addActionListener(this);
        	bt_Sair.addActionListener(this);
        	setSize(500, 700);
        	setVisible(true);
        	//setResizable(false);
    }   
     
    public void actionPerformed(ActionEvent e)
    {
    	ta_Mostra_Calculos.setText("");//Qualquer botão que for apertado ele limpa a caixa de textos
    	
    	if(e.getSource()==bt_Testar)
    	{
    		
    		int Numero = Integer.parseInt(tf_Numero.getText());
    		tf_Numero.setText("");
    
    		Metodo_Testa_Collatz(Numero);   		 	
    	}
    	if(e.getSource()==bt_Limpar)
    	{
    		ta_Mostra_Calculos.setText("");
    		tf_Numero.setText("");
    	}
    	if(e.getSource()==bt_Sair)
    		{System.exit(0);}		
    		
    }   
    public void Metodo_Testa_Collatz(int Numero)
    {
    	ta_Mostra_Calculos.append(""+Numero + "\n");
    	int  Qtde_Ciclos = 0; 
    	while(Numero > 1)
    	{
    			if((Numero%2)==0)
    			{
    				Numero = Numero / 2;
    				ta_Mostra_Calculos.append(""+Numero + "\n"); 
    			}	
    			else
    			{
    				Numero = (Numero * 3) + 1; 
    				ta_Mostra_Calculos.append(""+Numero + "\n"); 	
    			}
    			Qtde_Ciclos = Qtde_Ciclos + 1; 		
    	}
    	ta_Mostra_Calculos.append("----------------------------------------");
    	ta_Mostra_Calculos.append("\n"+"Foram realizados " + Qtde_Ciclos + " ciclos."+"\n");
    	ta_Mostra_Calculos.append("----------------------------------------");
    		
    	
    }	 
     
     
    public static void main(String[] args)
    {
 			new FrameCollatz2();
    }
     
}