Java swing sort visualization

Andrea Goldoni :

i'm new in java programming and i'm trying to make a sort visualization.
How you can see top the window (in the link) there is a JTextArea, here i can put my array and pressing enter the programm will build a matrix of buttons. The program will paint for every columns n buttons. So i can represent the numbers graphically. When bubble sort is pressed, i want to make see the animation of the bubble sort to sort the array graphically but when i do that, setBackground in animation class doesn't do anything.
I used a timer why i found in internet that swing doesn't allow to call repaint multiple times in short time.
Below i put a link of the GUI.
Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
class animation implements ActionListener{
    private JButton grid[][];
    private int i;
    private int j;
    private int N;
    animation(JButton grid[][], int N, int i, int j){
        this.grid=grid;
        this.N=N;
        this.i=i;
        this.j=j;
    }
    public void actionPerformed(ActionEvent e){
        JButton tmp[]=new JButton[N];
        for(int y=0;y<N;++y){
            tmp[y]=new JButton();
            tmp[y].setBackground(grid[y][i].getBackground());
        }
        for(int y=0;y<N;++y){
            grid[y][i].setBackground(grid[y][j].getBackground());
            grid[y][j].setBackground(tmp[y].getBackground());
        }
        return;
    }
}
class sortInterface extends JFrame implements ActionListener{
    //components declaration
    private ArrayList<Integer> arr=new ArrayList<Integer>();
    private JPanel simulation=new JPanel();
    private JButton grid[][];
    private JPanel algorithm=new JPanel();
    private JPanel frontPanel=new JPanel();
    private JLabel title=new JLabel("SORTING ALGORITHMS", JLabel.CENTER);
    private JTextField input=new JTextField();
    private JButton enter=new JButton("ENTER");
    private JButton bubble=new JButton("Bubble Sort");
    private JButton insertion=new JButton("Insertion Sort");
    private JButton selection=new JButton("Selection Sort");
    private JButton quick=new JButton("Quick Sort");
    private JButton merge=new JButton("Merge Sort");
    //here start the GUI 
    private void setFrame(){
        setLayout(new BorderLayout());
        add(frontPanel, "North");
        add(simulation, "Center");
        add(algorithm, "South");
        setSize(720, 480);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return;
    }
    private void setFrontPanel(){
        frontPanel.setLayout(new BorderLayout());
        frontPanel.add(title, "North");
        frontPanel.add(input, "Center");
        frontPanel.add(enter, "South");
        return;
    }
    private void setAlgorithm(){
        algorithm.add(bubble);
        algorithm.add(insertion);
        algorithm.add(selection);
        algorithm.add(quick);
        algorithm.add(merge);
        return;
    }
    private void setListeners(){
        enter.addActionListener(new buttonHandler(simulation, arr, input));
        bubble.addActionListener(new buttonHandler(simulation, arr, input));
        return;
    }
    //here start the computational part
    private boolean isNumber(char car){
        if(car=='1'||car=='2'||car=='3'||car=='4'||car=='5'||car=='6'||car=='7'||car=='8'||car=='9'||car=='0')
            return true;
        return false;
    }
    private int convertSubStringToInt(String read, int i){
        String tmp="";
        while(isNumber(read.charAt(i))&&i<read.length()){
            tmp=tmp+String.valueOf(read.charAt(i));  
            ++i;
        }
        return (Integer.parseInt(tmp));
    }   
    private void addElementToArray(String read){
        boolean flag=true;
        for(int i=0;i<read.length();++i){
            if(read.charAt(i)==' ')
                flag=true;
            if(isNumber(read.charAt(i))&&flag){
                arr.add(convertSubStringToInt(read, i));
                flag=false;
            }
        }
        return;
    }
    private int findMax(){
        int maxIndex=0;
        for(int i=1;i<arr.size();++i)
            if(arr.get(maxIndex)<arr.get(i))
                maxIndex=i;
        return arr.get(maxIndex);
    }
    private void setSimulation(){
        int max=findMax();
        int constMax=max;
        grid=new JButton[max][arr.size()];
        simulation.removeAll();
        simulation.revalidate();
        simulation.repaint();
        simulation.setLayout(new GridLayout(max, arr.size()));
        for(int i=0;i<constMax;++i){
            for(int j=0;j<arr.size();++j){
                grid[i][j]=new JButton();
                if(arr.get(j)>=max)
                    grid[i][j].setBackground(Color.white);
                else
                    grid[i][j].setBackground(Color.black);
                simulation.add(grid[i][j]);
            }
            --max;
        }
        simulation.validate();
        return;
    }
    sortInterface(){
        setFrame();
        setFrontPanel();
        setAlgorithm();
        setListeners();
    }
    public void actionPerformed(ActionEvent e){
        String read=e.getActionCommand();
        if(read=="ENTER"){
            arr.clear();
            addElementToArray(input.getText()+" ");
            setSimulation();
        }
        else if(read=="Bubble Sort"){
            for(int i=0;i<arr.size()-1;++i)
                for(int j=0;j<arr.size()-1-i;++j)
                    if(arr.get(j)>arr.get(j+1)){
                        int tmp=arr.get(j);
                        arr.set(j, arr.get(j+1));
                        arr.set(j+1, tmp);
                        Timer time=new Timer(1, new animation(grid, arr.size(), j, j+1));
                        time.start();
                        try{
                            Thread.sleep(200);
                        }
                        catch(Exception sleep){
                            //prova
                        }
                        time.stop();
                    }
        }
        else if(read=="Insertion Sort"){

        }
        else if(read=="Selection Sort"){

        }
        else if(read=="Quick Sort"){

        }
        else{

        }
        return;
    }
}
class projSortInterface
{
    public static void main(String argv[])
    {
        sortInterface prova=new sortInterface();
        prova.setVisible(true);
        return; 
    }
}

GUI

VGR :

As Joni said, you must not sleep in an ActionListener, because every ActionListener is called in the Event Dispatch Thread. Any significant delay will prevent Swing from painting any of your changes (and will also prevent Swing from responding to any user input).

But you can perform your logic and your sleeps in a different thread. The only thing you need to remember is that calls to Swing methods and Swing constructors may not be performed in a different thread; while running in a different thread, you must use EventQueue.invokeLater to execute those method calls.

Using a new thread, it would look something like this:

Runnable sortTask = () -> {
    try {
        for (int i=0; i < arr.size()-1; ++i) {
            for (int j=0; j < arr.size()-1-i; ++j) {
                if (arr.get(j) > arr.get(j+1)) {
                    int tmp = arr.get(j);
                    arr.set(j, arr.get(j+1));
                    arr.set(j+1, tmp);

                    int index1 = j;
                    int index2 = j+1;

                    EventQueue.invokeLater(() -> {
                        animation a =
                            new animation(grid, arr.size(), index1, index2);
                        a.actionPerformed(null);
                    });

                    Thread.sleep(200);
                }
            }
        }
    } catch (InterruptedException e) {
        System.out.println("Interrupted.  Terminating sort.");
    }
};

new Thread(sortTask, "Sort").start();

As you can see, the entire sort operation has been moved into a Runnable, which is then passed to a new Thread. The actual ‘animation’ is done in a separate small Runnable which is passed to EventQueue.invokeLater, since it contains Swing operations and is not permitted to run in any thread other than the Event Dispatch Thread.

It is possible to accomplish your sort using a Timer, but it would require rearranging your logic. You would not be able to use for loops. I think creating a Thread and retaining the use of for will be more readable and easier to work with in this case.

Some other notes:

  • Notice that the try/catch is around the entire sort. Interrupts do not happen by accident; if someone interrupts your thread, it means they are asking you to stop what you’re doing and exit as cleanly as possible. By allowing an InterruptedException to exit the loop when the program moves execution to the catch block, you are honoring this request.
  • Never catch Exception. There are a lot of unchecked exceptions which exist to expose programmer errors, like NullPointerException, IllegalArgumentException, and IndexOutOfBoundsException. You want those to terminate your program, because they expose errors that you, the programmer, need to fix. Suppressing them will not make the program work.
  • As George Z. pointed out, Strings are objects, and you should never compare objects using ==, since that doesn’t check whether they’re equal in value, it compares whether they are the same object created in the same place in the program. How do I compare strings in Java? explains this in detail. (There are a few special cases where it’s safe to compare objects with ==, but Strings definitely are not among them.)
  • The index1 and index2 variables are needed because only final variables can be passed to inner classes and lambdas. They don’t need to be declared with final (though it is allowed); they just need to be effectively final, which they are, because they are only assigned a value once.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=345766&siteId=1