标题最有可能令人困惑,但让我详细说明一下。
我正在制作一个视频游戏。我到处搜索这个问题的答案,但似乎我无法使用我的代码。我有两个类:一个用于主要方法,另一个用于 JFrame:
运行游戏.java:
package net.naprav.wardungeon;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class RunGame extends Canvas implements Runnable {
private static final long serialVersionUID = 1203994186653691379L;
private static final int WIDTH = WindowFrame.WIDTH;
private static final int HEIGHT = WindowFrame.HEIGHT;
private static final Dimension size = WindowFrame.size;
private Thread thread;
public boolean isRunning = false;
public static int FPS = 0; //Frames per second.
public static int TPS = 0; //Ticks per second. (Updates per second)
public static String frames;
public static String updates;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] allPixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
//static Key key;
RenderSystem system;
/**
* Added the main constructor.
*/
public RunGame() {
this.setVisible(true);
this.setSize(size);
this.setPreferredSize(size);
this.setMinimumSize(size);
this.setMinimumSize(size);
system = new RenderSystem(WIDTH, HEIGHT);
}
/**
* Responsible for the logic behind the game.
*/
public void tick() {
}
/*
* Responsible for the actual rendering behind the game.
*/
public void render() {
BufferStrategy buffer = this.getBufferStrategy();
//Clearing the screen to make room for the pixels! :D
system.clearScreen();
//Rendering the pixels in the RenderMechanism class.
system.changePixels();
for (int counter = 0; counter < allPixels.length; counter++) {
//Setting the pixels in this class to the ones in RenderMechanism.java.
allPixels[counter] = system.allPixels[counter];
}
//Buffer is automatically null, so we can create one to render a number of buffers. (3)
if (buffer == null) {
this.createBufferStrategy(3);
return;
}
//Graphics setup.
Graphics gfx = buffer.getDrawGraphics();
gfx.setColor(new Color(146, 17, 189));
gfx.fillRect(0, 0, this.getWidth(), this.getHeight());
//Draw stuffs between here...
gfx.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
//and here.
gfx.dispose();
buffer.show();
}
/**
* Used to start the thread and make it go! :D
*/
public synchronized void begin() {
thread = new Thread(this);
thread.start();
isRunning = true;
}
/**
* Used to end the thread and make it stop! :/
*/
public synchronized void finish() {
if (isRunning == true) {
isRunning = false;
try {
thread.join();
} catch (InterruptedException exc) {
System.out.println("Couldn't join thread! :(");
exc.printStackTrace();
}
}
}
/**
* The run method is used to run the game itself in the thread.
*/
public void run() {
long previousTime = System.nanoTime();
long secondTimer = System.currentTimeMillis();
final double nanoSeconds = 1000000000.0 / 60.0;
double omega = 0;
while (isRunning == true) {
long currentTime = System.nanoTime();
omega += (currentTime - previousTime) / nanoSeconds;
previousTime = currentTime;
while (omega >= 1) {
this.tick();
TPS++;
omega--;
}
this.render();
FPS++;
if ((System.currentTimeMillis() - secondTimer) > 1000) {
secondTimer += 1000;
//Un-comment out below code for console output of frames and updates.
frames = String.valueOf(FPS);
updates = String.valueOf(TPS);
System.out.println(frames + ", " + updates);
FPS = 0;
TPS = 0;
}
}
this.finish();
}
/**
* The Main method.
* @param args
*/
public static void main(String[] args) {
WindowFrame window = new WindowFrame("WarDungeon", FPS, TPS);
//Get rid of '//' below to test the login screen!
//LoginScreen login = new LoginScreen("WarDungeon Login");
}
}
上面的代码用于使用线程运行游戏。我将 RunGame.java 类(扩展 Canvas)添加到 WindowFramejava 类:
package net.naprav.wardungeon;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class WindowFrame extends JFrame {
private static final long serialVersionUID = 1203994186653691379L;
public static final int WIDTH = 330;
public static final int HEIGHT = 200;
public static final int SCALE = 3;
public static final Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
private ImageIcon icon;
RunGame game;
public WindowFrame(String title) {
this.setTitle(title);
this.setSize(size);
this.setVisible(true);
this.setResizable(true);
this.setLocationRelativeTo(null);
icon = new ImageIcon("res/wardungeon_logo.png");
this.setIconImage(icon.getImage());
game = new RunGame();
this.add(game);
game.begin();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public WindowFrame(String title, int frame, int update) {
this.setTitle(title + " | " + "FPS: " + frame + ", UPS: " + update);
this.setSize(size);
this.setVisible(true);
this.setResizable(true);
this.setLocationRelativeTo(null);
icon = new ImageIcon("res/wardungeon_logo.png");
this.setIconImage(icon.getImage());
game = new RunGame();
this.add(game);
game.begin();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我知道这是很多代码,我很抱歉。我仍然是 Java 编码的菜鸟,但如果你能帮我解决这个问题,那就太好了。我的主要问题是我无法在标题栏上显示 FPS 和 TPS。我不知道如何在不影响帧和更新的“每秒”的情况下更改该变量。
提前致谢。