我在 Java Applet 中创建了一个简单的记忆游戏。我有一个自定义加载器问题(在特殊的小程序属性中添加为 gif - docs.oracle.com/.../special_attributes.html),它消失得太快,留下白屏几秒钟。
索引.html
(...)
<body>
<center>
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {code:'(...).Memo.class', archive:'Memo.jar', width: 1200, height: 900};
var parameters = {image: 'res/loading0.gif', boxbgcolor: 'white', boxborder: 'false', centerimage: 'true'};
deployJava.runApplet(attributes, parameters, '1.6');
</script>
<noscript>
(...)
</noscript>
<script type="text/javascript" language="JavaScript" src="http://www.oracle.com/ocom/groups/systemobject/@mktg_admin/documents/systemobject/s_code_download.js"></script>
<script type="text/javascript" language="JavaScript" src="http://www.oracle.com/ocom/groups/systemobject/@mktg_admin/documents/systemobject/s_code.js"></script>
<script type="text/javascript" language="javascript">var s_code=s.t();if(s_code)document.write(s_code)</script>
</center>
</body>
(...)
此加载程序在小程序警告消息后立即消失,但我仍然需要加载一些图像,这需要一些时间。我该如何解决?
Memo.cs - 加载图像的主类
public class Memo extends JApplet {
//...
public void init() {
//...
try {
SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { createGUI(); }});
}
catch(Exception e) {
e.printStackTrace();
}
}
private void createGUI() {
final Model model = new Model(...);
final View view = new View(model);
getContentPane().add(view, BorderLayout.CENTER);
setBackground(backgroundColor);
setPreferredSize(new Dimension(width, height));
model.setLoading(loadImages(loadingPath, format, 1));
model.setCardsImages(loadImages(cardImagePath, format, 13));
//...
model.setAppState(AppStates.PROCESS);
model.startNewGame();
view.repaint();
}
private Image[] loadImages(String path1, String path2, int count) {
Image[] imgs = new Image[count];
for(int i = 0; i < count; ++i) {
imgs[i] = getImage(getCodeBase(), path1 + i + path2);
}
return imgs;
}
}
- - - - - - - - - - 解决方案 - - - - - - - - - -
媒体跟踪器效果不佳,所以我尝试使用 JavaScript。这是我的代码:
index.html 中的新功能(头)
<script language="javascript" type="text/javascript">
function setApplet()
{
document.getElementById('loader').innerHTML = "";
}
</script>
index.html(正文)中的加载程序 div 和小程序 div
loading.gif
是一个动画加载器图标loading0.gif
是一个假的加载器图标 - 一个 1x1 的图像 - 设置为隐藏默认的 Java 加载器
小程序在准备好之前也是空白的。
<div id="loader" style="font-size: 14pt; font-weight: bold;">
<p>Loading... Please wait</p>
<img src="res/loading.gif" alt="Loading... Please wait" />
</div>
<div id="applet">
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {code:'(...).Memo.class', archive:'Memo.jar', width: 1200, height: 900};
var parameters = {image: 'res/loading0.gif', boxbgcolor: 'white', boxborder: 'false', centerimage: 'true'};
deployJava.runApplet(attributes, parameters, '1.6');
</script>
(...)
</div>
Memo.cs 中的新功能
private void runJs() throws JSException, Exception {
JSObject setApplet = null;
setApplet = (JSObject) JSObject.getWindow(this);
setApplet.call("setApplet", null);
}