简体   繁体   English

如何设置具有刷新率的 JFrame?

[英]How do I set up a JFrame with a refresh rate?

I have a 2d game which the entire structure is completely coded except the graphics.我有一个 2d 游戏,除了图形之外,整个结构都是完全编码的。 I am just updating the single component of a JFrame which is a Graphic containing 50 images.我只是在更新 JFrame 的单个组件,它是一个包含 50 个图像的图形。 Each frame the images are in a different location hence the need for a refresh rate.每一帧图像都在不同的位置,因此需要刷新率。

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 毫秒重新绘制一次组件(同样,它只是一个图形)。

How do you set up a JFrame with 25FPS?如何设置 25FPS 的 JFrame?

It's a bad idea to refresh a JFrame.刷新 JFrame 是个坏主意。 It's one of the few heavy components in Swing.它是 Swing 中为数不多的重型组件之一。 The best way is to最好的方法是

  • identify the Panel that contains your animation识别包含您的 animation 的面板
  • create a class that extends JPanel.创建一个扩展 JPanel 的 class。 ie GamePane extends JPanel即 GamePane 扩展了 JPanel
  • override paintComponent (no s)覆盖paintComponent(无)

Your title is misleading, you did the right thing.您的标题具有误导性,您做对了。

  • in your JFrame create a runner class在你的 JFrame 创建一个跑步者 class

/** 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.您的 animation 会更顺畅。 And use MVC and events to refresh other parts of UI.并使用 MVC 和事件来刷新 UI 的其他部分。

Regards, Stéphane问候, 斯蒂芬

This AnimationTest uses javax.swing.Timer .这个AnimationTest使用javax.swing.Timer A period of 40 ms would give a rate of 25 Hz. 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM