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 {

        private JTextField[] textfields = new JTextField[7];
        JPanel resultPanel = new JPanel(new GridLayout());
        ArrayList<JPanel> panels = new ArrayList<JPanel>();
        boolean isSearch = true;
        int size = 1;
        JButton b2 = new JButton("next");

        public insertion() {
        this.setLocation(200, 200);
        this.setTitle("homwork # 1");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(400, 300);

        setLayout(new BorderLayout());
        // setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JPanel textFieldPanel = new JPanel(new GridLayout(1, 5, 5, 5));

        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(" "));

        textFieldPanel.add(b2);
        b2.addActionListener(this);

        add(textFieldPanel, 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);
       //this.pack();
       this.setSize(getWidth(), getHeight() + panels.size() *10);
        }

        public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("next")) {
                sortAction();
        }
        }

        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-1);
                }
        }
        }

        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 the  "+(j)+"  with all elemant from   "+(x[0])+"  to  "+(x[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.yellow);
        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);
        }
}
