java swing 排序动态演示代码
时间: 2023-08-08 22:06:10 浏览: 181
以下是使用Java Swing实现排序算法动态演示的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class SortingAlgorithmDemo extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton generateButton, bubbleSortButton, selectionSortButton, insertionSortButton;
private JTextField arraySizeField;
private int[] arr;
public SortingAlgorithmDemo() {
setTitle("Sorting Algorithm Demo");
setBounds(100, 100, 800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel arraySizeLabel = new JLabel("Array Size:");
arraySizeLabel.setBounds(20, 20, 100, 30);
arraySizeField = new JTextField();
arraySizeField.setBounds(120, 20, 100, 30);
generateButton = new JButton("Generate");
generateButton.setBounds(250, 20, 100, 30);
generateButton.addActionListener(this);
bubbleSortButton = new JButton("Bubble Sort");
bubbleSortButton.setBounds(400, 20, 100, 30);
bubbleSortButton.addActionListener(this);
selectionSortButton = new JButton("Selection Sort");
selectionSortButton.setBounds(550, 20, 100, 30);
selectionSortButton.addActionListener(this);
insertionSortButton = new JButton("Insertion Sort");
insertionSortButton.setBounds(700, 20, 100, 30);
insertionSortButton.addActionListener(this);
panel = new JPanel();
panel.setLayout(null);
panel.add(arraySizeLabel);
panel.add(arraySizeField);
panel.add(generateButton);
panel.add(bubbleSortButton);
panel.add(selectionSortButton);
panel.add(insertionSortButton);
setContentPane(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == generateButton) {
int size = Integer.parseInt(arraySizeField.getText());
arr = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) {
arr[i] = random.nextInt(100);
}
repaint();
} else if (e.getSource() == bubbleSortButton) {
bubbleSort(arr);
} else if (e.getSource() == selectionSortButton) {
selectionSort(arr);
} else if (e.getSource() == insertionSortButton) {
insertionSort(arr);
}
}
private void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (arr != null) {
int n = arr.length;
int width = getWidth() / n;
int height = getHeight() - 100;
g.setColor(Color.BLUE);
for (int i = 0; i < n; i++) {
g.fillRect(i * width, height - arr[i] * 3, width, arr[i] * 3);
}
}
}
public static void main(String[] args) {
new SortingAlgorithmDemo();
}
}
```
这个程序演示了三种常见的排序算法:冒泡排序、选择排序和插入排序。用户可以指定数组大小并生成随机数组,然后选择一个排序算法演示排序过程。程序使用Java Swing绘制直方图来显示数组元素。通过每次交换或移动数组元素时重绘直方图,用户可以实时地看到排序算法的执行过程。
阅读全文