3

可能重复:
如何使用 Swing 制作动画?

在摇摆中,我对数组进行排序并希望对其进行动画处理。每次我在循环中更新数组时,我都有另一个用于暂停效果的 while 循环,当我调用实际上没有调用的 repaint() 时。为什么它不起作用?线程.sleep(1000); 冻结应用程序并且不恢复。

    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);  
    }
}
4

1 回答 1

7

不要Thread.sleep用于 Swing,因为它会阻塞 UI。使用 ajavax.swing.Timer代替:每次计时器触发动作侦听器时,您都会更新数组并安排重绘。这将为您提供动画效果。

也可以看看

于 2012-06-15T14:56:22.700 回答