Java 中的 Graphics.drawImage() 在某些计算机上非常慢,但在其他计算机上要快得多 [英] Graphics.drawImage() in Java is EXTREMELY slow on some computers yet much faster on others

查看:45
本文介绍了Java 中的 Graphics.drawImage() 在某些计算机上非常慢,但在其他计算机上要快得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,基本上在 Java Graphics.drawImage() 在某些计算机上非常慢,而在其他计算机上则更快.这也与计算机的能力无关,一些较弱的计算机运行良好,而一些较强的计算机似乎在调用 drawImage 时卡住了.

I'm having a strange problem, basically in Java Graphics.drawImage() is extremely slow on some computers and faster on others. This isn't related to the computers power either, some weaker computers run it fine while some stronger ones seem to choke up at the drawImage call.

它可能与宽度和高度有关,也可能不相关,我定义了一个非常非常大的宽度和高度(例如 5000 x 2500).我不认为这是问题所在,除非我说它在某些计算机上以实时速度运行而在其他计算机上运行速度较慢,而且似乎与计算机的相对功率无关.

It may or may not be related to the width and height, I have a very, very large width and height defined (something like 5000 by 2500). I wouldn't think it's the issue except like I said it runs in real time speed on some computers and slower on others and doesn't seem to be tied to the computers relative power.

两台电脑的Java版本相同,都使用Vista.一个有一个 1.83ghz Core 2 Duo,带有 1gb RAM 和板载图形(运行一切正常),另一个有一个 2.53 ghz core 2 duo,带有 9600GS(最新的 nVidia 驱动程序)和 4gb 的 RAM,它在 drawImage 调用上确实很突兀.

Both computers have the same version of Java, both use Vista. One has a 1.83ghz Core 2 Duo with 1gb RAM and onboard graphics (runs everything fine), the other has a 2.53 ghz core 2 duo with a 9600GS (latest nVidia drivers) and 4gb of RAM and it literally chugs on the drawImage call.

有什么想法吗?

好的,这真的很奇怪,我正在将图像绘制到 Swing 中的窗口,现在当我调整窗口大小并使其非常小时,图像也会缩小并变小.突然间一切都运行顺利,当我将它放大到它之前的大小时,它仍然运行顺利!

edit: ok this is really wierd, I'm drawing the image to a window in Swing, now when I resize the window and make it really small the image gets scaled down too and it becomes small. Suddenly everything runs smoothly, when I scale it back up to the size it was before it's still running smoothly!

它还存在多个显示器问题,如果我使用调整大小技巧使其在一台显示器上运行得更快,然后当超过一半的窗口位于新显示器中时将其滚动到另一台显示器,它会再次开始卡顿.我必须再次将窗口大小调整为小,然后恢复到原始大小以恢复速度.

It also has multiple monitor issues, if I do the resize trick to make it run faster on one monitor then scroll it over to another monitor when more than half of the window is in the new monitor it starts chugging again. I have to resize the window again to small then back to its original size to get back the speed.

如果我在一台显示器上执行调整大小技巧,将其移到另一台显示器上,它当然会发出声音,但是如果我将其返回到我执行调整大小技巧的原始显示器上,它可以 100% 工作

If I do the resize trick on one monitor, move it over to the other it of course chugs, but if I return it back to the original monitor on which I did the resize trick it works 100%

如果我打开了两个摇摆窗口(显示相同的图像),它们都运行缓慢,但是如果我在一个窗口上执行调整大小技巧,它们都会开始平稳运行(但情况并非总是如此).

If I have two swing windows open (displaying the same image) they both run slow, but if I do the resize trick on one window they both start running smoothly (however this isn't always the case).

*当我说调整窗口大小时,我的意思是让它尽可能小到无法实际看到图像的程度.

*when I say resize the window I mean make it as small as possible to the point the image can't actually be seen.

这可能是 Java 中的错误吗?

Could this be a bug in Java maybe?

推荐答案

将图像写入屏幕的性能在很大程度上受图像存储格式的影响.如果格式与屏幕内存所需的格式相同,那么它可以非常快;如果不是,则必须进行转换,有时是逐个像素的,这非常慢.

Performance of writing an image to a screen is very much affected by the format in which the image is stored. If the format is the same as the screen memory wants then it can be very fast; if it is not then a conversion must be done, sometimes pixel by pixel, which is very slow.

如果您可以控制图像的存储方式,则应以屏幕正在寻找的格式存储它.下面是一些示例代码:

If you have any control over how the image is stored, you should store it in a format that the screen is looking for. Here is some sample code:

    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getDefaultConfiguration();
    BufferedImage buffy = config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    Graphics g = buffy.getGraphics();

如果您要多次绘制图像,即使它采用其他格式,也可能值得转换为兼容格式.

If you are going to draw the image many times it may be worth converting to a compatible format even if it came in some other format.

如果您在绘制时对其进行变换,那么绘制图像也会变慢,您描述中的调整大小"部分让我认为您可能是这样.再次,调整大小一次(当调整窗口大小时)并缓存调整大小和兼容的图像,以便可以快速重绘.

Drawing an image will also be slower if you are transforming it as you draw, which the 'resizing' part of your description makes me think you might be. Again, do the resize once (when the window is resized) and cache the resized and compatible image so that it can be redrawn quickly.

这篇关于Java 中的 Graphics.drawImage() 在某些计算机上非常慢,但在其他计算机上要快得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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