java 如何设置具有刷新率的 JFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6260744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set up a JFrame with a refresh rate?
提问by Will
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.
我有一个 2d 游戏,除了图形之外,整个结构都是完全编码的。我只是更新 JFrame 的单个组件,它是一个包含 50 个图像的图形。每帧图像都在不同的位置,因此需要刷新率。
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.
为了绘制,我已经覆盖了paintComponent() 方法,所以我真正需要做的就是每40 毫秒重新绘制一次组件(同样,它只是一个Graphic)。
How do you set up a JFrame with 25FPS?
你如何设置一个 25FPS 的 JFrame?
回答by Snicolas
It's a bad idea to refresh a JFrame. It's one of the few heavy components in Swing. The best way is to
刷新 JFrame 是个坏主意。它是 Swing 中为数不多的重要组件之一。最好的方法是
- identify the Panel that contains your animation
- create a class that extends JPanel . i.e. GamePane extends JPanel
- override paintComponent (no s)
- 识别包含您的动画的面板
- 创建一个扩展 JPanel 的类。即 GamePane 扩展了 JPanel
- 覆盖paintComponent(无)
Your title is misleading, you did the right thing.
你的标题有误导性,你做对了。
- in your JFrame create a runner class
- 在您的 JFrame 中创建一个跑步者类
/** Thread running the animation. */
private class Runner extends Thread
{
/** Wether or not thread should stop. */
private boolean shouldStop = false;
/** Pause or resume. */
private boolean pause = false;
/** Invoke to stop animation. */
public void shouldStop() {
this.shouldStop = true;
}//met
/** Invoke to pause. */
public void pauseGame() { pause = true; }//met
/** Invoke to resume. */
public void resumeGame() { pause = false; }//met
/** Main runner method : sleeps and invokes engine.play().
* @see Engine#play */
public void run()
{
while( !shouldStop )
{
try {
Thread.sleep( 6 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//catch
if( !pause )
{
engine.play();
gamePane.repaint();
}//if
}//while
}//met
/** Wether or not we are paused. */
public boolean isPaused() {
return pause;
}//met
}//class Runner (inner)
Your animation will be much smoother. And use MVC and events to refresh other parts of UI.
你的动画会更流畅。并使用 MVC 和事件来刷新 UI 的其他部分。
Regards, Stéphane
问候, 斯蒂芬
回答by trashgod
This AnimationTest
uses javax.swing.Timer
. A period of 40 ms would give a rate of 25 Hz.
这AnimationTest
使用javax.swing.Timer
. 40 ms 的周期将提供 25 Hz 的速率。
private final Timer timer = new Timer(40, this);
Addendum: The timer's ActionListener
responds by invoking repaint()
, which subsequently calls your overridden paintComponent()
method
附录:计时器ActionListener
通过调用响应repaint()
,随后调用您的重写paintComponent()
方法
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
}