【发布时间】:2012-07-31 11:54:20
【问题描述】:
我用 Java 制作了一个游戏,当我在 Eclipse 中运行它时可以正常运行。一切看起来都很棒,并且有效地完成了(至少在我想出与它有关的其他事情之前)。所以我一直试图把它放在我的网站上,但是每当我在浏览器中运行游戏时,我都会得到一个白屏,尽管检查 Java 控制台没有显示任何错误。我已经设法将问题缩小到屏幕的绘画。我有一个计时器,它运行游戏并让事情发生。最后,它调用 repaint() 方法。在 Eclipse 中,这工作正常,但在浏览器中,没有任何反应。
以下是相关代码(全部在名为 FinalProject 的主类中):
public class FinalProject extends JApplet implements ActionListener,
KeyListener, MouseListener, MouseMotionListener {
public void init(){
//...initialize program
System.out.println("game started");
}
/**
* A method called every so often by a timer to control the game world.
* Mainly calls other functions to control objects directly, but this
* is used as the only timer, which also calls repaint() at it's end.
*/
private void runGame(){
//...Run game and do important stuff
//This Draws The Screen
System.out.println("about to paint");
repaint();
}
public void paint(Graphics g){
System.out.println("painting");
//...paint screen
}
public void update(Graphics gr){
System.out.println("updating");
paint(gr);
}
}
runGame() 由计时器调用。在 Eclipse 中,输出为:
游戏开始
绘画
绘画
即将上色
绘画
即将上色
绘画
即将上色
绘画
...
在浏览器中执行此操作时(直接在我的机器上离线运行。所有浏览器也有同样的问题),控制台显示:
...(加载内容)
游戏开始
基本:小程序已初始化
基本:启动小程序
基本:已完成性能汇总
基本:小程序可见
基本:小程序启动
基本:告诉客户小程序已启动
即将上色
即将上色
即将上色
...
我现在不知道还能尝试什么。尽管我很努力,但我仍然不完全理解 repaint() 的作用,我所知道的是它最终调用了 update() 和 paint()。除了浏览器中似乎没有发生这种情况。我正在使用带有 Java Version 7 Update 5 的 Windows 7 64x。在此先感谢您的帮助。
【问题讨论】:
-
我没有任何问题。但是,我确实将您的
update方法改为调用super.update(g)