📄 sort.java
字号:
/*
* @(#)Sort.java 1.0 03/05/09
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_2\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
import java.awt.*;
import java.applet.*;
public class Sort extends Applet implements Runnable {
static public int delay = 200;
private Button switchButton, anewButton, pauseButton;
private CheckboxGroup radio;
private Checkbox radioxz, radiomp;
private Scrollbar delaySb;
private Thread sort_thread;
Graphics g;
int begin_x = 68, begin_y = 200;
int a[] = new int[19];
int hold;
boolean flag_run = false, flag_pause = false;
//----------------------------------------------
public void init() {
switchButton = new Button("开始演示");
anewButton = new Button("换组数值");
pauseButton = new Button("暂停");
add(switchButton);
add(pauseButton);
add(anewButton);
delaySb = new Scrollbar (Scrollbar.HORIZONTAL, 10, 0, 1,19 );
add(delaySb);
radio = new CheckboxGroup();
add(radioxz = new Checkbox("选择法排序", radio, true));
add(radiomp = new Checkbox("冒泡法排序", radio, false));
pauseButton.enable(false);
g = getGraphics();
for (int i=0; i<a.length; i++)
a[i] = (int)(Math.random()*90+10);
repaint();
}
public void stop() {
sort_thread.stop();
sort_thread = null;
}
public void run() {
sort();
}
public boolean action(Event e, Object o) {
if (o.equals("开始演示")) {
if (! flag_run || ! sort_thread.isAlive()) {
sort_thread = new Thread(this);
sort_thread.start();
flag_run = true;
}
rubber_bottom(g);
delay = delaySb.getValue()*20;
flag_run = true;
anewButton.enable(false);
pauseButton.enable(true);
radiomp.enable(false);
radioxz.enable(false);
switchButton.setLabel("停止演示");
}
if (o.equals("换组数值")) {
for (int i=0; i<a.length; i++)
a[i] = (int)(Math.random()*90+10);
repaint();
}
if (o.equals("暂停")) {
flag_pause = true;
flag_run = false;
switchButton.enable(false);
pauseButton.setLabel("继续");
}
if (o.equals("继续")) {
flag_pause = false;
flag_run = true;
switchButton.enable(true);
pauseButton.setLabel("暂停");
}
if (o.equals("停止演示")) {
flag_run = false;
anewButton.enable(true);
pauseButton.enable(false);
radiomp.enable(true);
radioxz.enable(true);
rubber_bottom(g);
repaint();
switchButton.setLabel("开始演示");
}
if (e.target instanceof Checkbox)
repaint();
return true;
}
public boolean handleEvent(Event e) {
delay = delaySb.getValue()*20;
return super.handleEvent(e);
}
public void paint(Graphics g) {
switchButton.reshape(60,25,80,20);
pauseButton.reshape(160,25,60,20);
anewButton.reshape(240,25,80,20);
delaySb.reshape(350,31,88,15);
g.drawString("快←←←←←慢",353,29);
radioxz.reshape(66,5,80,20);
radiomp.reshape(155,5,80,20);
g.drawString("数列:", 18, begin_y);
int x = begin_x, y = begin_y;
for (int i=0; i<a.length; i++) {
print_int(g, x, y, a[i]);
print_rectangle(g, x, y, a[i]);
x += 20;
}
if (flag_run) {
delay();
}
}
//---------------排序---------------
//选择法排序
public void sort_xz() {
for (int i = 0; i < a.length; i++){
print_sign(g, i, 1);
delay();
int k = i;
for (int j = i+1; j < a.length; j++){
if (!flag_run) break;
if (j != i+1) {
rubber_sign(g, j-1, 2);
if (i != k)
print_sign(g, k, 3);
}
print_sign(g, j, 2);
delay();
if (a[k] > a[j]) {
if (k != i)
rubber_sign(g, k, 3);
k = j;
print_sign(g, k, 3);
delay();
}
do {}
while (flag_pause);
}
if (!flag_run) break;
if (i != a.length-1) {
rubber_sign(g, a.length-1, 2);
if (i != k)
print_sign(g, k, 3);
}
delay();
if (k != i) {
swap_print(g, a[i], a[k], i, k);
rubber_sign(g, k, 1);
hold = a[i];
a[i] = a[k];
a[k] = hold;
}
else delay();
if (flag_run) {
moor(g,i);
delay();
}
}
flag_run = false;
}
//气泡法排序
public void sort_mp() {
int k = 0;
for (int pass = 1; pass < a.length; pass++){
k = pass;
boolean flag_swap = true;
for (int i = 0; i < a.length - pass; i++){
if (!flag_run) break;
if (i != 0)
rubber_Arc(g,i-1);
print_Arc(g,i);
delay();
if (a[i] > a[i+1]) {
swap_print(g, a[i], a[i+1], i, i+1);
hold = a[i];
a[i] = a[i+1];
a[i+1] = hold;
flag_swap = false;
}
else delay();
do {}
while (flag_pause);
}
if ((!flag_run) || (flag_swap)) break;
if (flag_run) {
rubber_Arc(g, a.length - pass - 1);
moor(g, a.length - pass);
}
}
if (flag_run) {
rubber_Arc(g, a.length - k - 1);
while (k <= a.length) {
moor(g, a.length - k);
k += 1;
}
}
flag_run = false;
}
public void sort() {
//选择法排序
if (radioxz.getState()) {
sort_xz();
}
//气泡法排序
if (radiomp.getState()) {
sort_mp();
}
flag_run = false;
anewButton.enable(true);
pauseButton.enable(false);
radiomp.enable(true);
radioxz.enable(true);
switchButton.setLabel("开始演示");
}
// 输出数字(坐标x,坐标y,值)
public void print_int(Graphics g, int x, int y, int n) {
g.drawString(Integer.toString(n), x, y);
}
// 画长方形(坐标x,坐标y,高度)
public void print_rectangle(Graphics g, int x, int y, int height) {
g.fillRoundRect(x+3, y-(height+20), 8, height, 2, 2);
}
// 画弧(第i个元素)
public void print_Arc(Graphics g, int i) {
g.setColor(new Color (0,0,0));
g.drawArc((i*20+begin_x+7), begin_y, 20, 7, 180, 180);
}
// 除弧(第i个元素)
public void rubber_Arc(Graphics g, int i) {
g.setColor(Color.lightGray);
g.drawArc((i*20+begin_x+7), begin_y, 20, 7, 180, 180);
}
public void rubber_bottom(Graphics g) {
g.setColor(Color.lightGray);
g.fillRoundRect(begin_x, begin_y, 375, 12, 0, 0);
g.setColor(Color.black);
}
// 画底标
public void print_sign(Graphics g, int i, int x) {
g.setColor(new Color(0,0,0));
if (x==1)
g.drawString("▲", i*20+begin_x+2, begin_y+10);
else
if (x==2)
g.drawString("↑", i*20+begin_x+2, begin_y+10);
else
g.drawString("△", i*20+begin_x+2, begin_y+10);
}
// 除底标
public void rubber_sign(Graphics g, int i, int x) {
g.setColor(Color.lightGray);
if (x==1)
g.drawString("▲", i*20+begin_x+2, begin_y+10);
else
if (x==2)
g.drawString("↑", i*20+begin_x+2, begin_y+10);
else
g.drawString("△", i*20+begin_x+2, begin_y+10);
}
// 交换图(被交换 a,被交换 b, 元素 i, 元素 j)
public void swap_print(Graphics g, int a, int b, int i, int j) {
g.setColor(Color.lightGray);
g.fillRoundRect((i*20+begin_x), begin_y-10, 15, 10,0,0);
g.fillRoundRect((j*20+begin_x), begin_y-10, 15, 10,0,0);
print_rectangle(g, i*20+begin_x, begin_y, a);
print_rectangle(g, j*20+begin_x, begin_y, b);
delay();
g.setColor(new Color (0,0,0));
print_int(g, i*20+begin_x, begin_y, b);
print_int(g, j*20+begin_x, begin_y, a);
print_rectangle(g, i*20+begin_x, begin_y, b);
print_rectangle(g, j*20+begin_x, begin_y, a);
delay();
}
//固定元素(第i个元素)
public void moor(Graphics g, int i) {
delay();
g.setColor(new Color (128,0,0));
print_int(g, i*20+begin_x, begin_y, a[i]);
print_rectangle(g, i*20+begin_x, begin_y, a[i]);
g.drawString("▲", i*20+begin_x+2, begin_y+10);
delay();delay();
}
//时延
void delay() {
try {
Thread.sleep(Sort.delay);
}
catch(InterruptedException e){
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -