将程序设置为60FPS,但运行速度为120FPS?

时间:2012-05-06 19:39:26

标签: java swing frame-rate

我有一个困扰我的问题。我有这个小应用程序,它创建一个JFrame,它应该以所需的fps更新图形。但是当我启动应用程序时,它以120 fps而不是60 fps运行。如果我设置fps = 30,它以60fps运行,如果fps = 60,则以120fps运行,依此类推(测量fps我使用FRAPS)。

这是SSCCE:

import java.awt.*;
import javax.swing.*;

public class Controller
{
    public static TheFrame window;
    public static long time = 0;
    public static boolean funciona = true;
    public static int fps = 60;
    public static int x;

    public static void main(String [] args)
    {
        window = new TheFrame();
        while(funciona) {
            time = System.nanoTime();
            window.Redraw();
            time = System.nanoTime() - time;
            try {Thread.sleep( (1000/fps) - (time/1000000) );} catch (Exception e){}
        }
    }
}

class TheFrame
{
    public JFrame theFrame;
    public CanvasScreen canvas;

    public TheFrame()
    {
        canvas = new CanvasScreen();
        theFrame = new JFrame();
        theFrame.setSize(1280, 720);
        theFrame.setContentPane(canvas);
        theFrame.setUndecorated(true);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.setVisible(true);
    }

    public void Redraw()
    {
        theFrame.repaint();
    }
}

class CanvasScreen extends JComponent
{
    public void paintComponent(Graphics g)
    {
    }
}

定时器根据需要将程序设置为60fps,但它实际绘制30 fps,重复每帧两次。每次调用repaint()时,paintComponent()都会绘制两次。如何将其更改为仅涂漆一次?提前谢谢。

0 个答案:

没有答案