/** */ package heart5.thread; import heart5.thread.BaiSortThread; import java.awt.*; import java.io.InputStream; import java.util.Hashtable;/** * A simple applet class to demonstrate a sort algorithm. */ public class BaiSortItem extends java.applet.Applet { /** *设置调试控制变量 */ private boolean debug = true; /** * The thread that is sorting (or null). */ java.awt.Canvas[] baiCanvas; java.awt.Button baiButton; int HEIGHT=160; int WIDTH=140; /** * The sorting algorithm (or null). */ BaiSortAlgorithm[] algorithmoo; private Thread[] kicker; /** * The array that is being sorted. */ public int[][] arr; /** * Fill the array with random numbers from 0..n-1. *依据画布的大小生成随机数列,包含0到n-1。 */ void scramble() { int[] a = new int[ HEIGHT/ 2]; //算出斜率 double f = WIDTH / (double) a.length; //生成顺序排列的数列 for (int i = a.length; --i >= 0;) { a[i] = (int)(i * f); } //打乱次序 for (int i = a.length; --i >= 0;) { int j = (int)(i * Math.random()); int t = a[i]; a[i] = a[j]; a[j] = t; } arr = new int[3][]; arr[0] = a;
setBackground(Color.darkGray); scramble(); //resize(100, 100); } /** * Run the sorting algorithm. This method is * called by class Thread once the sorting algorithm * is started. * @see java.lang.Thread#run * @see SortItem#mouseUp */ private synchronized void startSort() { //scramble(); repaint(); kicker[0] = new BaiSortThread(algorithmoo[0],arr[0],baiCanvas[0]); kicker[0].start (); }}
package heart5.thread; import java.awt.Color; public class BaiSortThread extends Thread { /** *设置调试控制变量 */ private boolean debug = true; /** * The thread that is sorting (or null). */ private Thread sorter; java.awt.Graphics baig;
*/
package heart5.thread;
import heart5.thread.BaiSortThread;
import java.awt.*;
import java.io.InputStream;
import java.util.Hashtable;/**
* A simple applet class to demonstrate a sort algorithm.
*/
public class BaiSortItem extends java.applet.Applet {
/**
*设置调试控制变量
*/
private boolean debug = true;
/**
* The thread that is sorting (or null).
*/
java.awt.Canvas[] baiCanvas;
java.awt.Button baiButton;
int HEIGHT=160;
int WIDTH=140;
/**
* The sorting algorithm (or null).
*/
BaiSortAlgorithm[] algorithmoo;
private Thread[] kicker; /**
* The array that is being sorted.
*/
public int[][] arr; /**
* Fill the array with random numbers from 0..n-1.
*依据画布的大小生成随机数列,包含0到n-1。
*/
void scramble() {
int[] a = new int[ HEIGHT/ 2];
//算出斜率
double f = WIDTH / (double) a.length;
//生成顺序排列的数列
for (int i = a.length; --i >= 0;) {
a[i] = (int)(i * f);
}
//打乱次序
for (int i = a.length; --i >= 0;) {
int j = (int)(i * Math.random());
int t = a[i];
a[i] = a[j];
a[j] = t;
}
arr = new int[3][];
arr[0] = a;
//拷贝到另外两个中,使之内容相同以便比较速度;
int[] bb = new int[a.length];
for(int j=0;j<a.length-1;j++){
bb[j] = a[j];
}
arr[1] = bb;
int[] cc = new int[a.length];
for(int j=0;j<a.length-1;j++){
cc[j] = a[j];
}
arr[2] = cc;
if (debug==true) {
System.out.println ("数组准备完毕");}
}
/**
* Initialize the applet.
*/
public void init() {
String algName[]= {"BubbleSortAlgorithm","BidirectionalBubbleSortAlgorithm","QSortAlgorithm"};
algorithmoo =new BaiSortAlgorithm[3] ;
for(int i=0;i<algorithmoo.length;i++){
try {
if (algorithmoo[i] == null) {
algorithmoo[i] = (BaiSortAlgorithm)Class.forName("heart5.thread."+algName).newInstance();
//algorithmoo[i].setParent(this);
}
} catch(Exception e) {
}}
kicker =new Thread[3] ;
//生成画面,放置三个画布和一个按钮;
//this.setLayout (new java.awt.GridLayout(3,4));
Color[] baiColor={Color.blue,Color.red,Color.pink};
Canvas[] baiCanvas=new Canvas[3];
for (int i=baiCanvas.length-1;i>=0;i--){
baiCanvas[i]=new Canvas();
baiCanvas[i].setSize (WIDTH,HEIGHT);
baiCanvas[i].setBackground (baiColor[i]);
add(baiCanvas[i]);
} baiButton=new Button();
baiButton.setSize (40,40);
baiButton.setBackground (Color.orange);
baiButton.setLabel ("计算");
add(baiButton);
//使按钮响应鼠标事件;
baiButton.addMouseListener (new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
scramble();
startSort();
}
});
validate();
setBackground(Color.darkGray);
scramble();
//resize(100, 100);
}
/**
* Run the sorting algorithm. This method is
* called by class Thread once the sorting algorithm
* is started.
* @see java.lang.Thread#run
* @see SortItem#mouseUp
*/
private synchronized void startSort() {
//scramble();
repaint();
kicker[0] = new BaiSortThread(algorithmoo[0],arr[0],baiCanvas[0]);
kicker[0].start ();
}}
import java.awt.Color;
public class BaiSortThread extends Thread {
/**
*设置调试控制变量
*/
private boolean debug = true;
/**
* The thread that is sorting (or null).
*/
private Thread sorter;
java.awt.Graphics baig;
private int[] arofint;
private java.awt.Canvas sortcan;
/**
* The name of the algorithm.
*/
String[] algName; /**
* The high water .
*/
public int h1 = -1; /**
* The low water .
*/
public int h2 = -1; /**
* The sorting algorithm (or null).
*/
BaiSortAlgorithm algorithm; public BaiSortThread(BaiSortAlgorithm bsa,int[] aa,java.awt.Canvas canb) {
super();
algorithm = bsa;
arofint = aa;
sortcan = canb;
baig = sortcan.getGraphics ();
bsa.setParent (this);
} public void run() {
if (debug==true) {
System.out.println ("运行中"+Thread.currentThread ().toString ());}
try {
if (algorithm == null) {
algorithm.setParent(this);
if (debug==true) {
System.out.println ("排序算法已经挂接,名字为"+algorithm.toString ());
}
}
algorithm.init();
algorithm.sort(arofint);
} catch(Exception e) {
}
} /**
* Pause a while.
* @see SortAlgorithm
*/
void pause() {
pause(-1, -1);
} /**
* Pause a while, and draw the high water .
* @see SortAlgorithm
*/
void pause(int H1) {
pause(H1, -1);
} /**
* Pause a while, and draw the low&high water s.
* @see SortAlgorithm
*/
void pause(int H1, int H2) {
h1 = H1;
h2 = H2;
if (sorter != null) {
sortcan.repaint();
}
try {Thread.sleep(10);} catch (InterruptedException e){}
} /**
* Paint the array of numbers as a list
* of horizontal lines of varying lenghts.
*/
private void baiPaint(java.awt.Graphics gg,int[] aa){
int[] a=aa;
int y = sortcan.HEIGHT - 1; // Erase old lines
gg.setColor(Color.black);
for (int i = a.length; --i >= 0; y -= 2) {
gg.drawLine(a[i], y, sortcan.WIDTH, y);
} // Draw new lines
gg.setColor(Color.green.brighter ());
y = sortcan.HEIGHT - 1;
for (int i = a.length; --i >= 0; y -= 2) {
gg.drawLine(0, y, a[i], y);
} if (h1 >= 0) {
gg.setColor(Color.red);
y = h1 * 2 + 1;
gg.drawLine(0, y, sortcan.WIDTH, y);
}
if (h2 >= 0) {
gg.setColor(Color.blue);
y = h2 * 2 + 1;
gg.drawLine(0, y, sortcan.WIDTH, y);
}
}
public void paint(java.awt.Graphics g) {
baiPaint(baig,arofint);
} /**
* Update without erasing the background.
*/
public void update(java.awt.Graphics g) {
paint(g);
//paint(baiCanvas[1].getGraphics ());
}
}