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