-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingPanel.java
More file actions
144 lines (120 loc) · 3.64 KB
/
SortingPanel.java
File metadata and controls
144 lines (120 loc) · 3.64 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class SortingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Random random = new Random();
private int[] array = new int[20];
private int array_index;
private int compare_index;
JButton start = new JButton("Start");
JButton reset = new JButton("Reset");
private boolean isRunning;
public SortingPanel() {
this.array_index = 0;
this.compare_index = Integer.MAX_VALUE;
this.setArray();
start.setBackground(Color.CYAN);
start.setFocusPainted(false);
start.setBorderPainted(false);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
isRunning = true;
BubbleSortAnimate();
}catch(Exception exception) {
exception.printStackTrace();
}
}
});
reset.setBackground(Color.CYAN);
reset.setFocusPainted(false);
reset.setBorderPainted(false);
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setArray();
compare_index = Integer.MAX_VALUE;
array_index = 0;
isRunning = false;
repaint();
}
});
this.add(start);
this.add(reset);
}
public int[] getArray() {
return this.array;
}
public void setArray() {
for(int i = 0; i < this.array.length; i++)
{
this.array[i] = random.nextInt(510) + 40;
}
}
public void sortOnlyOneItem() {
if(array[compare_index] > array[compare_index+1])
{
int temp = array[compare_index];
array[compare_index] = array[compare_index+1];
array[compare_index+1] = temp;
}
if((compare_index+1) >= (array.length - array_index - 1))
{
array_index++;
compare_index = 0;
}
else
compare_index++;
}
public boolean isSorted() {
for(int i = 0; i <array.length - 1; i++)
{
if(array[i] > array[i+1])
{
return false;
}
}
return true;
}
public void BubbleSortAnimate() throws Exception{
compare_index = 0;
Timer timer = new Timer(200, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(isSorted())
{
compare_index = Integer.MAX_VALUE;
((Timer)e.getSource()).stop();
}else
{
if(isRunning == true)
sortOnlyOneItem();
}
repaint();
}
});
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
for(int i = 0; i <array.length; i++)
{
g.setColor(Color.CYAN);
if(i == compare_index) // || i == compare_index+1)
{
g.setColor(Color.RED);
}
else if(i == (compare_index + 1))
{
g.setColor(Color.GREEN);
}
g.drawRect(i*30, 600 - array[i], 29, array[i]);
g.fillRect(i*30, 600 - array[i], 29, array[i]);
}
}
}