簡體   English   中英

與筆記本電腦相比,PC上的Java FPS低

[英]Java low FPS on PC comparing to laptop

我剛剛開始用Java開發簡單的2D游戲。 我從分裂平局方法的更新。 更新以60fps的特定速度刷新,而繪制則盡可能快。 當我編寫簡單的FPS取款計數器並將其發送給朋友時,我發現在筆記本電腦上,它的速度至少為200fps,而在PC機上的最大速度為35fps。我想知道這可能是由什么引起的。 這是基類:

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

}

}

可以做一些改進。 我嘗試更改代碼,因為您沒有提供Update類。

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

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

目前,按照上述代碼,我的舊PC中的FPS為103。 我應用的主要更改是在此行上:

//g2d.clearRect(0, 0, getWidth(), getHeight());

通過啟用此線路,FPS下降到70FPS,這意味着該線路本身使用50 + FPS。 但是,在執行此命令后填充圖形rect時,請禁用它,這再次意味着您無緣無故地使用CPU和FPS,並且對繪圖畫布沒有影響。

如果Thread.sleep(time)超過60 + FPS,則可以應用Thread.sleep(time)來修復FPS。 或者,當還為時過早時,可以在while(Base.isGameRunning)中忽略重繪方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM