问题
I stumbled upon a problem I cant solve nor can I understand. I'm writing a simple 2D game using Java 1.8.0_31 with java.awt.Canvas
on Ubuntu 14.04. The problem is that I get this unbearable flickering(an issue I never had on Windows), to make sure my code is not the problem I wrote a small test program with a moving circle...
The test loop:
public void run() {
long startTime = System.currentTimeMillis();
long tick = 1000;
int fps = 0;
int upd = 0;
double nanoScale = 1000000000.0 / 60;
double delta = 0;
long lastTime = System.nanoTime();
while (running) {
long now = System.nanoTime();
delta += (System.nanoTime() - lastTime) / nanoScale;
lastTime = now;
while (delta > 1) {
update();
upd++;
delta--;
}
render();
fps++;
if (System.currentTimeMillis() - startTime > tick) {
window.setTitle(title + " || Fps: " + fps + " | Upd: " + upd);
fps = 0;
upd = 0;
tick += 1000;
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This gives me a nice and steady Update rate of 60 with a 185-190 fps rate which is only limited by the Thread.sleep(5)
call.
However it looks/feels like the fps is incredibly low(as though as most frames get dropped), now before you start yelling use multiple buffers...
The render method:
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics2D g2d = (Graphics2D) bs.getDrawGraphics().create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.red);
g2d.fillOval(x, y, 10, 10);
g2d.dispose();
bs.show();
}
Oddly enough enabling the antialising hint greatly reduces the flickering but not removes it.
I cant see the relation between the problem and antialiasing, enlighten me if there is a connection(I always got a clean and smooth result on Windows regardless of antialiasing).
So I guess the question is:
What might cause the issue described and what can be done about it?
Just in case this helps my Graphics card is gtx770 and I'm using the NVIDIA binary driver - version 340.76 from nvidia-340(open source)
来源:https://stackoverflow.com/questions/28438715/flickering-java-gui-in-ubuntu-14-04