01: import java.awt.*;
02: import java.awt.event.*;
03: import javax.swing.*;
04:
05: /**
06: This program animates a sort algorithm.
07: */
08: public class AnimationTest
09: {
10: public static void main(String[] args)
11: {
12: JFrame frame = new JFrame();
13: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14:
15: Container contentPane = frame.getContentPane();
16: ArrayPanel panel = new ArrayPanel();
17: contentPane.add(panel, BorderLayout.CENTER);
18: final Gate gate = new Gate();
19: gate.setActive(true);
20:
21: JButton runButton = new JButton("Run");
22: runButton.addActionListener(new
23: ActionListener()
24: {
25: public void actionPerformed(ActionEvent event)
26: {
27: gate.setActive(false);
28: }
29: });
30:
31: JButton stepButton = new JButton("Step");
32: stepButton.addActionListener(new
33: ActionListener()
34: {
35: public void actionPerformed(ActionEvent event)
36: {
37: gate.setOpen(true);
38: }
39: });
40:
41: JPanel buttons = new JPanel();
42: buttons.add(runButton);
43: buttons.add(stepButton);
44: contentPane.add(buttons, BorderLayout.NORTH);
45: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
46: frame.show();
47:
48: Object[] values = new Object[VALUES_LENGTH];
49: for (int i = 0; i < values.length; i++)
50: values[i] = new Integer(
51: (int)(Math.random() * panel.getHeight()));
52:
53: Runnable r = new Sorter(values, panel, gate);
54: Thread sorterThread = new Thread(r);
55: sorterThread.start();
56: }
57:
58: private static final int FRAME_WIDTH = 300;
59: private static final int FRAME_HEIGHT = 300;
60: private static final int VALUES_LENGTH = 30;
61: }