📄 groupbs.java
字号:
package datastructure;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import java.awt.Color;
import java.awt.Graphics;
class groupBS {
private personBS theArray[];
//// barWidth设计图形宽度
private int barWidth;
//// barSeparation设计图形间隔
private int barSeparation;
private boolean flag = false;
private boolean doneFlag;
private int codePart;
private boolean unsort;
private CodeAnimationPanel codePanel;
Color newColor;
private int a[];
//// comps表示比较的次数
private int comps = 0;
//// swaps表示交换的次数
private int swaps = 0;
//// pass表示第几趟排序
private int pass = 1;
//// count表示一趟排序中的第几次排序
private int count = 0;
public groupBS(CodeAnimationPanel codePanel) {
this.codePanel = codePanel;
//// comps表示比较的次数
comps = 0;
//// swaps表示交换的次数
swaps = 0;
//// pass表示第几趟排序
pass = 1;
//// count表示一趟排序中的第几次排序
count = 0;
unsort = true;
codePart = 0;
a = FrameBS.data.a;
theArray = new personBS[a.length];
BubbleSort.text3.setText(" " + String.valueOf(a.length));
barWidth = 20;
barSeparation = 10;
doneFlag = false;
codePanel.highlight(4);
////// 设计演示图形
for (int k = 0; k < a.length; k++) {
int k1 = (int) (Math.random() * 254);
int i2 = (int) (Math.random() * 254);
int k2 = (int) (Math.random() * 254);
newColor = new Color(k1, i2, k2);
theArray[k] = new personBS(a[k] + 40, newColor);
}
}
public boolean getDone() {
return doneFlag;
}
// 绘制图形矩形下面指针
public void arrowText(Graphics g, Color color, String s, int i, int j,
boolean flag2, boolean flag1) {
int k = 35 + i * (barWidth + barSeparation);
int l = 325 + (j + 1) * 13;
g.setColor(Color.red);
if (flag1) {
g.drawString(s, k + 5, l + 10);
}
if (flag2) {
int k1 = 327;
g.drawLine(k + barWidth / 2, k1, k + barWidth / 2, k1 + 20);
g.drawLine(k + barWidth / 2 + 1, k1, k + barWidth / 2 + 1, k1 + 20);
g.drawLine(k + barWidth / 2, k1, k + barWidth / 2 + 6, k1 + 10);
g.drawLine(k + barWidth / 2 + 1, k1, k + barWidth / 2 + 7, k1 + 10);
g.drawLine(k + barWidth / 2, k1, k + barWidth / 2 - 6, k1 + 10);
g.drawLine(k + barWidth / 2 + 1, k1, k + barWidth / 2 - 5, k1 + 10);
}
}
// 画演示图形的一个矩形
public void drawOneBar(Graphics g, int i) {
int j = theArray[i].getHeight();
int k = 35 + i * (barWidth + barSeparation);
int l = 230 - j + 80;
Color color = theArray[i].getColor();
g.setColor(color);
g.fill3DRect(k, l, barWidth, j, true);
g.setColor(Color.black);
g.drawString(String.valueOf(theArray[i].getHeight()), k + 4, l + j + 12);
}
// 调用函数绘制图形和绘制图形矩形下面指针
public void draw(Graphics g) {
if (flag) {
theArray[a.length - pass + 1].setColor(new Color(97, 194, 255));
flag = false;
}
if (doneFlag) {
for (int i = 0; i <= a.length - pass; i++)
theArray[i].setColor(new Color(97, 194, 255));
}
//g.setColor(Color.lightGray);
//g.fillRect(10, 10, 550, 500);
for (int i = 0; i < a.length; i++)
drawOneBar(g, i);
// g.setColor(Color.lightGray);
// g.fillRect(0, 230, 370, 65);
int tag = 0;
if (count >= a.length - pass) {
count--;
tag = 1;
}
arrowText(g, Color.blue, "j", count, 1, true, true);
arrowText(g, Color.blue, "j+1", count + 1, 1, true, true);
if (tag == 1)
count++;
if (codePart == 4) {
if (theArray[count].getHeight() > theArray[count + 1].getHeight()) {
arrowText(g, Color.red, "交换", count, 2, false, true);
}
else {
arrowText(g, Color.red, "不交换", count, 2, false, true);
}
}
}
// 实现交换
public void swap(int i, int j) {
personBS personbs = theArray[i];
theArray[i] = theArray[j];
theArray[j] = personbs;
}
public void Bubble_sort() {
switch (codePart) {
case 0:
codePanel.highlight(5);
BubbleSort.text4.setText(" " + String.valueOf(pass));
if (pass < a.length && unsort)
codePart = 1;
else
codePart = 2;
break;
case 1:
codePanel.highlight(7);
unsort = false;
BubbleSort.text5.setText(" " + String.valueOf(unsort));
count = 0;
BubbleSort.text6.setText(" " + String.valueOf(count));
codePart = 3;
break;
case 2:
codePanel.highlight(15);
doneFlag = true;
break;
case 3:
codePanel.highlight(8);
if (count < a.length - pass)
codePart = 4;
else
codePart = 5;
break;
case 4:
codePanel.highlight(10);
comps++;
BubbleSort.text1.setText(" " + String.valueOf(comps));
if (theArray[count].getHeight() > theArray[count + 1].getHeight())
codePart = 6;
else
codePart = 8;
break;
case 5:
codePanel.highlight(13);
flag = true;
pass++;
BubbleSort.text4.setText(" " + String.valueOf(pass));
codePart = 0;
break;
case 6:
codePanel.highlight(11);
swaps++;
BubbleSort.text2.setText(" " + String.valueOf(swaps));
swap(count, count + 1);
codePart = 7;
break;
case 7:
codePanel.highlight(12);
codePart = 3;
count++;
unsort = true;
BubbleSort.text5.setText(" " + String.valueOf(unsort));
BubbleSort.text6.setText(" " + String.valueOf(count));
break;
case 8:
codePanel.highlight(13);
count++;
BubbleSort.text6.setText(" " + String.valueOf(count));
codePart = 3;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -