与笔记本电脑相比,PC上的Java低FPS

时间:2015-05-22 09:04:07

标签: java frame-rate

我刚开始用Java开发简单的2D游戏。我从draw方法中分离出更新。更新以60fps的特定速度刷新,同时绘图尽可能快。当我写简单的FPS抽奖计数器并将其发送给朋友时,我发现在笔记本电脑上它至少有200fps,当它在PC上时它最多有35个。我想知道它可能是由什么造成的。这是基类:

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);
}}

这里有一个Draw课程:

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();
        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

可以做一些改进。我试图更改代码,因为你没有提供Update类。

Zend session save handler

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);
    }
}

目前我的旧PC中的FPS是上述代码的103。我应用的主要变化是在这一行:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

/**
 *
 * @author Pasban
 */
class Draw extends JPanel implements Runnable {

    private static final long serialVersionUID = 1L;
    public Color bgColor = Color.BLACK;
    private long timer;
    private long lastTime;
    private int FPS;
    private int FPSLast;

    public void run() {
        paintComponent(getGraphics());

        this.timer = System.currentTimeMillis();
        this.lastTime = 0;
        this.FPS = 0;
        this.FPSLast = 0;
        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);
            g2d.setColor(Color.WHITE);
            long dist = System.currentTimeMillis() - this.timer;
            if (dist - this.lastTime > 1000) {
                this.lastTime = dist;
                this.FPSLast = this.FPS;
                this.FPS = 0;
            }
            this.FPS++;
            g2d.drawString(dist + "ms / " + this.FPSLast + "fps", 50, 50);

        } catch (NullPointerException e) {
        }
    }
}

通过启用此线路,FPS向下降至70FPS,这意味着此线路本身使用50 + FPS。但是,在此命令之后填充绘图矩形时禁用它,再次表示您无缘无故地使用CPU和FPS,它对绘图画布没有任何影响。

如果超过60 + FPS,可以通过应用Thread.sleep(时间)来修复您的FPS。或者你可以忽略你的while(Base.isGameRunning)中的重绘方法,因为现在还不能重新绘制。