java swing actionperformed()跳过可视化

carvr3hs  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(171)

我正在使用java和swing开发我的第一个排序可视化工具。如果我硬编码选择排序,可视化工具工作正常。当我将actionperformed()方法添加到“sort”jbutton时,事情就开始出错了。程序现在等待排序完成后再重新绘制。有办法吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SortingAlgorithmVisualizer implements ActionListener {

private static final int WINDOW_WIDTH = 1200;
private static final int WINDOW_HEIGHT = 600;
private static final String[] ALGORITHM_NAMES = { "Selection Sort", "Insertion Sort", "Quick Sort", "Merge Sort" };

// JFrame to create GUI window
private JFrame window;
private Sort array;
private JButton sortButton;
private JComboBox<String> algorithmMenu;

// Constructor
public SortingAlgorithmVisualizer() {

    // Construct the JFrame and add components
    addComponents();

    // Set Basic JFrame Settings
    frameSetup();

    // Unsort the array
    array.unsort();
}

// Construct the JFrame and add components
public void addComponents() {
    window = new JFrame("Sorting Algorith Visualiser");
    array = new Sort();
    sortButton = new JButton("Sort");
    sortButton.addActionListener(this);
    algorithmMenu = new JComboBox<>(ALGORITHM_NAMES);
    // algorithmMenu.addActionListener(this);

    window.add(algorithmMenu, BorderLayout.PAGE_START);
    window.add(array, BorderLayout.CENTER);
    window.add(sortButton, BorderLayout.SOUTH);
}

// Set Basic JFrame Settings
public void frameSetup() {
    window.setLocationRelativeTo(null);
    window.setResizable(false);
    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == sortButton) {
        array.selectionSort();
    }
}

public static void main(String[] args) {
    SortingAlgorithmVisualizer vis = new SortingAlgorithmVisualizer();
}

}

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Sort extends JPanel {

private static final long serialVersionUID = 1L;
private static final int ARRAY_SIZE = 25;
private static final int WINDOW_WIDTH = 1200;
private static final int WINDOW_HEIGHT = 600;
private static int barWidth = WINDOW_WIDTH / ARRAY_SIZE;
private int[] elementColor;

// Array to sort
private int[] array;

// Contructor
public Sort() {

    // DO SOMETHING WITH THIS!!!!
    array = new int[ARRAY_SIZE];
    elementColor = new int[ARRAY_SIZE];

    // Fill Array
    for (int i = 0; i < ARRAY_SIZE; i++) {
        array[i] = i + 1;
        elementColor[i] = 0;
    }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);

    for (int i = 0; i < ARRAY_SIZE; i++) {
        int height = (array[i] * 20 + 100);
        int x = i + (barWidth - 1) * i;
        int y = WINDOW_HEIGHT - height;
        g.fillRect(x, y, barWidth, height);
    }
}

public void unsort() {
    int shuffles = 100;
    for (int i = 0; i < shuffles; i++) {
        int rand1 = (int) (Math.random() * ARRAY_SIZE);
        int rand2 = (int) (Math.random() * ARRAY_SIZE);

        int temp = array[rand1];
        array[rand1] = array[rand2];
        array[rand2] = temp;
    }
}

public void selectionSort() {

    int n = ARRAY_SIZE;

    // Loop thought unsorted array
    for (int i = 0; i < n - 1; i++) {

        // Set Current Min
        int minIndex = i;

        for (int j = i + 1; j < n; j++) {
            if (array[j] < array[minIndex])
                minIndex = j;
        }

        // Swap the min element with the first element
        int temp = array[minIndex];
        array[minIndex] = array[i];
        array[i] = temp;

        // Repaints the Array after every swap
        repaint();
        wait(1000);
    }
}

public void insertionSort() {

}

public void quickSort() {

}

public void mergeSort() {

}

// Wait fuction for visualisation delay
public static void wait(int ms) {
    try {
        Thread.sleep(ms);
    }

    catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题