简体   繁体   中英

Repaint() doesn't work when called from new thread

I want to do a sorting animation. I've got a JFrame with GridLayout(1,3) because I have 3 sorting alghorithms. In each cell I have a JPanel with BorderLayout() . In the center of the JPanel I have the numbers and for each number a stick with different widths(I placed them horizontally). For each type of sorting I need a thread to call them. So I created class SortThread which does my sorting (it extends JThread ). Each pair of numbers and sticks are painted on the center panel with class StickNumber which extends JLabel . The problem is when I start the thread. The repaint() method doesn't seem to work.

Here is class SortThread :

public class SortThread extends Thread{
private JPanel sortPanel;
private StickNumber[] listOfNumbers;

public SortThread(JPanel sortPanel)
{
    this.sortPanel = sortPanel;
    Component[] components = sortPanel.getComponents();
    listOfNumbers = new StickNumber[components.length];
    for(int i = 0; i<components.length; i++)
        listOfNumbers[i] = (StickNumber) components[i];
}

public SortThread(){}

public void bubbleSort()
{
    boolean swapped = true;
    int j = 0;
    StickNumber tmp;

    while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < listOfNumbers.length - j; i++) {                                       
            if (listOfNumbers[i].getValue() > listOfNumbers[i+1].getValue()) {                          
                tmp = listOfNumbers[i];
                listOfNumbers[i]=listOfNumbers[i+1];
                listOfNumbers[i+1]=tmp;
                swapped = true;
                sortPanel.validate();
                sortPanel.repaint();
            }
        }                
    }
}
public void run(){
    bubbleSort();
}

Here is the listener for my button which starts the thread:

ActionListener sortListener = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            if(sortFlag == false)
            {
                sortFlag = true;
                SortThread thread1= new SortThread(centerPanel1);
                thread1.start();
                appFrame.validate();
                appFrame.repaint();
            }

        }
    };
    sortButton.addActionListener(sortListener);

StickNumber class:

private void doDrawing(Graphics g) {  

    Graphics2D g2 = (Graphics2D) g;
    String number = Integer.toString(value);
    if(dimension!=0)
        setText(number);
    g2.setStroke(new BasicStroke(5));
    g2.draw(new Line2D.Float(dimension*10, 5, height+dimension*10, 5));

}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}

Using SwingWorker, assuming your code does what it should and your problems is the EDT is blocked:

import java.awt.Component;
import java.util.List;
import javax.swing.*;

public class SortSwingWorker extends SwingWorker<Object, Object> {

    private JPanel sortPanel;
    private StickNumber[] listOfNumbers;

    public SortSwingWorker(JPanel sortPanel) {
        this.sortPanel = sortPanel;
        Component[] components = sortPanel.getComponents();
        listOfNumbers = new StickNumber[components.length];
        for (int i = 0; i < components.length; i++) {
            listOfNumbers[i] = (StickNumber) components[i];
        }
    }

    @Override
    protected Object doInBackground() throws Exception {
        boolean swapped = true;
        int j = 0;
        StickNumber tmp;

        while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < listOfNumbers.length - j; i++) {
                if (listOfNumbers[i].getValue() > listOfNumbers[i + 1].getValue()) {
                    tmp = listOfNumbers[i];
                    listOfNumbers[i] = listOfNumbers[i + 1];
                    listOfNumbers[i + 1] = tmp;
                    swapped = true;
                    publish(null);
                }
            }
        }
        return null;
    }

    @Override
    protected void process(List<Object> list) {
        sortPanel.validate();
        sortPanel.repaint();
    }

    //dummy class
    private class StickNumber extends Component {

        public Integer getValue() {
            return null;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM