Java Graphics.drawImage() 是如何工作的,ImageObserver 的作用是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1684422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Java Graphics.drawImage() work and what is the role of ImageObserver
提问by peter.murray.rust
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.
Java 的 drawImage() 应该如何使用?我不觉得 JDK 文档很受欢迎。例如,所有的 drawImage 签名都需要一个 ImageObserver,但是这方面的文档对新用户不是很有帮助。
采纳答案by Kevin Montrose
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.
你可以逃脱Graphics.drawImage(img, x, y, null)
[或类似]。该ImageObserver
参数是一个回调,通知您绘制操作的进度;并且只有在异步获取 Image 参数时才有用。
To be clearer, if you call drawImage
with an incompletely loaded Image it will:
更清楚地说,如果您drawImage
使用未完全加载的图像调用,它将:
- 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
- 返回 false(立即)
- 尽可能多地绘制
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
.
基本上,如果您正在处理 in memory Image
(从文件系统加载或由您的程序构建),请不要担心ImageObserver
参数。如果您Image
通过网络加载s 并且没有明确等待它们加载,则需要使用 anImageObserver
来确保“完全”绘制一个Image
.
回答by Hyman
Actually I used drawImage()
many times always with ImageObserver
parameter set to null
. Ok that doesn't mean it's useless, but I did whatever I needed without knowing its use..
实际上我使用了drawImage()
很多次总是将ImageObserver
参数设置为null
. 好吧,这并不意味着它没用,但我做了我需要的任何事情而不知道它的用途..
回答by Laurence Gonsalves
Image
objects aren't necessarily completely loaded. If Graphics.drawImage
is invoked on an incomplete image it will draw as much of the image as it can, and then alert the ImageObserver
(by calling imageUpdate
) when more of the image is loaded.
Image
对象不一定完全加载。如果Graphics.drawImage
在不完整的图像上调用它,它将尽可能多地绘制图像,然后在加载更多图像时提醒ImageObserver
(通过调用imageUpdate
)。
The ImageObserver
can be null, in which case you won't get any notification. This is common if the images are known to be loaded, or if there's already another mechanism doing repaints.
该ImageObserver
可以为null,在这种情况下,你不会得到任何通知。如果已知图像已加载,或者已经有另一种机制进行重绘,这很常见。
Note that Component
implements ImageObserver
, and its imageUpdate
method will cause a repaint
on the affected area.
注意Component
implements ImageObserver
,它的imageUpdate
方法会repaint
在受影响的区域造成a 。
回答by Duncan McGregor
As others have intimated, this API was conceived when it was assumed that the images that would be rendered would be being loaded over the network. When you ask the toolkit to load an image, the assumption is that it is just a shell, and that the bytes required to know its size and pixels are still crawling down the wire.
正如其他人所暗示的那样,这个 API 是在假设要渲染的图像将通过网络加载时构思出来的。当您要求工具包加载图像时,假设它只是一个外壳,并且知道其大小和像素所需的字节仍在网络上爬行。
In this case drawImage may render nothing when it is first called. As the size and pixels become available, the ImageObserver is notified. In the case of Component implements ImageObserver
, its behaviour is to repaint when the data is available.
在这种情况下,drawImage 在第一次调用时可能不会渲染任何内容。当尺寸和像素可用时,会通知 ImageObserver。在 的情况下Component implements ImageObserver
,它的行为是在数据可用时重新绘制。