3

Possible Duplicate:
How to make an animation with Swing?

in swing, i sort an array and want to animate it. Each time i update array in a loop, i have another while loop for pausing effect and when i call repaint() which actually is not called. Why it doesn't work? Thread.sleep(1000); freezes application and doesn't resume.

    public class Shape extends JPanel  {

    private static final long serialVersionUID = 1L;
    public int col;
    public JButton b2 = new JButton("Sort");
    public Rectangle2D.Double table = null;
    public ArrayList<Integer> arr = null;

    public Shape(){

        initArray(); // initialize array with random number of random numbers
        table = new Rectangle2D.Double(10,10,400,400);
        add(b2); // button sort

        b2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            System.out.println("sort it!");
                            Sort(arr);
                            display(arr);
                    }
         });
    }

    public void initArray() {
        col = generateRandom(10,20);            // generate a random number of bars 10 - 20
        arr = new ArrayList<Integer>(col);

        for(int i = 0; i < col; i++){
                // for each bar generate a random height 10 - 300
                int h = generateRandom(10, 390);
                arr.add(h);
        }
    }


    public void paintComponent(Graphics g){
        System.out.println("check");
        clear(g);
        Graphics2D g2d = (Graphics2D)g;
        System.out.println(arr.size());
        for(int j = 0; j < arr.size(); j++) {
                g2d.drawRect(j*20+20,410-arr.get(j),20,arr.get(j));
        }
    }

    protected void clear(Graphics g){
        super.paintComponent(g);
    }

    /*
     * generate random number in the range from 1 to 400
     */
    private int generateRandom(int start, int end){
        return new Random().nextInt(end)+start;
    }

    public void Sort(ArrayList<Integer> arr) {
        int t = 0;

        boolean swap = true;
        do {
                swap = false;
                for(int i = 0; i < arr.size() - 1; i++) {
                        if(arr.get(i).compareTo(arr.get(i+1)) > 0) {
                                int temp = arr.get(i);
                                arr.set(i, arr.get(i+1));
                                arr.set(i+1, temp);
                                swap = true;
                                // iterate to make effect of pausing
                                while(t < 100) {display(arr);t++;}
                                t = 0;
                                // repaint array
                                repaint();
                        }
                }
        } while(swap);
    }

    static void display(ArrayList<Integer> arr) {
        for(int i = 0; i < arr.size()-1; i++)
                System.out.print(arr.get(i) + ", ");
        System.out.println(arr.get(arr.size()-1));
    }

    public static void main(String[] arg){
        WindowUtilities.openInJFrame(new Shape(), 500,500);  
    }
}
Community
  • 1
  • 1
Alex
  • 483
  • 1
  • 12
  • 23
  • Please do have a look at this [answer](http://stackoverflow.com/questions/10338163/paintcomponent-does-not-work-if-its-called-by-the-recursive-function/10352884#10352884) of mine. The starting of this answer can give you a fair judgement, as to why your repaint is not called (Though only the first repaint() in the queue is called and the rest of them are discarded). And this answer also puts light on the situation, if one wants to repaint inside a loop, then how to draw, though this approach is not used frequently, cause of some strings attached. – nIcE cOw Jun 15 '12 at 15:28

1 Answers1

7

Do not use Thread.sleep for Swing as it will block the UI. Use a javax.swing.Timer instead: each time the timer triggers the action listener, you update your array and schedule a repaint. This will provide you with an animation effect.

See also

Robin
  • 36,233
  • 5
  • 47
  • 99