Animation 动画闪烁

Animation 动画闪烁,animation,graphics,Animation,Graphics,我最近所有的动画项目都有这个问题。每次我运行我的动画,他们似乎从来没有完全可见和充分,而是他们闪烁和闪光类似的灯泡,没有完全拧入(我知道,奇怪的比较,但我想不出它还有什么相似之处)。我觉得这一定与我重新喷漆的位置有关();但我现在还不确定。在我之前制作的动画中,问题是我的“private BufferedImage offScr”变量设置不正确,但是查看与我现在正在处理的程序类似的其他程序时,我不明白为什么需要该变量。感谢大家的帮助,我为自己缺乏编程词汇而道歉 以下是我目前的计划: import

我最近所有的动画项目都有这个问题。每次我运行我的动画,他们似乎从来没有完全可见和充分,而是他们闪烁和闪光类似的灯泡,没有完全拧入(我知道,奇怪的比较,但我想不出它还有什么相似之处)。我觉得这一定与我重新喷漆的位置有关();但我现在还不确定。在我之前制作的动画中,问题是我的“private 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();
    }
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.image.buffereImage;
公共类Animator扩展JFrame实现ActionListener{
private int DELAY=40;//动画的延迟
private final int WIDTH=400;//窗口宽度
private final int HEIGHT=180;//窗口高度
私人最终int IMAGEAMT=8;
私有图像[]左=新图像[IMAGEAMT];
私有图像[]右=新图像[IMAGEAMT];
私有指数=0;
私有布尔值isRight=true;
私人杰帕内尔小组;
私人形象鸽;
私有JButton slowerButton=新JButton(“较慢”);
私有JButton fasterButton=新JButton(“更快”);
私有JButton reverseButton=新JButton(“反向”);
私有JButton pauseResumeButton=新JButton(“暂停”);
私人定时器;
私有int=2;
私有布尔暂停=false;
/**构造器*/
公共动画师(){
MediaTracker track=新的MediaTracker(此);
对于(int i=0;i=IMAGEAMT)
指数=0;
如果(isRight)
鸽子=鸽子右[鸽子指数];
其他的
鸽子=鸽子左[鸽子指数];
鸽子=(isRight)?鸽子右[鸽子索引]:鸽子左[鸽子索引];
}
/**按钮单击和计时器事件的处理程序*/
已执行的公共无效操作(操作事件evt){
if(evt.getSource()==slowerButton)
{
延迟+=10;
}
else if(evt.getSource()==反向按钮)
{
if(evt.getSource()==reverseButton&&isRight==true){
isRight=false;
}
else if(evt.getSource()==reverseButton&&!isRight){
isRight=正确;
}
}
else if(evt.getSource()==fasterButton)
{
延迟-=10;

如果(DELAYyes),这似乎是您的repaint()调用之一。请在此处执行repaint()方法调用:

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

这让程序很混乱,因为你已经在切换鸽子了。这是我最好的猜测。我运行了它,但它似乎起了作用。干杯!

另外,我刚刚注意到你调整计时器的方式不会给你带来任何结果。你需要使用命令:
timer.setDelay(newDelay)
您还可以在括号中放入参数,如
timer.setDelay(DELAY-=10);