如何等待直到图像完全加载到Java中 [英] How to wait until an image is fully loaded in Java

查看:161
本文介绍了如何等待直到图像完全加载到Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java映像API假定异步加载。各种方法都将ImageObserver作为参数,一旦图像完全加载,该参数可能会被告知。

The Java image API assumes asynchronous loading. Various methods take an ImageObserver as a parameter which might get informed once the image is completely loaded.

另一方面,某些类型的图像(例如BufferedImages)不使用

On the other hand some types of images (e.g. BufferedImages) don't use the ImageObserver and will never call it.

那么代码看起来如何等到图像完全加载?

So how would code look that waits until an image is fully loaded?

我想拥有这样的方法

public void waitUntilLoaded(Image img){
    ...
}

保证图像在返回时已完全加载。

Which guarantees that the image is completely loaded when it returns.

推荐答案

一个不错的选择是 java.awt.MediaTracker 。 API文档中有一个很好的代码示例 。简而言之,您可以创建一个MediaTracker对象来跟踪组件的图像,告诉它要跟踪的图像,然后调用 tracker.waitForID(n),该对象将一直阻塞直到

A good option would be java.awt.MediaTracker. There's a good code example in the API documentation. In short, you create a MediaTracker object to track images for a component, tell it which images you want to track, and then call tracker.waitForID(n) which will block until the specified image has finished loading.

显然,您需要将其保留在事件线程之外,但是您知道这一点。 :)

Obviously, you need to keep this out of the event thread--but you knew that. :)

这篇关于如何等待直到图像完全加载到Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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