import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;


public class insertion extends JFrame implements ActionListener {
          JPanel contener = new JPanel(new BorderLayout());
           JPanel y = new JPanel(new BorderLayout());
          JPanel contener2 = new JPanel(new BorderLayout());
          
          int s11=6;
        private JTextField[] textfields ;
        JPanel Buttom4 = new JPanel(new BorderLayout());
         JPanel Buttom = new JPanel(new BorderLayout());
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
        JPanel resultPanel = new JPanel(new GridLayout());
        JPanel heder = new JPanel(new BorderLayout());
         JPanel labelll = new JPanel(new BorderLayout());
        JPanel hh = new JPanel(new FlowLayout());
        ArrayList<JPanel> panels = new ArrayList<JPanel>();
        boolean isSearch = true;
        int size = 1;
        JButton b2 = new JButton("next    ");
        JButton b3 = new JButton("Start     ");
         TextArea a=new TextArea("");
         JLabel numb=new JLabel(" Number of Elemant");
         JLabel valuo=new JLabel("      rang of valuo ");
         JLabel chooce=new JLabel("     type of Enter valuo");
         JComboBox num = new JComboBox(new String[]{"5",
         "7", "10"});
       JComboBox value = new JComboBox(new String[]{"1-10",
          "10-100", "more than 100"});
          JComboBox method = new JComboBox(new String[]{"Randomly",
          "Enter by user"});
          String s="                                    APPLET FOR  INSERTION  SORT          ";
         // s+="   "+"\n"+" ";
           JLabel ah =new JLabel(s) ; 
       
        public insertion() {
        this.setLocation(100, 100);
        this.setTitle("insertion applet ");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(400, 300);
        Font f=new Font("SansSerif", Font.BOLD, 16);
       
         ah.setFont(f);
         
        setLayout(new BorderLayout());
        labelll.add(ah,BorderLayout.NORTH);
        labelll.setBorder(new TitledBorder(" "));
        hh.add(numb);
        hh.add(num);
        hh.add(valuo);
        hh.add(value);
        hh.add(chooce);
        hh.add(method);
        hh.setBorder(new TitledBorder(" "));
        // add(labelll,BorderLayout.NORTH);
         // add(hh,BorderLayout.CENTER);
        // setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        
        contener.add(labelll,BorderLayout.NORTH);
        contener.add(hh,BorderLayout.CENTER);
        contener.add(Buttom,BorderLayout.SOUTH);
        JPanel textFieldPanel = new JPanel(new GridLayout(1, 5, 5, 5));
        
         textfields = new JTextField[s11];
        for (int i = 0; i < textfields.length; i++) {
                JTextField field = new JTextField(3);
                field.setText((int) (Math.random() * 10) + "");
                textfields[i] = field;
                textFieldPanel.add(field);
        }
        textFieldPanel.setBorder(new TitledBorder(" "));
          buttonPanel.add(b3);
          buttonPanel.add(b2);
       // textFieldPanel.add(b2);
        b2.addActionListener(this);
        b3.addActionListener(this);
        // textFieldPanel.add(num);
       
       contener2. add(textFieldPanel, BorderLayout.NORTH);
       Buttom.add(buttonPanel,BorderLayout.NORTH);
       y. add(contener,BorderLayout.NORTH);
       y.  add(contener2,BorderLayout.CENTER);
      // Buttom4.add(Buttom,BorderLayout.NORTH);
      // Buttom4.add(y,BorderLayout.CENTER);
       add(y,BorderLayout.NORTH);
      this.pack();
        }

        private void sortAction() {
        if (isSearch) {
                resultPanel.removeAll();
                GridLayout g = (GridLayout) resultPanel.getLayout();
                g.setRows(1);
                insertionSort();
                isSearch = false;
        }
        resultPanel.updateUI();
        for (int i = 0; i < size; i++) {
                resultPanel.add(panels.get(i));
        }
        size++;
        if (size > panels.size()) {
                b2.setEnabled(false);
        }
        add(resultPanel, BorderLayout.CENTER);
        a.setSize(20,20);
        
        //add(a,BorderLayout.EAST);
      this.pack();
      // this.setSize(getWidth(), getHeight() + panels.size() *10);
        }

        public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(b2)) {
                sortAction();
        }
        if (e.getSource().equals(b3)) {
        	
                String  s1= (String) num.getSelectedItem();
                int s11=Integer.parseInt(s1);
                String  s2= (String)value.getSelectedItem();
                int s22=Integer.parseInt(s2);
                String  s3= (String) method.getSelectedItem();
                int s33=Integer.parseInt(s3);
         
        }
        }

        public static void main(String[] args) {
        insertion frame = new insertion();
        }

        public void insertionSort() {
        int[] x = new int[textfields.length];
        for (int i = 0; i < textfields.length; i++) {
                x[i] = Integer.parseInt(textfields[i].getText());
        }
        for (int i = 1; i < x.length; i++) {
                for (int j = i; j > 0 && x[j - 1] > x[j]; j--) {
                swap(x, j, j - 1);
                printArray(x,j);
                }
        }
        }

        public void swap(int[] x, int a, int b) {
        int t = x[a];
        x[a] = x[b];
        x[b] = t;
        }

        private void printArray(int[] x,int j) {
       JPanel panel = new JPanel(new GridLayout(1, x.length));
      // JLabel l=new JLabel(" compuer from 0 to "+(j-1)+" " );
      // JLabel l=new JLabel("compuer"+(j)+" with all elemant from");
       //panel.add(l);
        
        for (int i = 0; i < x.length; i++) {
        	
                JTextField field = new JTextField(3);
                	if(i==j){
                	
        field.setBackground(Color.cyan);
        field.setFont(new Font("SansSerif", Font.BOLD, 16));

                	}
                field.setText("" + x[i]);
                panel.add(field);
        }
        GridLayout g = (GridLayout) resultPanel.getLayout();
        g.setRows(g.getRows() + 1);
        panels.add(panel);
//      resultPanel.add(panel);
        }
}
