title: Animation of java.awt rectangles not acting as wanted
body: <p>I am trying to animate the process of the Selection &amp; Insertion sort algorithms with rectangles displaying sorted values by height. </p>
  
  <p>I call <code>repaint()</code> when a single change occurs in my Select/Insert sort algorithms, but it does not seem to repaint them entirely, only some of the rectangles change.  In the end result, it doesn't actually display the fully sorted array at all.</p>
  
  <p>Both algorithms are tested and work, so the problems seems to be with animating their process. </p>
  
  <p>Any help?</p>
  
  <p><strong>MyPaint.java</strong></p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.util.Timer;
  import java.util.TimerTask;
  public class MyPanel extends JPanel{
    private static int[] mainArr;
    private boolean reset = false;
    private Timer t = new Timer();
    private int[] tempArr;
      public MyPanel(){
         JPanel panel = new JPanel();
         panel.setPreferredSize(new Dimension(400, 400));
         panel.setBackground(Color.WHITE);
         panel.setLayout(new FlowLayout(FlowLayout.CENTER));
         //JButton selButton = new JButton("Select Sort");
         //panel.add(selButton);
         add(panel);      
      }   
      public static void main(String[] args) {
        SortDriver frame = new SortDriver();
        mainArr = frame.getArr();
        frame.setVisible(true);
  
      }
  }
  </code></pre>
  
  <p><strong>SortDriver.java</strong></p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.awt.Graphics;
  public class SortDriver extends JFrame implements ActionListener {
  
          private int modeName;
          private JButton startButton;
          private JButton selButton;
          private JButton insButton;
          private JPanel mainPanel;
          private MyPanel panel;
          private int[] sortArr = new int[41];
  
          public SortDriver() {
                  setLayout(null);
                  setPreferredSize(new Dimension(420, 420));
                  setResizable(false);
                  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  mainPanel = new JPanel();
                  mainPanel.setBackground(Color.WHITE);
                  mainPanel.setBounds(0, 0, 420, 420);
                  mainPanel.setPreferredSize(new Dimension(400, 400));
                  //mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
                  add(mainPanel);
  
                  //Buttons 
                  startButton = new JButton("Start");
                  startButton.addActionListener(this);
                  mainPanel.add(startButton);
                  selButton = new JButton("Select Sort");
                  selButton.addActionListener(this);
                  mainPanel.add(selButton);
                  insButton = new JButton("Insert Sort");
                  insButton.addActionListener(this);
                  mainPanel.add(insButton);
  
  
                  //Panel
                  panel = new MyPanel();
  
                  mainPanel.add(panel);
                  //Array                              
                  for(int i = 0; i &lt;= 40; i++){
                    sortArr[i] = 0; 
                  }
                  for(int i = 0; i &lt;= 40; i++){
                    int random = (int)(Math.random() * 50 + 1);
                    sortArr[i] = random; 
                  }
                  //Final
                  pack();
          }
          public void paint(Graphics g) {
            for(int i = 0; i &lt;= 40; i++){
              g.fillRect(i * 10, 100, 5, sortArr[i]);
            }      
          }
  
          public void selectionSort (int[] list){       
            int min;       
            int temp;        
            for (int index = 0; index &lt; list.length-1; index++){          
              min = index;          
              for (int scan = index+1; scan &lt; list.length; scan++)             
                if (list[scan] - (list[min]) &lt; 0) min = scan;           
  
        // Swap the values          
              temp = list[min];          
              list[min] = list[index];
              list[index] = temp;
              repaint();
            }
            //for(int i = 0; i &lt;= list.length; i++){
            //  System.out.println(list[i]); 
            //}
          }
          public void insertionSort (int[] list){       
            for (int index = 1; index &lt; list.length; index++){          
              int key = list[index];          
              int position = index;           
  
              //  Shift larger values to the right          
              while (position &gt; 0 &amp;&amp; key - (list[position-1]) &lt; 0){             
                list[position] = list[position-1];
                position--;
                repaint();
              }          
              list[position] = key;       
            }    
          }
          public void actionPerformed(ActionEvent event) {
            if (event.getSource() == selButton) {
              modeName = 1;
            }
            else if (event.getSource() == insButton) {
              modeName = 2;
            }
            else if (event.getSource() == startButton) {
              if(modeName == 1){
                selectionSort(sortArr);
              }
              if(modeName == 2){
                insertionSort(sortArr);
              }
            }
          }
          public int getMode(){
            return modeName; 
          }
          public int[] getArr(){
            return sortArr; 
          }
  }
  </code></pre>