SE450: Pausing and Running the Animation [36/36] Previous pageContents

file:horstmann/ch09_animation2/Sorter.java [source] [doc-public] [doc-private]
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
package horstmann.ch09_animation2;
import java.util.Comparator;
import java.util.concurrent.BlockingQueue;

/**
   This runnable executes a sort algorithm.
   When two elements are compared, the algorithm
   pauses and updates a panel.
 */
public class Sorter implements Runnable
{
  public Sorter(Double[] values, ArrayComponent panel, BlockingQueue<String> queue)
  {
    this.values = values;
    this.panel = panel;
    this.queue = queue;
  }

  public void run()
  {
    Comparator<Double> comp = (d1, d2) -> {
      try
      {
        String command = queue.take();
        if (command.equals("Run"))
        {
          Thread.sleep(DELAY);
          if (!"Step".equals(queue.peek()))
            queue.add("Run");
        }
      }
      catch (InterruptedException exception)
      {
        Thread.currentThread().interrupt();
      }
      panel.setValues(values, d1, d2);
      return d1.compareTo(d2);
    };
    MergeSorter.sort(values, comp);
    panel.setValues(values, null, null);
  }

  private Double[] values;
  private ArrayComponent panel;
  private BlockingQueue<String> queue;
  private static final int DELAY = 100;
}

file:horstmann/ch09_animation2/AnimationTester.java [source] [doc-public] [doc-private]
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;
}

Previous pageContents