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?
基础教程推荐
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
