-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSortPanel.java
More file actions
177 lines (152 loc) · 5.61 KB
/
BubbleSortPanel.java
File metadata and controls
177 lines (152 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package src.classSrc;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class BubbleSortPanel extends JPanel {
public TimerAction timerAction;
public Timer timer;
private final String str;
private final String[] code;
private final JList<String> codeList;
private JScrollPane codeScrollPane;
int fontSize = 15;
private List<NumberRectangle> numbers = initialNumberRectangles();
public BubbleSortPanel() {
// panel setting
setLayout(null);
setOpaque(false);
// set Timer
timerAction = new TimerAction();
timer = new Timer(1000, timerAction);
// set code list ( change string -> stringList[] )
SortInfoReader reader = new SortInfoReader("src/textSrc/BubbleSort.txt");
str = reader.getContent();
code = str.split("\n");
codeList = new JList<String>(code);
// set code text area
codeList.setFont(new Font("Consolas", Font.PLAIN, fontSize));
codeScrollPane = new JScrollPane(codeList);
codeScrollPane.setBounds(510, 100, 425, 400);
add(codeScrollPane);
}
// if sort is finished
private boolean completed = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawNumberRectangles(g2);
}
private void drawNumberRectangles(Graphics2D g2) {
// draw chart bar area
Rectangle2D.Double area = new Rectangle2D.Double(50, 100, 425, 398);
g2.setPaint(Color.white);
g2.fill(area);
g2.setPaint(Color.BLACK);
g2.draw(area);
for (NumberRectangle rectangle : numbers) {
rectangle.draw(g2);
}
}
// set variable in Selectionsertion Sort algorithm
int i = 0; // 0~9, total 10
int j = 0; // 0~9, total 10
int k = 0; // the minIndex of clean up the BLUE color
boolean first = false;
int lineIndex = 1;
int temp = 0;
private class TimerAction implements ActionListener, Serializable {
public void actionPerformed(ActionEvent event) {
switch (lineIndex) {
case 1: // i loop start
codeList.setSelectedIndex(lineIndex);
lineIndex++;
break;
case 2: // i loop start
codeList.setSelectedIndex(lineIndex);
// initialize
if (i < 10) {
j = 0;
lineIndex++;
} else { // end i loop
lineIndex = 10;
}
break;
case 3:
codeList.setSelectedIndex(lineIndex);
for (k = 0; k < 10; k++)
numbers.get(k).setColor(Color.WHITE);
if (j < 9) {// j < arr.length - i - 1
numbers.get(j).setColor(Color.RED); // set target color = red
lineIndex++;
// System.out.println(lineIndex);
} else {
lineIndex = 2;
i++;
}
break;
case 4:
numbers.get(j + 1).setColor(Color.BLUE);
codeList.setSelectedIndex(lineIndex);
if (numbers.get(j).getValue() > numbers.get(j + 1).getValue()) {
lineIndex++;
} else {
lineIndex = 8;
}
break;
case 5:
codeList.setSelectedIndex(lineIndex);
temp = numbers.get(j).getValue();
lineIndex++;
break;
case 6:
codeList.setSelectedIndex(lineIndex);
numbers.get(j).setValue(numbers.get(j + 1).getValue());
lineIndex++;
break;
case 7: // case 7~9 operate swap()
codeList.setSelectedIndex(lineIndex);
numbers.get(j + 1).setValue(temp);
lineIndex++;
break;
case 8:
codeList.setSelectedIndex(lineIndex);
lineIndex++;
break;
case 9: // j round finish
codeList.setSelectedIndex(lineIndex);
j++;
lineIndex = 3;
break;
case 10: // end loop
codeList.setSelectedIndex(lineIndex);
for (int r = 0; r < 10; r++) {
numbers.get(r).setColor(Color.BLACK);
}
lineIndex++;
break;
case 11: // end algorithm
codeList.setSelectedIndex(lineIndex);
completed = true;
break;
default:
break;
}
repaint();
}
}
private List<NumberRectangle> initialNumberRectangles() {
List<NumberRectangle> list = new ArrayList<NumberRectangle>();
// generate 10 random numbers
Random random = new Random();
for (int i = 1; i <= 10; i++) {
list.add(new NumberRectangle(i, 1, random.nextInt(15) + 1, Color.white));
}
return list;
}
}