1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  import java.util.concurrent.*;
  5  
  6  /**
  7     This program animates a sort algorithm.
  8  */
  9  public class AnimationTester
 10  {
 11     public static void main(String[] args)
 12     {
 13        JFrame frame = new JFrame();
 14        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 15  
 16        ArrayComponent panel = new ArrayComponent();
 17        frame.add(panel, BorderLayout.CENTER);
 18  
 19        JButton stepButton = new JButton("Step");
 20        JButton runButton = new JButton("Run");
 21  
 22        JPanel buttons = new JPanel();
 23        buttons.add(stepButton);
 24        buttons.add(runButton);
 25        frame.add(buttons, BorderLayout.NORTH);
 26        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
 27        frame.setVisible(true);
 28  
 29        Double[] values = new Double[VALUES_LENGTH];
 30        for (int i = 0; i < values.length; i++)
 31           values[i] = Math.random() * panel.getHeight();
 32  
 33        BlockingQueue<String> queue = new LinkedBlockingQueue<>();
 34        queue.add("Step");
 35  
 36        Sorter sorter = new Sorter(values, panel, queue);
 37  
 38        stepButton.addActionListener(event -> 
 39              {
 40                 queue.clear();
 41                 queue.add("Step");
 42              });
 43  
 44        runButton.addActionListener(event ->
 45              {
 46                 queue.clear();
 47                 queue.add("Run");
 48              });
 49  
 50        ExecutorService service = Executors.newCachedThreadPool();
 51        service.execute(sorter);
 52     }
 53  
 54     private static final int FRAME_WIDTH = 300;
 55     private static final int FRAME_HEIGHT = 300;
 56     private static final int VALUES_LENGTH = 30;
 57  }