如何设置刷新率的JFrame? [英] How do I set up a JFrame with a refresh rate?
问题描述
我有一个2D游戏,整个结构完全编码,除了图形。我只是更新JFrame的单个组件,它是一个包含50个图像的Graphic。每一帧图像都在不同的位置,因此需要刷新率。
I have a 2d game which the entire structure is completely coded except the graphics. I am just updating the single component of a JFrame which is a Graphic containing 50 images. Each frame the images are in a different location hence the need for a refresh rate.
要绘制,我已经覆盖了paintComponent()方法,所以我真的需要do是每40ms重新绘制一个组件(再次,只是一个Graphic)。
To paint, I have overridden the paintComponent() method, so all I really need to do is repaint the component (which, again, is just a Graphic) every 40ms.
如何设置一个25FPS的JFrame?
How do you set up a JFrame with 25FPS?
推荐答案
此 AnimationTest
使用 javax.swing.Timer中
。 40 ms的周期将给出25 Hz的速率。
This AnimationTest
uses javax.swing.Timer
. A period of 40 ms would give a rate of 25 Hz.
private final Timer timer = new Timer(40, this);
附录:计时器的 ActionListener
通过调用来响应 repaint()
,随后调用被覆盖的 paintComponent()
方法
Addendum: The timer's ActionListener
responds by invoking repaint()
, which subsequently calls your overridden paintComponent()
method
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
}
这篇关于如何设置刷新率的JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!