1  import java.awt.Graphics;
  2  import java.awt.Graphics2D;
  3  import javax.swing.JComponent;
  4  
  5  /**
  6     A component that displays the current state of the selection sort algorithm.
  7  */
  8  public class SelectionSortComponent extends JComponent
  9  {
 10     private SelectionSorter sorter;
 11  
 12     /**
 13        Constructs the component.
 14     */
 15     public SelectionSortComponent()
 16     {
 17        int[] values = ArrayUtil.randomIntArray(30, 300);
 18        sorter = new SelectionSorter(values, this);
 19     }
 20  
 21     public void paintComponent(Graphics g)
 22     {
 23        Graphics2D g2 = (Graphics2D)g;
 24        sorter.draw(g2);
 25     }
 26  
 27     /**
 28        Starts a new animation thread.
 29     */
 30     public void startAnimation()
 31     {
 32        class AnimationRunnable implements Runnable
 33        {
 34           public void run()
 35           {
 36              try
 37              {
 38                 sorter.sort();
 39              }
 40              catch (InterruptedException exception)
 41              {
 42              }
 43           }
 44        }
 45        
 46        Runnable r = new AnimationRunnable();
 47        Thread t = new Thread(r);
 48        t.start();
 49     }
 50  }
 51