-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectionSortPanel.java
More file actions
185 lines (163 loc) · 6.14 KB
/
SelectionSortPanel.java
File metadata and controls
185 lines (163 loc) · 6.14 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
178
179
180
181
182
183
184
185
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 SelectionSortPanel 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 SelectionSortPanel() {
// 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/SelectionSort.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 minIndex = 0;
int minNumber = 0;
private class TimerAction implements ActionListener, Serializable {
public void actionPerformed(ActionEvent event) {
if (completed) {
return;
}
switch (lineIndex) {
case 1: // i loop start
codeList.setSelectedIndex(lineIndex);
lineIndex++;
break;
case 2: // i loop start
codeList.setSelectedIndex(lineIndex);
if (i != 0)
numbers.get(i - 1).setColor(Color.BLACK);
// initialize
for (k = i; k < 10; k++)
numbers.get(k).setColor(Color.WHITE);
if (i < 9) {
numbers.get(i).setColor(Color.RED); // set target color = red
j = i + 1;
lineIndex++;
} else { // end i loop
lineIndex = 10;
}
break;
case 3:
codeList.setSelectedIndex(lineIndex);
minIndex = i;
lineIndex++;
break;
case 4: // j loop start
codeList.setSelectedIndex(lineIndex);
if (j < 10) { // j < array.length
numbers.get(j).setColor(Color.BLUE);
lineIndex++;
} else { // end j loop
lineIndex = 7;
}
break;
case 5: // if judge
codeList.setSelectedIndex(lineIndex);
if (numbers.get(j).getValue() < numbers.get(minIndex).getValue()) {
lineIndex = 6;
} else {
numbers.get(j).setColor(Color.WHITE);
j++;
lineIndex = 4;
}
break;
case 6: // if (true)
for (k = i + 1; k < 10; k++)
numbers.get(k).setColor(Color.WHITE);
numbers.get(j).setColor(Color.GREEN);
codeList.setSelectedIndex(lineIndex);
minIndex = j;
j++; // for(...; ...; j++)
lineIndex = 4; // back to "for j loop" line
break;
case 7: // case 7~9 operate swap()
codeList.setSelectedIndex(lineIndex);
minNumber = numbers.get(minIndex).getValue();
lineIndex++;
break;
case 8:
codeList.setSelectedIndex(lineIndex);
numbers.get(minIndex).setValue(numbers.get(i).getValue());
lineIndex++;
break;
case 9: // i round finish
codeList.setSelectedIndex(lineIndex);
numbers.get(i).setValue(minNumber);
i++;
first = true;
lineIndex = 2;
break;
case 10: // end loop
codeList.setSelectedIndex(lineIndex);
numbers.get(9).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;
}
}