Java:从Graphics中获取图像并将其转换为临时BufferedImage

发布于 2025-01-04 13:30:19 字数 967 浏览 1 评论 0原文

大家好,我正在制作一个 2D 游戏,我从 4 个预设图像中生成了随机的草地背景。我的问题是这样的,我的游戏从使用以下代码在游戏开始时创建的数组中绘制每个图像:

    public static void createBackground() {
    for(int x = 0; x < 640; x+= 32) {
        for(int y = 0; y < 496; y+= 32) {
            random = new Random();
            int grassTex = random.nextInt(grassTextures.size());

            grassPos.add(grassTextures.get(grassTex));
        }
    }
}

这工作正常,但这就是问题发生的地方。我正在使用以下命令重新绘制放入该数组中的每个图像:

    public static void paintBackground(Graphics g) {
    counter = 0;
    for(int x = 0; x < 640; x+= 32) {
        for(int y = 0; y < 496; y+= 32) {
            random = new Random();

            int grassTex = random.nextInt(grassTextures.size());
            g.drawImage(grassPos.get(counter), x, y, null);
            counter++;

        }
    }

}

这会导致 fps 下降(虽然不是很多,但很明显)。无论如何,我是否可以将所有这些草图像绘制到一个 BufferedImage 中,以便只绘制一张图像?或者有更有效的方法来做到这一点吗?

谢谢。

Hey guys I am making a 2D game and I have generating a random grassy background from 4 preset images. My problem is this, my game draws each image from an array which is created on game start using this code:

    public static void createBackground() {
    for(int x = 0; x < 640; x+= 32) {
        for(int y = 0; y < 496; y+= 32) {
            random = new Random();
            int grassTex = random.nextInt(grassTextures.size());

            grassPos.add(grassTextures.get(grassTex));
        }
    }
}

That works fine, but this is where the problem occurs. I am re-drawing every image that was put into that array using this:

    public static void paintBackground(Graphics g) {
    counter = 0;
    for(int x = 0; x < 640; x+= 32) {
        for(int y = 0; y < 496; y+= 32) {
            random = new Random();

            int grassTex = random.nextInt(grassTextures.size());
            g.drawImage(grassPos.get(counter), x, y, null);
            counter++;

        }
    }

}

This causes a drop in fps (It's not a lot, but it is noticeable). Is there anyway that I can draw all those grass images into one BufferedImage so that it is only one image that is being drawn? Or is there a more efficient way of doing this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2025-01-11 13:30:19

正如您所要求的,没有办法获取“来自 Graphics 的图像”,但是创建 BufferedImage 然后将图像绘制到其中并不是很难。如果有什么不同的话,AWT 的成像模型相当复杂,您必须构建一些辅助数据结构和其他东西,但这里有一个创建 32 位 RGBA 图像的模板:

private static final ComponentColorModel colormodel =
    new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                            new int[] {8, 8, 8, 8}, true, false,
                            ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
public static BufferedImage makeimage(int w, int h) {
    WritableRaster buf = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, 4, null);
    return(new BufferedImage(colormodel, buf, false, null));
}

一旦您有了由 makeimage 创建的 BufferedImage (),只需对其调用 getGraphics() 即可随心所欲地进行绘制。

There is no way to take "the image from Graphics", as you ask, but it is not very hard to just create BufferedImage and then draw the image into it, instead. If anything, AWT's imaging model is quite complex, and you'll have to construct a couple of auxiliary data structures and stuff, but here's a template which creates 32-bit RGBA images:

private static final ComponentColorModel colormodel =
    new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                            new int[] {8, 8, 8, 8}, true, false,
                            ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
public static BufferedImage makeimage(int w, int h) {
    WritableRaster buf = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, 4, null);
    return(new BufferedImage(colormodel, buf, false, null));
}

Once you have a BufferedImage as created by makeimage(), just call getGraphics() on it and paint to your heart's content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文