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

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

问题描述

我遇到了一个奇怪的问题,基本上使用Java Graphics.drawImage()在某些计算机上速度非常慢,在其他计算机上速度更快。这与计算机功能无关,一些较弱的计算机运行正常,而一些较强的计算机似乎在drawImage调用中窒息。



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

两台电脑都有相同版本的Java,都使用Vista。其中一款配备1.83GHz Core 2 Duo,1Gb RAM和板载显卡(运行一切正常),另一款则拥有2.53 ghz核心2双核心,配备9600GS(最新nVidia驱动程序)和4GB内存,它实际上是在drawImage调用中突围而出。



有什么想法?

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



它也有多个监视器问题,如果我执行调整大小的技巧使其在一台显示器上运行得更快,然后当窗口的一半以上位于新显示器中时,将其滚动到另一台显示器上,然后再次启动。我必须再次调整窗口大小,然后恢复到原来的大小以恢复速度。



如果我在一台显示器上进行重新调整大小的技巧,将它移到其他它当然chugs,但如果我把它返回到原来的显示器,我做了调整大小的技巧它工作100%



如果我有两个打开窗户打开(显示相同的图像),他们都运行缓慢,但如果我在一个窗口上执行调整大小技巧,他们都开始顺利运行(但情况并非总是如此)。

<当我说调整窗口的大小时,我的意思是尽可能小到图像实际上看不到的地方。



这可能是一个错误Java可能吗?

解决方案

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



如果您对图像的存储有任何控制,您应该以屏幕正在查找的格式存储它。下面是一些示例代码:

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

如果您要多次绘制图像,可能需要转换为兼容格式,即使它以其他格式出现。



如果您在绘制时对图像进行转换,绘制图像的速度也会变慢,因此描述中的调整大小部分让我思考你可能会。再次,调整大小一次(调整窗口大小时)并缓存调整大小和兼容的图像,以便可以快速重绘。


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.

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.

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.

Any ideas?

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.

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.

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天全站免登陆