与笔记本电脑相比,PC上的Java FPS较低
我刚刚开始用Java开发简单的2D游戏。我从绘图方法中分离了更新。“更新”以60fps的特定速度刷新,同时以尽可能快的速度绘制。当我写了一个简单的FPS计数器并把它发给朋友时,我发现在笔记本电脑上它至少有200fps,而在个人电脑上它最多有35 FPS。我想知道这可能是什么原因造成的。下面是基类:与笔记本电脑相比,PC上的Java FPS较低,java,frame-rate,Java,Frame Rate,我刚刚开始用Java开发简单的2D游戏。我从绘图方法中分离了更新。“更新”以60fps的特定速度刷新,同时以尽可能快的速度绘制。当我写了一个简单的FPS计数器并把它发给朋友时,我发现在笔记本电脑上它至少有200fps,而在个人电脑上它最多有35 FPS。我想知道这可能是什么原因造成的。下面是基类: public class Base{ public static boolean isGameRunning; private static Draw d; public static void m
public class Base{
public static boolean isGameRunning;
private static Draw d;
public static void main(String[] args)
{
isGameRunning = true;
Game g = new Game();
d = new Draw(g);
Thread draw = new Thread(d);
Thread update = new Thread(new Update(g));
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
JFrame window = new JFrame();
setWindow(window);
window.setContentPane(d);
gd.setFullScreenWindow(window);
g.initialize();
draw.start();
update.start();
}
private static void setWindow(JFrame window)
{
window.setUndecorated(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
这里有一个绘画课:
package basic;
import java.awt.*;
import javax.swing.JPanel;
import main.Game;
class Draw extends JPanel implements Runnable
{
private static final long serialVersionUID = 1L;
private Game g;
public Color bgColor = Color.BLACK;
Draw(Game g)
{
this.g = g;
}
public void run()
{
paintComponent(getGraphics());
while(Base.isGameRunning)
{
repaint();
}
}
public void paintComponent(Graphics graph)
{
try
{
Graphics2D g2d = (Graphics2D) graph;
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setColor(bgColor);
g2d.fillRect(0, 0, getWidth(), getHeight());
g.draw(g2d);
}catch(NullPointerException e)
{
}
}
}
[编辑]这里有一个更新类:
package basic;
import main.Game;
class Update implements Runnable{
private final int FPS = 60;
private long targetTime = 1000/FPS;
//private Game g;
//Update(Game g)
//{
//this.g = g;
//}
public void run()
{
long start, elapsed, wait;
while(Base.isGameRunning)
{
start = System.nanoTime();
//g.update();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
try
{
Thread.sleep(wait);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}可以做一些改进。我试图更改代码,因为您没有提供更新类
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @author Pasban
*/
public class Base {
public static boolean isGameRunning;
private static Draw d;
public static void main(String[] args) {
isGameRunning = true;
d = new Draw();
Thread draw = new Thread(d);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
JFrame window = new JFrame();
setWindow(window);
window.setContentPane(d);
gd.setFullScreenWindow(window);
//g.initialize();
draw.start();
}
private static void setWindow(JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
及
根据上述代码,我的旧电脑中的FPS目前为103。我应用的主要更改是在这一行:
//g2d.clearRect(0, 0, getWidth(), getHeight());
通过启用该线路,FPS下降到70 FPS,这意味着该线路本身使用50+FPS。但是,在执行此命令后填充绘图矩形时,将其禁用,这同样意味着您无缘无故地使用CPU和FPS,并且不会对绘图画布产生任何影响
另一个改进是,如果超过60+FPS,可以通过应用Thread.sleeptime来修复FPS。或者,您可以忽略while Base.isGameRunning中的重新绘制方法,因为现在重新绘制还为时过早。我没有帮助:/I添加了更新。你知道为什么在笔记本电脑上它能正常工作吗?我想你需要检查一下你的显卡是否和你朋友的一样。如果您比较设备的功能,就会找到原因。我没有任何外部图形卡,但当我有一个图形卡时,图形处理运行得更快。很明显,它不是访问主RAM,而是访问自己的RAM,速度更快。我就是这么想的。也许其他人有更好的解释。
//g2d.clearRect(0, 0, getWidth(), getHeight());