Java 什么是 ImageObserver?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19212990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:06:23  来源:igfitidea点击:

What is an ImageObserver?

javaawtbufferedimageimageobserver

提问by Troubleshoot

When you draw an image it requires an image observer. From what I understand so far a BufferedImage is an image observer. But my question is, what defines an image observer and what does it do? I'm quite confused.

当您绘制图像时,它需要一个图像观察者。据我目前所了解的 BufferedImage 是一个图像观察者。但我的问题是,图像观察者的定义是什么,它有什么作用?我很困惑。

采纳答案by Mateusz

First of all, ImageObserveris an interface. According to docs:

首先,ImageObserver是一个界面。根据文档

An asynchronous update interface for receiving notifications about Image information as the Image is constructed.

一个异步更新接口,用于在构建 Image 时接收有关 Image 信息的通知。

In other words, it's an object-oriented way to use Images which can be modified before fully created. Method imageUpdate(Image img, int infoflags, int x, int y, int width, int height)is called any time the image is modified. It returns trueif it wants to be notified about further changes and falseotherwise. This method can be used to force size, resolution, colours etc. It also gives you some control of the errors (ERRORflag). For more info see this.

换句话说,它是一种使用图像的面向对象的方式,可以在完全创建之前进行修改。imageUpdate(Image img, int infoflags, int x, int y, int width, int height)任何时候修改图像时都会调用方法。它返回true,如果要通知有关进一步的变化和false其他。此方法可用于强制大小、分辨率、颜色等。它还可以让您控制错误(ERROR标志)。有关更多信息,请参阅

The observer may also process important information about the image - for example if we're drawing an image on the screen and change it to a bigger one before the rendering is complete, there has to be a way to inform whatever we're drawing on that the dimension has changed (allocate more space) and that it has to deal with the changes. The fact that ImageObserveris asynchronousis extremely important in that case.

观察者还可以处理有关图像的重要信息——例如,如果我们在屏幕上绘制图像并在渲染完成之前将其更改为更大的图像,则必须有一种方法来通知我们正在绘制的任何内容维度已经改变(分配更多空间)并且它必须处理这些变化。这事实上ImageObserver异步的是在这种情况下非常重要。

回答by cMinor

ImageObserveris an interface that has methods for handling notification of state of image loading. It can use this for redisplay as needed. JFrameand Appletboth implement ImageObserverinterface.

ImageObserver是一个接口,具有处理图像加载状态通知的方法。它可以根据需要使用它重新显示。 JFrame并且Applet都实现了ImageObserver接口。

To keep users informed regarding the loading of animage

为了让用户关于加载通知图像

  • ImageObserverinterface – Enables the monitoring of the loading process so that users can be informed and the image can be used asap once it is loaded.

  • Loading an image asynchronously – how to know when the image is ready.

    • An image is ready – getImage()method returns, long before anything is known about the image.

      imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
      
  • Note: java.awt.Componentimplements ImageObserver, all the subclasses do as well!

  • g.drawImage(imge, 0,0, this)-- this refers to the ImageObserverinstance.

  • imageUpdate()– Called by the ImageObserverwhenever necessary. You do not call it explicitly!

    • If the image is complete, returns false.
    • If the image is not complete and needs to be updated, returns true.
  • ImageObserver.ALLBITS = 32

  • Various constants are combined to form the infoflagsargument, which indicates whether all information is available or not.

    info flag table

  • ImageObserverinterface – 启用加载过程的监控,以便用户可以在加载后立即通知用户并可以尽快使用图像。

  • 异步加载图像——如何知道图像何时准备就绪。

    • 图像已准备就绪 -getImage()方法返回,早在有关图像的任何信息之前。

      imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
      
  • 注意:java.awt.Componentimplements ImageObserver,所有子类也一样!

  • g.drawImage(imge, 0,0, this)——这是指ImageObserver实例。

  • imageUpdate()ImageObserver必要时由 调用。你没有明确地调用它!

    • 如果图像完整,则返回false
    • 如果图像不完整需要更新,则返回true
  • ImageObserver.ALLBITS = 32

  • 各种常量组合在一起形成infoflags参数,该参数指示是否所有信息都可用。

    信息标志表

Take a look at thisand Oreilly's explanation.

看看这个Oreilly 的解释