重新进行循环

时间:2012-06-15 14:53:51

标签: java swing

  

可能重复:
  How to make an animation with Swing?

在摇摆中,我对数组进行排序并想要为它制作动画。每次我在循环中更新数组时,我有另一个while循环用于暂停效果,当我调用repaint()时实际上没有调用。为什么它不起作用? 了Thread.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);  
    }
}

1 个答案:

答案 0 :(得分:7)

不要将Thread.sleep用于Swing,因为它会阻止用户界面。改为使用javax.swing.Timer:每次定时器触发动作侦听器时,都会更新阵列并安排重新绘制。这将为您提供动画效果。

另见