Skip to content

Commit ca05e6e

Browse files
committed
Partially working
1 parent 6907c3c commit ca05e6e

File tree

2 files changed

+106
-29
lines changed

2 files changed

+106
-29
lines changed

src/tk/jimgao/BarGraphWindow.java

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ public class BarGraphWindow extends JFrame {
77

88
public static final int Y_OFFSET = 50;
99
public static final int Y_BOTTOM_OFFSET = 60;
10-
public static final double Y_SPACE = 0.05;
10+
public static final int X_SPACE_ABS = 2;
1111

12-
public static final int REDRAW_DELAY = 250;
12+
public static final int REDRAW_DELAY = 50;
1313

1414
public static final Color DEFAULT_COLOR = new Color(93, 173, 226);
1515
public static final Color RANGE_COLOR = new Color(243, 156, 18);
1616
public static final Color PIVOT_COLOR = new Color(211, 84, 0);
1717
public static final Color BORDER_COLOR = new Color(21, 67, 96);
1818

19-
public static boolean DRAW_BORDER = true;
19+
public static boolean DRAW_BORDER = false;
20+
public static boolean DRAW_NUMBER = false;
2021

2122
public int relWidth, relSpace, relHeight;
2223
public int[] barXLeft, barXRight, barValues;
2324
public Color[] barColor;
2425

2526
public BarGraphWindow() {
26-
this.setSize(800, 600);
27+
this.setSize(1800, 1024);
2728
this.setVisible(true);
2829
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
2930

@@ -37,9 +38,8 @@ public int findMax(int[] values){
3738
}
3839

3940
public void initBarGraph(int[] values){
40-
this.relWidth = (int)(this.getWidth() / (values.length * (1 + Y_SPACE * 2)));
41-
this.relSpace = (int)(relWidth * Y_SPACE);
42-
this.relHeight = (int)((this.getHeight() - Y_OFFSET - Y_BOTTOM_OFFSET) / findMax(values) * (1 - Y_SPACE));
41+
this.relWidth = (int)((this.getWidth() - values.length) / values.length);
42+
this.relHeight = (int)((this.getHeight() - Y_OFFSET - Y_BOTTOM_OFFSET) / findMax(values));
4343

4444
this.barValues = values.clone();
4545
this.barXLeft = new int[values.length];
@@ -52,20 +52,67 @@ public void initBarGraph(int[] values){
5252
barXRight[i] = barXLeft[i] + relWidth;
5353
barColor[i] = BarGraphWindow.DEFAULT_COLOR;
5454

55-
prevX += relWidth + relSpace;
55+
prevX += relWidth + X_SPACE_ABS;
5656
}
5757
}
5858

5959
public void updateBarGraph(int[] values){
60-
this.relHeight = (int)((this.getHeight() - Y_OFFSET - Y_BOTTOM_OFFSET) / findMax(values) * (1 - Y_SPACE));
61-
this.barValues = values.clone();
62-
int prevX = relSpace;
63-
for (int i = 0; i < values.length; i++){
64-
barXLeft[i] = prevX;
65-
barXRight[i] = barXLeft[i] + relWidth;
60+
int newHeight = (this.getHeight() - Y_OFFSET - Y_BOTTOM_OFFSET) / findMax(values);
61+
62+
if (newHeight == this.relHeight){
63+
for (int i = 0; i < values.length; i++){
64+
if (this.barValues[i] != values[i]){
65+
this.barValues[i] = values[i];
66+
redrawBar(i);
67+
}
68+
}
69+
} else {
70+
this.relHeight = newHeight;
6671

67-
prevX += relWidth + relSpace;
72+
this.barValues = values.clone();
73+
int prevX = relSpace;
74+
for (int i = 0; i < values.length; i++){
75+
barXLeft[i] = prevX;
76+
barXRight[i] = barXLeft[i] + relWidth;
77+
78+
prevX += relWidth + relSpace;
79+
}
6880
}
81+
82+
try {
83+
Thread.sleep(REDRAW_DELAY);
84+
} catch(Exception ex){}
85+
}
86+
87+
public synchronized void clearBar(int index){
88+
Graphics g = this.getGraphics();
89+
g.clearRect(barXLeft[index], 0, relWidth, this.getHeight());
90+
}
91+
92+
public synchronized void drawBar(int index){
93+
Graphics2D g = (Graphics2D) this.getGraphics();
94+
int leftCornerX = barXLeft[index];
95+
int leftCornerY = this.getHeight() - Y_BOTTOM_OFFSET - relHeight * barValues[index];
96+
int barHeight = relHeight * barValues[index];
97+
98+
g.setColor(barColor[index]);
99+
g.fillRect(leftCornerX, leftCornerY + Y_OFFSET, relWidth, barHeight);
100+
101+
if (DRAW_BORDER){
102+
g.setColor(BarGraphWindow.BORDER_COLOR);
103+
g.drawRect(leftCornerX, leftCornerY + Y_OFFSET, relWidth, barHeight);
104+
}
105+
106+
if (DRAW_NUMBER) {
107+
g.setColor(Color.BLACK);
108+
g.setFont(new Font("Consolas", Font.BOLD, 14));
109+
g.drawString(Integer.toString(barValues[index]), leftCornerX + 10, leftCornerY);
110+
}
111+
}
112+
113+
public void redrawBar(int index){
114+
clearBar(index);
115+
drawBar(index);
69116
}
70117

71118
public synchronized void drawBarGraph(){
@@ -87,9 +134,11 @@ public synchronized void drawBarGraph(){
87134
g.drawRect(leftCornerX, leftCornerY + Y_OFFSET, relWidth, barHeight);
88135
}
89136

90-
g.setColor(Color.BLACK);
91-
g.setFont(new Font("Consolas", Font.BOLD, 14));
92-
g.drawString(Integer.toString(barValues[i]), leftCornerX + 10, leftCornerY);
137+
if (DRAW_NUMBER) {
138+
g.setColor(Color.BLACK);
139+
g.setFont(new Font("Consolas", Font.BOLD, 14));
140+
g.drawString(Integer.toString(barValues[i]), leftCornerX + 10, leftCornerY);
141+
}
93142
}
94143

95144
try {

src/tk/jimgao/QuickSortMain.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tk.jimgao;
22

3-
import java.awt.*;
4-
import java.util.Arrays;
3+
import java.util.HashSet;
4+
import java.util.Set;
55

66
public class QuickSortMain {
77
public static int[] numbers;
@@ -21,8 +21,10 @@ public static int partition(int left, int right){
2121
swap(i, pIndex);
2222
window.barColor[pIndex] = BarGraphWindow.RANGE_COLOR;
2323
window.barColor[pIndex + 1] = BarGraphWindow.PIVOT_COLOR;
24+
window.redrawBar(pIndex);
25+
window.redrawBar(pIndex + 1);
26+
2427
window.updateBarGraph(numbers);
25-
window.drawBarGraph();
2628
pIndex++;
2729
}
2830
}
@@ -34,32 +36,58 @@ public static int partition(int left, int right){
3436

3537
public static void quickSort(int left, int right){
3638
if (left > right) return;
37-
System.out.printf("left = %d, right = %d\n", left, right);
3839

39-
Arrays.fill(window.barColor, BarGraphWindow.DEFAULT_COLOR);
40+
window.setTitle(String.format("left = %d, right = %d", left, right));
41+
42+
for (int i = 0; i < left; i++){
43+
if (window.barColor[i] != BarGraphWindow.DEFAULT_COLOR){
44+
window.barColor[i] = BarGraphWindow.DEFAULT_COLOR;
45+
window.redrawBar(i);
46+
}
47+
}
48+
49+
for (int i = right + 1; i < numbers.length; i++){
50+
if (window.barColor[i] != BarGraphWindow.DEFAULT_COLOR){
51+
window.barColor[i] = BarGraphWindow.DEFAULT_COLOR;
52+
window.redrawBar(i);
53+
}
54+
}
55+
4056
for (int i = left; i <= right; i++){
41-
window.barColor[i] = BarGraphWindow.RANGE_COLOR;
57+
if (window.barColor[i] != BarGraphWindow.RANGE_COLOR){
58+
window.barColor[i] = BarGraphWindow.RANGE_COLOR;
59+
window.redrawBar(i);
60+
}
4261
}
43-
window.drawBarGraph();
4462

4563
if (left == right) return;
4664

4765
int pivotIndex = partition(left, right);
4866
window.updateBarGraph(numbers);
49-
window.drawBarGraph();
5067

5168
quickSort(left, pivotIndex - 1);
5269
quickSort(pivotIndex + 1, right);
5370
}
5471

5572
public static void main(String[] args){
73+
Set<Integer> visited = new HashSet<>();
74+
5675
window = new BarGraphWindow();
57-
numbers = new int[25];
58-
for (int i = 0; i < 25; i++) numbers[i] = (int)(Math.random() * 50) + 1;
76+
numbers = new int[400];
77+
for (int i = 0; i < 400; i++) {
78+
numbers[i] = (int)(Math.random() * 400) + 1;
79+
if (visited.contains(numbers[i])){
80+
i--;
81+
continue;
82+
}
83+
visited.add(numbers[i]);
84+
}
5985
window.initBarGraph(numbers);
6086

6187
window.drawBarGraph();
88+
quickSort(0, 399);
6289

63-
quickSort(0, 24);
90+
window.initBarGraph(numbers);
91+
window.drawBarGraph();
6492
}
6593
}

0 commit comments

Comments
 (0)