我如何调用我的绘制方法来使用每秒运行 60 次的厚方法绘制其中的所有内容。
这是 fps 计算:
int FRAMES_PER_SECOND = 60;
long maxWorkingTimePerFrame = 1000 / FRAMES_PER_SECOND; //this is optional
long lastStartTime = System.currentTimeMillis();
while(true)
{
lastStartTime = System.currentTimeMillis();
Tick();
long processingTimeForCurrentFrame = System.currentTimeMillis() - lastStartTime;
if(processingTimeForCurrentFrame < maxWorkingTimePerFrame)
{
try
{
Thread.sleep(maxWorkingTimePerFrame - processingTimeForCurrentFrame);
}
catch(Exception e)
{
System.err.println("TSEngine :: run :: " + e);
}
}
}
那么我该如何调用:
public void paint( Graphics g ) {
}
在我的 Tick 方法中?
最佳答案
要在 Tick 方法中调用 paint()
方法,您需要调用 repaint()
方法。当您希望组件调用您编写的绘制方法时,只需在循环中添加 repaint()
即可。
阅读这篇文章,它解释了绘画和重画的工作原理。 http://www.scs.ryerson.ca/~mes/courses/cps530/programs/threads/Repaint/index.html .
这里有一篇文章演示了如何调用重绘。 http://www.scs.ryerson.ca/~mes/courses/cps530/programs/threads/Repaint/RepaintApplet3.java
关于java - java中调用游戏中fps tick的paint方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24206085/