2014-02-15 57 views
0

我不斷有這個問題與我所有最近的動畫項目。每次我運行我的動畫時,它們看起來都不會完全可見且完整,但它們閃爍並閃爍,類似於未完全擰入的燈泡(我知道,奇怪的比較,但我想不出還有什麼類似)。我覺得它必須與我的repaint()放置有關。但我現在還不確定。在我之前製作的一個動畫中,問題在於我的「專用BufferedImage offScr」變量設置不正確,但查看其他類似於我正在使用的程序,我不明白爲什麼該變量是必需的。感謝所有的幫助人員,我對編程詞彙方面的知識缺乏表示歉意。動畫閃動

這是到目前爲止我的程序:

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 

public class DoveAnimator extends JFrame implements ActionListener { 
    private int DELAY = 40; // the delay for the animation 
    private final int WIDTH = 400; // the window width 
    private final int HEIGHT = 180; // the window height 




    private final int IMAGEAMT = 8; 
    private Image [] doveLeft = new Image[IMAGEAMT]; 
    private Image [] doveRight = new Image[IMAGEAMT]; 
    private int doveIndex = 0; 
    private boolean isRight = true; 


    private JPanel dovePanel; 
    private Image dove; 

    private JButton slowerButton = new JButton ("Slower"); 
    private JButton fasterButton = new JButton ("Faster"); 
    private JButton reverseButton = new JButton ("Reverse"); 
    private JButton pauseResumeButton = new JButton (" pause "); 
    private Timer timer; 
    private int clicks = 2; 

    private boolean pause = false; 

    /** The constructor */ 
    public DoveAnimator() { 
     MediaTracker track = new MediaTracker(this); 
     for (int i = 0; i < IMAGEAMT; ++i) { 
      doveLeft[i] = new ImageIcon("doves/ldove" + (i+1) + ".gif").getImage(); 
      doveRight[i] = new ImageIcon("doves/rdove" + (i+1) + ".gif").getImage(); 
      track.addImage(doveLeft[i],0); 
      track.addImage(doveRight[i],0); 
     } 
     // dove = doveRight[0]; 
     //track.addImage(bkgImage,0); 
     // track.addImage(dove,0); 

     try { 
      track.waitForAll(); 
     } catch (InterruptedException e) { } 
     dove = doveRight[0]; 

     JPanel mainPanel = new JPanel(); 
     mainPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT)); 
     setTitle ("Dove Animator"); 

     dovePanel = new JPanel(); 
     dovePanel.setPreferredSize(new Dimension(100, 125)); 
     dovePanel.setBackground(Color.WHITE); 

     mainPanel.add(dovePanel); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setPreferredSize(new Dimension(WIDTH, 40)); 

     // button 1 
     slowerButton.addActionListener (this); 
     buttonPanel.add (slowerButton); 

     // button 2 
     fasterButton.addActionListener (this); 
     buttonPanel.add (fasterButton); 

     // button 3 
     reverseButton.addActionListener (this); 
     buttonPanel.add (reverseButton); 

     // button 4 
     pauseResumeButton.addActionListener (this); 
     buttonPanel.add (pauseResumeButton); 

     mainPanel.add(buttonPanel); 
     add(mainPanel); 

     setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     setVisible (true); 
     pack(); 



     timer = new Timer(DELAY,this);  // setting timer delay 
     timer.start();      // start the timer 


    } 
    public void switchDove() { 
     ++doveIndex; 
     if (doveIndex >= IMAGEAMT) 
      doveIndex = 0; 

     if (isRight) 
      dove = doveRight[doveIndex]; 


     else 
      dove = doveLeft[doveIndex]; 

     dove = (isRight) ? doveRight[doveIndex] : doveLeft[doveIndex]; 

     } 





    /** Handler for button clicks and timer events */ 
    public void actionPerformed (ActionEvent evt) { 




     if (evt.getSource() == slowerButton) 
     { 
      DELAY += 10; 

     } 
     else if (evt.getSource() == reverseButton) 
     { 
      if(evt.getSource() == reverseButton && isRight == true){ 
       isRight = false; 

      } 
      else if(evt.getSource() == reverseButton && !isRight){ 
       isRight = true; 
      } 


     } 
     else if (evt.getSource() == fasterButton) 
     { 
      DELAY -= 10; 
      if (DELAY <= 10){ 
       DELAY = 10; 
      } 


     }     
     else if (evt.getSource() == pauseResumeButton) 
     {  
      if(evt.getSource() == pauseResumeButton && !pause){ 
       pauseResumeButton.setText(" Resume "); 
       timer.stop(); 
       pause = true; 

      } 
      else if(evt.getSource() == pauseResumeButton && pause == true){ 
       pauseResumeButton.setText(" Pause "); 
       timer.start(); 
       pause = false; 
      } 




     }   
     else if (evt.getSource() == timer) 
     { 
      drawAnimation(); 
      switchDove(); 
      repaint(); 

     } 


    } 



    /** Draws the dove in the dovePanel */ 
    public void drawAnimation() { 
     Graphics page = dovePanel.getGraphics(); 
     page.drawImage(dove,0,0,Color.WHITE,null); 
    } 

    /** The main method */ 
    public static void main (String [] args) { 
     new DoveAnimator(); 
    } 
} 

回答

0

是的它似乎是你重繪的一個()調用。取出你的repaint()方法調用在這裏:

 else if (evt.getSource() == timer) 
     { 
     drawAnimation(); 
     switchDove(); 
     repaint(); 

這是混淆程序,因爲你已經在切換鴿子。這是我最好的猜測。我跑了它,似乎工作。乾杯!

0

另外我只是注意到,你調整你的計時器的方式會讓你沒有結果。您需要使用命令:timer.setDelay(newDelay);您還可以將參數放在括號中,如timer.setDelay(DELAY -= 10);