MediaTracker - 如何使用它,有什么好处,或者是否有替代方法?

MediaTracker - how to use it, what are the benefits, or is there an alterative?(MediaTracker - 如何使用它,有什么好处,或者是否有替代方法?)

本文介绍了MediaTracker - 如何使用它,有什么好处,或者是否有替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码库中,我们继承了 的用法MediaTracker 总是在每个代码块中本地完成.

In the codebase we inherited the usage of MediaTracker was always done locally in each code block.

new MediaTracker(new Canvas());
mediatracker.addImage(i, 1);
try {
    mediatracker.waitForAll();
} catch (InterruptedException e) { }
mediatracker.removeImage(i);

决定这是低效的,我最终将其替换为静态实例和方法:

Deciding this was inefficient, I eventually replaced it with a static instance and method:

final static protected MediaTracker mediatracker = new MediaTracker(new Canvas());

static protected void checkImageIsReady(Image i) {
    mediatracker.addImage(i, 1);
    try {
        mediatracker.waitForAll();
    } catch (InterruptedException e) { }
    mediatracker.removeImage(i);
}

到目前为止,没有任何不良影响.

Thus far there have been no ill effects.

还有另一种可能的方法 - 将 MediaTracker 附加到每个组件(通常是 Frame 或 JFrame),这强烈暗示了 构造函数文档.

There is another possible approach - to attach the MediaTracker to each component (usually a Frame or JFrame) which is strongly implied as the approach to take by the constructor documentation.

所以我有两个问题:

  1. 如何以及为什么要使用 MediaTracker?

  1. How and why to use MediaTracker?

有什么选择?

推荐答案

MediaTracker 在 1995 年很有用.当时 java 的主要 GUI 使用是 Applet,Applet 通常会通过网络缓慢加载图像.

MediaTracker was useful in 1995. Back then the primary GUI use of java was Applets, and Applets would usually load images slowly over the network.

为了让 Applet 编写者更容易,java 为我们提供了一个不错的 MediaTracker api,它可以在后台下载图像,跟踪完成时间,甚至在图像完成时发出通知 部分加载.MediaTracker API 意味着 Applet 编写者不必在图像下载缓慢时阻止应用程序,也不必编写复杂的线程代码来在后台线程中加载图像.

To make it easier for Applet writers, java gave us a nice MediaTracker api which would download images in the background, tracking when they were done, and even give notifications when images were partially loaded. The MediaTracker API meant Applet writers didn't have to block the application while images slowly downloaded, and didn't have to write complicated threading code to load images in background threads.

现在您通常可以同步加载图像,最好使用 ImageIO.对于从本地文件系统加载图像的常见情况尤其如此.

These days you can typically load images synchronously, and it is best to use ImageIO. This is especially true for the common case where images are loaded from the local file system.

这篇关于MediaTracker - 如何使用它,有什么好处,或者是否有替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:MediaTracker - 如何使用它,有什么好处,或者是否有替代方法?

基础教程推荐