Source of AnimationTester.java


  1: import java.awt.*;
  2: import java.awt.event.*;
  3: import javax.swing.*;
  4: import java.util.concurrent.*;

  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);

 16:       ArrayComponent panel = new ArrayComponent();
 17:       frame.add(panel, BorderLayout.CENTER);

 19:       JButton stepButton = new JButton("Step");
 20:       final JButton runButton = new JButton("Run");

 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);

 29:       Double[] values = new Double[VALUES_LENGTH];
 30:       for (int i = 0; i < values.length; i++)
 31:          values[i] = Math.random() * panel.getHeight();

 33:       final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
 34:       queue.add("Step");

 36:       final Sorter sorter = new Sorter(values, panel, queue);

 38:       stepButton.addActionListener(new
 39:          ActionListener()
 40:          {
 41:             public void actionPerformed(ActionEvent event)
 42:             {
 43:                queue.add("Step");
 44:                runButton.setEnabled(true);
 45:             }
 46:          });

 48:       runButton.addActionListener(new
 49:          ActionListener()
 50:          {
 51:             public void actionPerformed(ActionEvent event)
 52:             {
 53:                runButton.setEnabled(false);
 54:                queue.add("Run");
 55:             }
 56:          });

 58:       Thread sorterThread = new Thread(sorter);
 59:       sorterThread.start();
 60:    }

 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: }