0

I am trying to make a sorting visualizer using Java Swing. I tested out my code involving bubble sort, but the GUI freezes for a few seconds before displaying the final result, which I think has something to do with Thread.sleep().

I was advised to use Swing timer instead, but I am not sure how to make that work. What can I try?

My code: MainWindow.java:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainWindow window = new MainWindow();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
}
public MainWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame("Sorting Algorithm Visualizer");
    frame.setBounds(100, 100, 1280, 720);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);
    
    newVisual = new Visual_window();
    frame.add(newVisual);
    
    SortingAlgorithms allAlgorithms = new SortingAlgorithms();
    allAlgorithms.bubbleSort(newVisual, 100);
    for(int i = 0; i < 100; i++) {
        System.out.print(newVisual.getValue(i) + ", ");
    }
}

Swap method in Visual_window class which extends JPanel and contains an unsorted array:

public void swap(int index1, int index2) {
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    
    repaint();
    //This is making the GUI not work and should be replaced with Swing timer equivalent.
    try {
        Thread.sleep(1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

BubbleSort in SortingAlgorithms class:

public void bubbleSort(Visual_window array, int size) {
    for(int i = 0; i < size - 1; i++) {
        for(int j = 0; j < size - i - 1; j++) {
            if(array.getValue(j) > array.getValue(j+1)) {
                array.swap(j, j+1);
            }
        }
    }
}

Although everything works if I remove the EventQueue.invokeLater portion of the main function, but I am not sure if that is the right thing to do.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pengibaby
  • 373
  • 2
  • 11
  • 1
    Never use Thread.sleep() on a gui thread. Use a timer instead (on a different thread). – NomadMaker Sep 30 '20 at 16:17
  • 1
    1) Where is your attempt? Have you tried reading the [docs](https://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html)? 2) We don't need to see your whole Swing program. If your question is about making a delay in a Swing application, all we should need is a bare-bones Swing application, and your attempt at putting in a delay for something. You sorting and swapping algorithm, and your `paintComponent()` method, are all irrelevant. Please see the [mre] page. – Charlie Armstrong Sep 30 '20 at 16:19
  • I'll second the need for a [MRE]. You show a call to `MainWindow window = new MainWindow()` in the beginning but we have no code for this class. How are we supposed to guess what it does? Please include enough code that we can actually debug something. The full code -- a complete example that runs as is -- would be the preferable. – markspace Sep 30 '20 at 16:49
  • Sorry for not making the best post. I have added the rest of the code for MainWindow class. Hope that is okay. – Pengibaby Sep 30 '20 at 17:13
  • 1
    1) *"Sorry for not making the best post."* Who are you replying to? Tip: Add @markspace (or whoever, the `@` is important) to *notify* the person of a new comment. 2) *"Hope that is okay."* Test at least some aspects of 'OK' before posting or editing. I.E. Can you do a single copy/paste into a new project/package in an IDE, compile the code without ***any*** alteration or addition, then run it to see the problem? If not, it's **not** an MRE! One of the benefits of making an MRE is that others can easily work with the code to help solve problems. – Andrew Thompson Oct 01 '20 at 03:39
  • *I have added the rest of the code for MainWindow class.* - that is not an [mre]. We can't copy/paste/compile/test the code. The code should be in a single class to make it easy for us to do the previous steps. – camickr Oct 02 '20 at 00:34

0 Answers0