|
1 | 1 | import java.awt.Color;
|
| 2 | +import java.awt.Dimension; |
| 3 | +import java.awt.Graphics; |
| 4 | +import java.awt.Graphics2D; |
| 5 | +import java.awt.geom.Rectangle2D; |
| 6 | +import java.util.Random; |
2 | 7 |
|
3 | 8 | import javax.swing.JFrame;
|
4 | 9 | import javax.swing.JPanel;
|
| 10 | +import javax.swing.SwingUtilities; |
| 11 | +import javax.swing.SwingWorker; |
5 | 12 |
|
6 | 13 | public class SortingAlgorithm extends JPanel {
|
| 14 | + private final int WIDTH = 800, HEIGHT = WIDTH * 9 /16; |
| 15 | + private final int SIZE = 200; // the number if sorting elements |
| 16 | + private final float BAR_WIDTH = (float)WIDTH / SIZE; // bar width |
| 17 | + private float[] bar_height = new float[SIZE]; // height of bars |
| 18 | + private SwingWorker<Void, Void> shuffler, sorter; |
| 19 | + private int current_index, traversing_index; |
| 20 | + |
7 | 21 | SortingAlgorithm() {
|
8 |
| - setBackground(Color.BLUE); |
| 22 | + setBackground(Color.BLACK); |
| 23 | + setPreferredSize(new Dimension(WIDTH, HEIGHT)); |
| 24 | + initBarHeight(); // initialize the height of each bar |
| 25 | + initSorter(); |
| 26 | + initShuffler(); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void paintComponent(Graphics g) { |
| 31 | + super.paintComponent(g); |
| 32 | + |
| 33 | + Graphics2D g2d = (Graphics2D)g; |
| 34 | + g2d.setColor(Color.CYAN); |
| 35 | + Rectangle2D.Float bar; |
| 36 | + for(int i = 0; i < SIZE; i++ ) { |
| 37 | + bar = new Rectangle2D.Float(i * BAR_WIDTH, 0, BAR_WIDTH, bar_height[i]); |
| 38 | + g2d.fill(bar); // g2d.draw(bar); |
| 39 | + } |
| 40 | + |
| 41 | + g2d.setColor(Color.RED); |
| 42 | + bar = new Rectangle2D.Float(current_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[current_index]); |
| 43 | + g2d.fill(bar); |
| 44 | + |
| 45 | + g2d.setColor(Color.GREEN); |
| 46 | + bar = new Rectangle2D.Float(traversing_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[traversing_index]); |
| 47 | + g2d.fill(bar); |
| 48 | + } |
| 49 | + |
| 50 | + public void initBarHeight() { |
| 51 | + float interval = (float)HEIGHT / SIZE; |
| 52 | + for(int i = 0; i < SIZE; i++) { |
| 53 | + bar_height[i] = i * interval; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public void initSorter() { |
| 58 | + sorter = new SwingWorker<>() { |
| 59 | + @Override |
| 60 | + public Void doInBackground() throws InterruptedException { |
| 61 | + for(current_index = 1; current_index < SIZE; current_index++) { |
| 62 | + traversing_index = current_index; |
| 63 | + while(traversing_index > 0 && bar_height[traversing_index] < bar_height[traversing_index - 1]) { |
| 64 | + swap(traversing_index, traversing_index - 1); |
| 65 | + traversing_index--; |
| 66 | + |
| 67 | + Thread.sleep(1); |
| 68 | + repaint(); |
| 69 | + } |
| 70 | + } |
| 71 | + current_index = 0; |
| 72 | + traversing_index = 0; |
| 73 | + |
| 74 | + return null; |
| 75 | + } |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + public void initShuffler() { |
| 80 | + shuffler = new SwingWorker<>() { |
| 81 | + @Override |
| 82 | + public Void doInBackground() throws InterruptedException { |
| 83 | + int middle = SIZE / 2; |
| 84 | + for (int i = 0, j = middle; i < middle; i++, j++) { |
| 85 | + int randow_index = new Random().nextInt(SIZE); |
| 86 | + swap(i, randow_index); |
| 87 | + |
| 88 | + randow_index = new Random().nextInt(SIZE); |
| 89 | + swap(j, randow_index); |
| 90 | + |
| 91 | + Thread.sleep(10); |
| 92 | + repaint(); |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void done() { |
| 99 | + super.done(); |
| 100 | + sorter.execute(); |
| 101 | + } |
| 102 | + }; |
| 103 | + shuffler.execute(); |
| 104 | + } |
| 105 | + |
| 106 | + public void swap(int indexA, int indexB) { |
| 107 | + float temp = bar_height[indexA]; |
| 108 | + bar_height[indexA] = bar_height[indexB]; |
| 109 | + bar_height[indexB] = temp; |
9 | 110 | }
|
10 | 111 |
|
11 | 112 | }
|
0 commit comments