Java2D性能问题 [英] Java2D Performance Issues

查看:150
本文介绍了Java2D性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Java2D时遇到了奇怪的现象。我知道sun.java2d.opengl VM参数可以为2D启用3D加速,但即使使用它也有一些奇怪的问题。

I'm having performance oddities with Java2D. I know of the sun.java2d.opengl VM parameter to enable 3D acceleration for 2D, but even using that has some weird issues.

以下是我运行的测试结果:

Here are results of tests I ran:

在JComponent上绘制一个带有32x32像素图块的25x18地图< br>
图像1 = .bmp格式,图像2 = .png格式

Drawing a 25x18 map with 32x32 pixel tiles on a JComponent
Image 1 = .bmp format, Image 2 = A .png format

120 FPS使用.BMP图像1

13 FPS使用.PNG图像2

120 FPS using .BMP image 1
13 FPS using .PNG image 2

12 FPS使用.BMP图像1

FPS使用.PNG图像2

12 FPS using .BMP image 1
700 FPS using .PNG image 2

如果没有加速,我假设每次使用drawImage()我都会在软件中进行某种转换,并且在.PNG的情况下大大降低了FPS。为什么,加速,结果会切换(和PNG实际上表现得更快)?!疯狂!

Without acceleration, I'm assuming some kind of transformation is taking place with every drawImage() I do in software, and is pulling down the FPS considerably in the case of .PNG. Why though, with acceleration, would the results switch (and PNG actually performs incredibly faster)?! Craziness!

.BMP图像1被转换为TYPE_INT_RGB的图像类型。 .PNG图像2被转换为TYPE_CUSTOM的图像类型。为了在有和没有opengl加速的情况下获得一致的速度,我必须创建一个图像类型为TYPE_INT_ARGB的新BufferedImage,并将Image 1或Image 2绘制到这个新图像。

.BMP Image 1 is translated to an image type of TYPE_INT_RGB. .PNG Image 2 is translated to an image type of TYPE_CUSTOM. In order to get consistent speed with and without opengl acceleration, I have to create a new BufferedImage with an image type of TYPE_INT_ARGB, and draw Image 1 or Image 2 to this new image.

以下是运行的结果:

120 FPS使用.BMP图像1

120 FPS使用.PNG图像2

120 FPS using .BMP image 1
120 FPS using .PNG image 2

700 FPS使用.BMP图像1

FPS使用.PNG图像2

700 FPS using .BMP image 1
700 FPS using .PNG image 2

我真正的问题是,我可以假设TYPE_INT_ARGB将是所有系统和平台的本机图像类型吗?我假设这个值可能不同。有没有办法让我获得原生值,以便我总能创建新的BufferedImages以获得最佳性能?

My real question is, can I assume that TYPE_INT_ARGB will be the native image type for all systems and platforms? I'm assuming this value could be different. Is there some way for me to get the native value so that I can always create new BufferedImages for maximum performance?

提前致谢...

推荐答案

我想我找到了通过从过多的谷歌搜索中研究和放置零碎的解决方案。

I think I found a solution by researching and putting bits and pieces together from too many Google searches.

在这里,评论和所有:

private BufferedImage toCompatibleImage(BufferedImage image)
{
    // obtain the current system graphical settings
    GraphicsConfiguration gfxConfig = GraphicsEnvironment.
        getLocalGraphicsEnvironment().getDefaultScreenDevice().
        getDefaultConfiguration();

    /*
     * if image is already compatible and optimized for current system 
     * settings, simply return it
     */
    if (image.getColorModel().equals(gfxConfig.getColorModel()))
        return image;

    // image is not optimized, so create a new image that is
    BufferedImage newImage = gfxConfig.createCompatibleImage(
            image.getWidth(), image.getHeight(), image.getTransparency());

    // get the graphics context of the new image to draw the old image on
    Graphics2D g2d = newImage.createGraphics();

    // actually draw the image and dispose of context no longer needed
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();

    // return the new optimized image
    return newImage; 
}

在我之前的文章中,GraphicsConfiguration保存了创建优化图像所需的信息在一个系统上。它似乎工作得很好,但我会认为Java会自动为你做这件事。显然你对Java不太满意。 :)我想我最终回答了自己的问题。哦,好吧,希望它能帮助你们中的一些人,我们已经看到过尝试将Java用于2D游戏。

In my previous post, GraphicsConfiguration was what held the information needed to create optimized images on a system. It seems to work pretty well, but I would have thought Java would automatically do this for you. Obviously you can't get too comfortable with Java. :) I guess I ended up answering my own question. Oh well, hopefully it'll help some of you I've seen trying to make use of Java for 2D games.

这篇关于Java2D性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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