java.awt.Graphics ->graphics.drawImage 太慢了,怎么了? [英] java.awt.Graphics -> graphics.drawImage is too slow, what is wrong?

查看:32
本文介绍了java.awt.Graphics ->graphics.drawImage 太慢了,怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 2D 游戏引擎.我遇到的问题(我不#不使用 opengl 之类的东西,所以我用 cpu 渲染)是,我只能通过 graphics.drawImage(); 获得 7fps;您有什么建议可以加快速度或有其他选择吗?

I'd like to program a 2D game engine. The problem i have (i don#t use opengl and stuff like that, so i render with cpu) is, that i only get 7fps through the graphics.drawImage(); Do you have any suggestions to speed this up or any alternatives?

image = new BufferedImage(gc.getWidth(), gc.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
canvas.createBufferStrategy(1);
bs = canvas.getBufferStrategy();
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null);

我的渲染器应该简单地将框架(宽 320 高 240 像素)着色为青色,他确实这样做了,但最多只能达到 8fps.渲染器:

My renderer should simply color the frame (width 320 height 240 pixels) cyan, and he does, but only at a maximum of 8fps. Renderer:

private int width, height;
private byte[] pixels;

public Renderer(GameContainer gc){
    width = gc.getWidth();
    height = gc.getHeight();
    pixels = ((DataBufferByte)gc.getWindow().getImage().getRaster().getDataBuffer()).getData();
}

public void clear(){
    for(int x = 0; x < width; x++){
        for(int y = 0; y < height; y++){
            int index = (x + y * width) * 4;
            pixels[index] = (byte)255;
            pixels[index+1] = (byte)255;
            pixels[index+2] = (byte)255;
            pixels[index+3] = 0;
        }
    }
}

推荐答案

我知道我参加这个派对有点晚了,但对于未来看到这个的任何人:

I know i'm a little late to this party but for anyone looking at this in the future:

不要尝试将图像作为数组处理!

Dont try to handle your images as arrays!

如果您想使用画布为整个屏幕或图像着色,请按上述方式获取 Graphics 对象并使用矩形填充您的屏幕,如下所示:

If you want to color your whole screen or an Image using a canvas, get the Graphics object as described above and use a rectangle to fill your screen like this:

g.setColor(new Color(REDVALUE,GREENVALUE,BLUEVALUE);
g.fillRect(0,0,canvas.getWidth(),canvas.getHeight());

如果您当前的类扩展了 Canvas,那么您显然可以使用:

If your current class extends Canvas then you can obviously use:

getWidth() and getHeight()

代替:

canvas.getWidth() and canvas.getHeight()

如果您在理解 Graphics 对象的工作方式方面遇到问题,您可能需要查看 首先.

If you have problems understanding how the Graphics object works you might want to take a look at this first.

如果你想做更复杂的图像处理这个可能对你有帮助.

If you want to do more complex Image manipulation this might help you.

绘制大图像不知何故(???)比绘制小图像以填充相同区域慢.

Drawing big images is somehow (???) slower then drawing small images to fill the same area.

这篇关于java.awt.Graphics ->graphics.drawImage 太慢了,怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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