How do I set up a JFrame with a refresh rate?(如何设置具有刷新率的 JFrame?)
问题描述
我有一个 2d 游戏,除了图形之外,整个结构都是完全编码的.我只是更新一个 JFrame 的单个组件,它是一个包含 50 个图像的图形.每一帧图像都位于不同的位置,因此需要刷新率.
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() 方法,所以我真正需要做的就是每 40 毫秒重新绘制一次组件(同样,它只是一个图形).
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何设置具有刷新率的 JFrame?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
