图形化显示---冒泡排序

代码:

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值