MediaTracker - how to use it, what are the benefits, or is there an alterative?(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.
所以我有两个问题:
如何以及为什么要使用 MediaTracker?
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 - 如何使用它,有什么好处,或者是否有替代方法?
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01