-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortVisualization.java
More file actions
68 lines (56 loc) · 1.99 KB
/
BubbleSortVisualization.java
File metadata and controls
68 lines (56 loc) · 1.99 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
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class BubbleSortVisualization extends JPanel {
private int[] array;
private int delay = 40;
private Image bubbleImage;
private Image backgroundImage;
public BubbleSortVisualization(int size) {
this.array = new int[size];
generateRandomArray();
loadImage();
}
public void loadImage(){
ImageIcon bubbleIcon= new ImageIcon("sakura.png");
bubbleImage=bubbleIcon.getImage();
ImageIcon backgroundIcon= new ImageIcon("background_theme.png");
backgroundImage=backgroundIcon.getImage();
}
public void generateRandomArray() {
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(400) + 20;
}
}
public void bubbleSort() throws InterruptedException {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
repaint();
Thread.sleep(delay);
}
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImage,0,0,getWidth(),getHeight(),this);
for (int i = 0; i < array.length; i++) {
g.drawImage(bubbleImage,i * 20, getHeight() - array[i], 20, 20,this);
}
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Bubble Sort Visualization");
BubbleSortVisualization visualizer = new BubbleSortVisualization(60);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(850, 400);
frame.add(visualizer);
frame.setVisible(true);
visualizer.bubbleSort();
}
}