01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package horstmann.ch09_animation2;
import java.awt.BorderLayout;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
This program animates a sort algorithm.
*/
public class AnimationTester
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ArrayComponent panel = new ArrayComponent();
frame.add(panel, BorderLayout.CENTER);
JButton stepButton = new JButton("Step");
final JButton runButton = new JButton("Run");
JPanel buttons = new JPanel();
buttons.add(stepButton);
buttons.add(runButton);
frame.add(buttons, BorderLayout.NORTH);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
Double[] values = new Double[VALUES_LENGTH];
for (int i = 0; i < values.length; i++)
values[i] = Math.random() * panel.getHeight();
final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
queue.add("Step");
final Sorter sorter = new Sorter(values, panel, queue);
stepButton.addActionListener(event -> {
queue.add("Step");
runButton.setEnabled(true);
});
runButton.addActionListener(event -> {
runButton.setEnabled(false);
queue.add("Run");
});
Thread sorterThread = new Thread(sorter);
sorterThread.start();
}
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;
private static final int VALUES_LENGTH = 30;
}
|