动画闪烁

时间:2014-02-15 20:23:00

标签: animation graphics

我所有最近的动画项目都一直存在这个问题。每次我运行我的动画时,它们似乎永远不会完全可见和充满,而是它们闪烁和闪烁类似于未完全拧入的灯泡(我知道,奇怪的比较,但我想不出还有什么呢)类似)。我觉得它必须与我的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();
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

这令人困惑,因为你已经在转换鸽派了。这是我最好的猜测。我跑了它似乎工作了。干杯!

答案 1 :(得分:0)

另外我只是注意到你调整时间的方式会让你没有结果。您需要使用命令:timer.setDelay(newDelay);您还可以将参数放在括号中,如timer.setDelay(DELAY -= 10);