私には困惑する問題があります。目的の fps でグラフィックを更新する必要がある JFrame を作成するこの小さなアプリケーションがあります。しかし、アプリケーションを起動すると、60 fps ではなく 120 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)
{
}
}
タイマーはプログラムを必要に応じて 60 fps に設定しますが、実際には 30 fps を描画し、各フレームを 2 回繰り返します。paintComponent() は、repaint() が呼び出されるたびに 2 回ペイントしています。一度だけペイントするように変更するにはどうすればよいですか?前もって感謝します。