2009-12-16 42 views
1

我正在用java製作一系列圖像的動畫製作。到目前爲止,我可以動畫沒有任何問題。只有當我添加控制(開始,停止等)時纔會出現問題。當我在我的GUI中按下「開始」時,GUI不顯示動畫結束後僅顯示最後一幀的動畫。對一系列圖像進行動畫製作

我不確定是線程問題還是繪畫問題,因爲我嘗試了幾種方法,但都沒有工作。

以下是我的代碼:

public class SwingAnimation extends JPanel implements ActionListener {

protected JFrame frame; 
protected JLabel lblDisplay; 
protected JButton BtnStart, BtnStop, BtnPause; 
protected JCheckBox chkLoop; 
protected Thread th; 

public static void main(String[] args) { 
    SwingAnimation sa = new SwingAnimation(); 
} 

public SwingAnimation() { 
    frame = new JFrame("Animation"); 
    Panel panel = new Panel(); 

    lblDisplay = new JLabel(); 
    BtnStart = new JButton("Start"); 
    BtnStop = new JButton("Stop"); 
    BtnPause = new JButton("Pause"); 
    chkLoop = new JCheckBox("Loop"); 

    BtnStop.setEnabled(false); 
    BtnPause.setEnabled(false); 

    BtnStart.setActionCommand("start"); 
    BtnStop.setActionCommand("stop"); 
    BtnPause.setActionCommand("pause"); 

    panel.add(lblDisplay); 
    panel.add(BtnStart); 
    panel.add(BtnPause); 
    panel.add(BtnStop); 
    panel.add(chkLoop); 
    frame.add(panel, BorderLayout.CENTER); 
    frame.setSize(400, 400); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //set the frame in the center of the screen 
    Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); 
    int screen_x = (screensize.width - frame.getWidth())/2; 
    int screen_y = (screensize.height - frame.getHeight())/2; 
    frame.setLocation(screen_x, screen_y); 

    BtnStart.addActionListener(this); 
    BtnStop.addActionListener(this); 
    BtnPause.addActionListener(this); 
    chkLoop.addActionListener(this); 
    th = new Thread(); 
    //ImageAnimator(); 
} 

public void ImageAnimator() { 
    try { 
     for (int i = 0; i <= 299; i++) { 
      ImageIcon images = new ImageIcon("C:\\Users\\Desktop\\Images\\Snap" + i + ".jpg"); 
      lblDisplay.setIcon(images); 
      th.sleep(25); 
     } 
    } catch (InterruptedException e) { 
    } 
} 

public void actionPerformed(ActionEvent e) { 
    if ("start".equals(e.getActionCommand())) { 
     BtnStart.setEnabled(false); 
     BtnStop.setEnabled(true); 
     BtnPause.setEnabled(true); 
     lblDisplay.setVisible(true); 
     ImageAnimator(); 
    } else if ("stop".equals(e.getActionCommand())) { 
     BtnStart.setText("Start"); 
     BtnStart.setEnabled(true); 
     BtnStop.setEnabled(false); 
     BtnPause.setEnabled(false); 
     lblDisplay.setVisible(false); 
     th = null; 
    } else if ("pause".equals(e.getActionCommand())) { 
     BtnStart.setText("Resume"); 
     BtnStart.setEnabled(true); 
     BtnStop.setEnabled(true); 
     BtnPause.setEnabled(false); 
    } 
} 

}

+0

與您的問題無關,但不需要創建保存在'th'中的線程。 'sleep'方法是靜態的,應該被稱爲'Thread.sleep(25)'。 – 2010-01-06 21:22:08

回答

3

快速修復:

變化ImageAnimator()這樣的:

 lblDisplay.setIcon(images); 
     lblDisplay.paintImmediately(getBounds()); 
     th.sleep(25); 

您遇到的問題是,設置圖標不會自動導致Swing在屏幕上重新繪製組件。調用paintImmediately將做到這一點。

一般建議:

動畫在揮杆使用Swing Timer正常完成。您目前注意到您的用戶界面無響應 - 一旦您開始動畫,您將無法停止它,直到它結束。這是因爲所有Swing事件都發生在單個線程上 - 事件調度線程。跑在中間的關係用了Thread.sleep環路(...)這個線程,留下它不可用於處理其它輸入(比如按下停止按鈕)

This article幫我沒法比,當我試圖瞭解擺動處理併發,並The Swing Trail有很多意見上有效地利用擺動,畫的自定義組件等

我還插演示代碼上Filthy Rich Clients,我通過這本書做什麼工作的,它是值得一試。

+0

和方法名稱應以小寫字母開頭,以便imageAnimator()... :) – willcodejavaforfood 2009-12-16 09:21:25

+0

良好的調用。我和一些前C++大師一起工作,已經習慣了隨機的大寫方法,甚至沒有註冊。 :( – MHarris 2009-12-16 09:35:22

+0

謝謝你的建議,但因爲它不斷告訴我,它無法找到象徵paintImmediately()方法沒有工作,但我會切換到擺動計時器。 – 2009-12-16 09:37:10