代码:
package com.thread.singal;
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Rectangle2D; import java.util.concurrent.Semaphore;
import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel;
/** * * @author yangjianzhou * @description TODO * @time Dec 5, 2014 : 8:17:30 PM */ public class AlgorithmAnimation {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() { JFrame frame = new AnimationFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
}); } }
class AnimationFrame extends JFrame {
/** * */ private static final long serialVersionUID = 1L;
private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 300;
/** * set layout */ public AnimationFrame() { ArrayComponent comp = new ArrayComponent(); add(comp, BorderLayout.CENTER); final Sorter sorter = new Sorter(comp);
JButton runButton = new JButton("Run"); runButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent event) { sorter.setRun(); } });
JButton stepButton = new JButton("Step"); stepButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent event) { sorter.setStep(); } });
JPanel buttons = new JPanel(); buttons.add(runButton); buttons.add(stepButton); add(buttons, BorderLayout.NORTH); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); this.setTitle("Bubble Sort");
Thread t = new Thread(sorter); t.start(); }
}
class Sorter implements Runnable {
private Double[] values; private ArrayComponent component; private Semaphore gate; private static final int DELAY = 100; private volatile boolean run; private static final int VALUES_LENGTH = 30;
/* init data */ public Sorter(ArrayComponent comp) { values = new Double[VALUES_LENGTH]; for (int i = 0; i < values.length; i++) { values[i] = new Double(Math.random()); } this.component = comp; this.gate = new Semaphore(1); this.run = false; }
/** * sort in one time */ public void setRun() { run = true; gate.release(); }
/** * sort step by step */ public void setStep() { run = false; gate.release(); }
public void run() {
component.setValues(values, values[0]); sort(values); component.setValues(values, null); }
/** * bub sort * * @param values */ public void sort(Double[] values) { int len = values.length; Double temp; for (int i = 0; i < len; i++) { for (int j = len - 1; j > i; j--) { if (values[j] < values[j - 1]) { temp = values[j - 1]; values[j - 1] = values[j]; values[j] = temp; component.setValues(values, values[j - 1]); try { if (run) { // show the draw process Thread.sleep(DELAY); } else { gate.acquire(); } } catch (InterruptedException exception) { Thread.currentThread().interrupt(); } } } } }
}
class ArrayComponent extends JComponent {
/** * */ private static final long serialVersionUID = 1L;
private Double marked;
private Double[] values;
public synchronized void setValues(Double[] values, Double marked) { this.values = values; this.marked = marked; repaint(); }
/** * paint the bars */ public synchronized void paintComponent(Graphics g) { if (values == null) { return; } Graphics2D g2 = (Graphics2D) g; int width = getWidth() / values.length; int compHeight = getHeight(); for (int i = 0; i < values.length; i++) { double height = values[i] * getHeight(); Rectangle2D bar = new Rectangle2D.Double(width * i, compHeight - height, width, height); if (values[i] == marked) { g2.fill(bar); } else { g2.draw(bar); } } } }
显示效果:
[img]http://dl2.iteye.com/upload/attachment/0104/1293/ce5f926f-8fcc-3342-bae7-6df5918f124e.png[/img] |