gpt4 book ai didi

java - FPS 上限仍然会导致闪烁

转载 作者:行者123 更新时间:2023-12-01 05:30:34 24 4
gpt4 key购买 nike

我使用的 FPS 上限设置为 60 fps。我已经测试过了,它有效。问题是它仍然会导致屏幕闪烁。我的显示器设置为 60 fps。我不知道它怎么仍然引起闪烁。这是我的代码:

package com.bgp.chucknorris;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.net.URL;

public class ChuckNorrisApplet extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thread = null;
Image title;
URL base;
MediaTracker mt;
String fps = "";

public void init() {
thread = new Thread(this);
thread.start();

mt = new MediaTracker(this);
base = getDocumentBase();
title = getImage(base, "title.png");
mt.addImage(title, 1);
try {
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}

}

public void paint(Graphics g) {
g.drawImage(title, 0, 0, null);
}

public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}

public void stop() {
thread = null;
}

public void run() {
int frames = 0;

double unprocessedSeconds = 0;
long lastTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int tickCount = 0;

requestFocus();

while (true) {
long now = System.nanoTime();
long passedTime = now - lastTime;
lastTime = now;
if (passedTime < 0)
passedTime = 0;
if (passedTime > 100000000)
passedTime = 100000000;

unprocessedSeconds += passedTime / 1000000000.0;

boolean ticked = false;
while (unprocessedSeconds > secondsPerTick) {
unprocessedSeconds -= secondsPerTick;
ticked = true;

tickCount++;
if (tickCount % 60 == 0) {
System.out.println(frames + " fps");
fps = Integer.toString(frames);
lastTime += 1000;
frames = 0;
}
}

if (ticked) {
repaint();
frames++;
} else {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

}

最佳答案

为什么选择 Applet(而不是 JApplet)?请注意,Swing 组件是双缓冲的。

关于java - FPS 上限仍然会导致闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9050238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com