在Java,BufferStrategy中闪烁图像? [英] Flickering images in java, BufferStrategy?

查看:224
本文介绍了在Java,BufferStrategy中闪烁图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java制作游戏,需要在游戏板上绘制单位。我将所有单位放在一个列表中并绘制该列表中的每个单位。 paint方法如下所示:

I'm making a game in java and need to paint units on a gameboard. I put all units in a list and paints every unit in that list. The paint method looks like this:

  public void paint(Graphics g) {
        super.paint(g);

        if (unitList != null) {
            Collections.sort(unitList);
            for (Unit unit : unitList) {
                Image image = unit.getImage();
                g.drawImage(
                        image,
                        (int) (playPosition.x + unit.getPosition().getX() - image
                                .getWidth(null) / 2), (int) (playPosition.y
                                + unit.getPosition().getY() - image
                                .getHeight(null) / 2), null);
            }
        }
    }

我试图制作一个BufferStrategy但它只会让问题变得更糟,猜测我做错了什么。

I have tried to make a BufferStrategy but it only makes the problem worse, guess I am doing something wrong.

谢谢

推荐答案

也许你还没有实现 BufferStrategy 正确。

通过在图像上进行屏幕外绘画尝试手动双缓冲,然后只需绘制整个所说的图像在你的常规覆盖 paint()方法中。

你会这样做:

Maybe you haven't implemented BufferStrategy correctly.
Try manual double buffering by doing offscreen painting on an Image, and then just paint the whole said image in your regular overriden paint() method.

You would do that like so:

// Double buffering objects.
Image doubleBufferImage;
Graphics doubleBufferGraphics;

/*
 * Onscreen rendering.
 */
 @Override
 public void paint(Graphics g) {
     doubleBufferImage = createImage(getWidth(), getHeight());
     doubleBufferGraphics = doubleBufferImage.getGraphics();
     paintStuff(doubleBufferGraphics);
     g.drawImage(doubleBufferImage, 0, 0, this);
 }

/*
 * Offscreen rendering.
 */
 public void paintStuff(Graphics g) {
     if (unitList != null) {
        Collections.sort(unitList);
        for (Unit unit : unitList) {
            Image image = unit.getImage();
            g.drawImage(
                    image,
                    (int) (playPosition.x + unit.getPosition().getX() - image
                            .getWidth(null) / 2), (int) (playPosition.y
                            + unit.getPosition().getY() - image
                            .getHeight(null) / 2), null);
        }
    }
 }

这篇关于在Java,BufferStrategy中闪烁图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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