正确查找自定义 Java 组件的帧速率
我不确定我的帧率代码是否正确,并且我无法找到我正在寻找的确切示例。
本质上,我对 java.awt.Component 进行了子类化,并在 Paint(Graphics) 方法中调用了我的calculateFrameRate() 函数,如下所示以下。我没有在 update()
中进行任何增量绘制。这个数字看起来很高,我想知道 Component 类固有的双缓冲是否意味着调用 Paint 的次数是渲染次数的两倍?不过,我对双缓冲的东西很生疏,这可能是完全不正确的。
这是帧速率方法:
private List<Long> updateTimes = new ArrayList<Long>();
private void calculateFrameRate() {
long time = System.currentTimeMillis();
updateTimes.add(new Long(time));
// We will have the wrong framerate for the first 30 draws. No big.
float timeInSec = (time - updateTimes.get(0)) / 1000f;
currentFrameRate_ = 30f / timeInSec;
if (updateTimes.size() == 31)
updateTimes.remove(0);
}
干杯,
Hamy
I am not confident my framerate code is correct, and I have not been able to find exact examples of what I am looking for.
Essentially I have subclassed java.awt.Component
, and inside the paint(Graphics)
method I call my calculateFrameRate()
function, which is shown below. I don't do any incremental drawing in update()
. The numbers from this seem high, and I am wondering if the inherent double buffering of the Component class means that paint is being called twice as much as it is being rendered? I'm rusty on the double-buffer stuff though, that may be totally incorrect.
Here is the frame rate method:
private List<Long> updateTimes = new ArrayList<Long>();
private void calculateFrameRate() {
long time = System.currentTimeMillis();
updateTimes.add(new Long(time));
// We will have the wrong framerate for the first 30 draws. No big.
float timeInSec = (time - updateTimes.get(0)) / 1000f;
currentFrameRate_ = 30f / timeInSec;
if (updateTimes.size() == 31)
updateTimes.remove(0);
}
Cheers,
Hamy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为替代方案,您可以查看
System.nanoTime()
。此示例计算由FRAMES
定义的先前帧数的平均值。As an alternative, you could look at
System.nanoTime()
. This example calculates an average over the preceding number of frames defined byFRAMES
.