Java Graphics.drawImage()如何工作以及ImageObserver的作用是什么 [英] How does Java Graphics.drawImage() work and what is the role of ImageObserver
问题描述
如何使用Java的drawImage()?我没有找到JDK文档。例如,所有drawImage签名都需要一个ImageObserver,但是这个文档对新用户来说并不是很有用。
How should Java's drawImage() be used? I do not find the JDK documentation very forthcoming. For example all drawImage signatures require an ImageObserver but the documentation for this is not very helpful for new users.
推荐答案
你可以逃脱使用 Graphics.drawImage(img,x,y,null)
[或类似]。 ImageObserver
参数是一个回调函,用于通知您绘制操作的进度;并且只有在异步获取Image参数时才真正有用。
You can get away with Graphics.drawImage(img, x, y, null)
[or similar]. The ImageObserver
parameter is a callback to inform you of the progress of the draw operation; and is really only useful if you're fetching the Image parameter asynchronously.
更清楚一点,如果你调用 drawImage
对于未完全加载的图像,它将:
To be clearer, if you call drawImage
with an incompletely loaded Image it will:
- 返回false(立即)
- 绘制尽可能多的尽可能
图像
(所有已加载的) - 并且,在将来的某个时刻,调用
ImageObserver
当有更多图像可用时
- return false (immediately)
- draw as much of the
Image
as possible (all that is loaded) - and, at some future point, call into the
ImageObserver
when more of the Image is available
基本上,如果你在内存中工作 Image
s(从文件系统加载,或由程序构建)不要担心 ImageObserver
参数。如果您在网络上加载 Image
并且未明确等待加载,则需要使用 ImageObserver
确保完全绘制图像
。
Basically, if you're working with in memory Image
s (either loaded from the file system, or constructed by your program) don't worry about the ImageObserver
parameter. If you're loading Image
s across the network and not explicitly waiting for them to load, you'll need to employ an ImageObserver
to make sure "completely" draw an Image
.
这篇关于Java Graphics.drawImage()如何工作以及ImageObserver的作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!