动画闪烁



我最近的所有动画项目都一直遇到这个问题。每次我运行动画时,它们似乎从来没有完全可见和完整,而是闪烁和闪烁,类似于没有完全拧入的灯泡(我知道,奇怪的比较,但我想不出它还像什么)。我觉得它一定与我放置重绘()有关;但我现在不确定。在我之前制作的动画中,问题是我的"私有 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();
    }
}

是的,这似乎是您的 repaint() 调用之一。在这里取出你的 repaint() 方法调用:

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

这让程序感到困惑,因为你已经在切换鸽子了。这是我最好的猜测。我运行了它,但它似乎有效。干杯!

另外,我刚刚注意到您调整计时器的方式不会让您大喊大叫。您需要使用以下命令: timer.setDelay(newDelay);您也可以将参数放在括号中,例如timer.setDelay(DELAY -= 10);

相关内容

  • 没有找到相关文章

最新更新