如何消除动画过程中出现的闪烁? [英] How to get rid of the flicker that appears during my animation?

查看:71
本文介绍了如何消除动画过程中出现的闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过在JApplet中编写一个小型游戏来学习Java.我的精灵动画有点问题.

I'm learning Java by making a small game in a JApplet. I got a little problem with my sprite's animation.

这是代码:

this.sprite.setBounds(0,0,20,17);

this.sprite.setIcon(this.rangerDown);
for(int i = 0; i< 16;i++)
{
    this.sprite.setBounds(this.sprite.getX(), this.sprite.getY()+1, 20, 17);
    this.sprite.update(this.sprite.getGraphics());

    try{
        Thread.currentThread().sleep(100);
    }catch(InterruptedException e){
}

}

它在动画过程中留下了一些闪烁.动画结束后,闪烁就消失了,但这有点丑陋……我想我错过了一些步骤.我之所以使用这种方法,是因为它目前可以提供更好的结果,但是我希望尽可能不使用AWT,而应该使用Swing.

It left some flicker during the animation. Once the animation end, the flicker disappears, but it's kind of ugly... I guess there is some step I missed. I use this method because it gives the better result for now, but I would like to stay without AWT if possible, using Swing instead.

任何想法如何摆脱闪烁?

Any ideas how to get rid of the flicker?

感谢阅读.

截图(无法发布图片,对不起).

Screenshoot (Can't post images, sorry).

推荐答案

这不是阴影.它是你的精灵的边界.它恰好是黑色,并显示为阴影.如果您改变精灵的数量(让我们说50个像素,而不仅仅是1个像素),您会明白我的意思.

This is not a shadow. Its the border of your sprite. It just happens to be black and appears as a shadow. If you change the amount you shift your sprite (lets say by 50 pixels, not just 1) you will see what i mean.

要修复此问题,您需要在每次更新精灵的位置时绘制背景.尽管这可能会产生闪烁.

To fix it what you need to do is to draw the background as well each time you update the location of your sprite. Although this will probably produce flickering.

正确的方法是更改​​绘制对象的方式.您需要覆盖面板的paintComponent方法,然后在每次更新精灵的位置时只需调用repaint.

The correct way to do it is to change the way you draw your objects. You need to override the paintComponent method of your panel and then simply call repaint each time you have updated the locations of your sprites.

有关基本用法,请参见此代码示例.注意:这不是使用线程编写动画的方式.我编写该脚本是为了向您展示paintComponent方法中的内容,并编写了动画Thread以向您显示您提到的阴影"已消失.永远不要在线程中有一个无休止的运行循环:)

See this code sample for basic usage. NOTE: This is NOT how you should write animation using Threads. I wrote that to show you what goes in the paintComponent method and wrote the animation Thread to show you that the "shadow" you mentioned is gone. NEVER have a non ending run loop in a thread :)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame("Test");
        MyPanel c = new MyPanel();
        f.getContentPane().add(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(350, 100);
        f.setVisible(true);
    }

}

class MyPanel extends JPanel {

    int x = 0;
    boolean toTheRight = true;

    public MyPanel() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    x = (toTheRight)?x+5:x-5;
                    if (x>300)
                        toTheRight = false;
                    if (x<0)
                        toTheRight = true;
                    repaint();
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setPaint(Color.white);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setPaint(Color.red);
        g2.fillOval(x-2, 50, 4, 4);
    }

}

这篇关于如何消除动画过程中出现的闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆