我试图通过画线制作一个排序算法,所以它看起来像一个图表。这里的问题是,当数组排序时,我无法将进度视为移动图。当我输入睡眠时间时,输出只显示长线,在睡眠线之后显示完整的排序图。所以我只需要在图表排序时做一些事情来查看进度。在这种情况下,我使用的是 bublesort。我非常感谢您的每一次帮助!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class newClass extends JFrame {
public newClass() {
final Thread hei = new Thread(new Runnable() {
public void run() {
bublesort();
}
});
hei.start();
}
int[] array = { 400, 420, 380, 120, 340, 179, 454, 400, 420, 380, 120, 340, 179, 454, 400, 420, 380, 120, 340, 179 };
public void updateGraphic() {
paint(this.getGraphics());
}
public void paint(Graphics g) {
for (int i = 0; i < array.length; i++) {
int tjue = 20;
g.drawLine(i * tjue, 500, i * tjue, array[i]);
}
for (int j = 0; j < array.length - 1; j++) {
for (int x = 0; x < array.length - 1; x++) {
if (array[x] > array[x + 1]) {
int temp = array[x];
array[x] = array[x + 1];
array[x + 1] = temp;
updateGraphic();
this.validate();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(newClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
}