01: import java.awt.*;
02: import java.awt.event.*;
03: import javax.swing.*;
04: import java.util.concurrent.*;
05: 
06: /**
07:    This program animates a sort algorithm.
08: */
09: 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:       final 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:       final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
34:       queue.add("Step");
35: 
36:       final Sorter sorter = new Sorter(values, panel, queue);
37: 
38:       stepButton.addActionListener(new
39:          ActionListener()
40:          {
41:             public void actionPerformed(ActionEvent event)
42:             {
43:                queue.add("Step");
44:                runButton.setEnabled(true);
45:             }
46:          });
47: 
48:       runButton.addActionListener(new
49:          ActionListener()
50:          {
51:             public void actionPerformed(ActionEvent event)
52:             {
53:                runButton.setEnabled(false);
54:                queue.add("Run");
55:             }
56:          });
57: 
58:       Thread sorterThread = new Thread(sorter);
59:       sorterThread.start();
60:    }
61: 
62:    private static final int FRAME_WIDTH = 300;
63:    private static final int FRAME_HEIGHT = 300;
64:    private static final int VALUES_LENGTH = 30;
65: }