【发布时间】:2011-07-22 23:01:28
【问题描述】:
我有这个Thread 应该更新游戏的元素,即重绘,等待一段时间然后再做一次,问题是它不会重绘。我已经测试了所有其他组件,它们都可以工作,但是即使我在循环中调用repaint(),paint 方法也只调用一次(因为组件是绘制的)。这是我的循环代码:
Thread t = new Thread(){
public void run()
{
mouse.init();
while(true)
{
mouse.Refresh();//Adds Dirty Regions in the RepaintManager
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//What here?
}
});
}
}
};
无需查看线程或任何东西,它会循环。
这是绘制方法:
@Override
public void paint(Graphics g)
{
EndTime = System.currentTimeMillis();//For the FPS Counter
Time = EndTime - StartTime;
FPS = (byte) (1000/Time);
TotalFPS += FPS;
TotalFrame++;
JPT.AverageFPs.setText( "" + TotalFPS/TotalFrame);
JPT.CurrentFPS.setText( "" + FPS);
StartTime = System.currentTimeMillis();
g.clearRect(0,0,dim.width,dim.width);
for(int x = 0; x < Variables.Width; x++)
{
for(int y = 0; y < Variables.Height; y++)
{
if(Variables.Map[x][y] == 0)
{
g.setColor(new Color(0x613F37));
g.drawLine(x, y, x, y);
}
else if(Variables.Map[x][y] == 1)
{
g.setColor(Color.black);
g.drawLine(x, y, x, y);
}
else if(Variables.Map[x][y] == 2)
{
g.setColor(new Color(0xDEDEDE));
g.drawLine(x, y, x, y);
}
}
}
g.setColor( new Color(0.5f, 0.5f, 0.5f, 0.5f));
g.fillRect(Variables.CurrentX, Variables.CurrentY, Variables.Zoom, Variables.Zoom);
}
非常感谢。
我还想指出,我之前将这个游戏作为 Applet 制作,它的工作原理很吸引人,但现在我需要它作为应用程序。
【问题讨论】:
-
你为什么要重绘一个令人讨厌的数量?
-
@Moonbeam:如果这是一个令人讨厌的数量,我不想知道你可能会对我的一些代码说些什么。嗯.. FPS 计算不应该在绘制方法之外吗?精彩资源:goo.gl/3Wf1f
-
变量名和方法名以大写字母开头是违反Java约定的。例如,
Variable似乎是一个类而不是一个对象。 -
可能是这样,但我刚刚做到了,因为它更新了 FPS 标签,这就像“绘画”,但无论如何