// 这是我的代码:
Main Class:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Display extends Canvas implements Runnable{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getScreenSize();
public static int WIDTH;
public static int HEIGHT;
public static final String title = "First Person Game";
public static Thread thread;
public static Screen screen;
public static BufferedImage img;
public static boolean running = false;
public static int[] pixels;
public static Render render;
public Display(){
WIDTH = dim.width;
HEIGHT = dim.height;
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
private void start(){
if(running){
return;
}else{
running = true;
thread = new Thread(this);
thread.start();
}
}
private void stop(){
if(!running){
return;
}else{
running = false;
try{
thread.join();
}catch(Exception x){
System.exit(0);
}
}
}
public void run(){
while(running){
render();
}
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
screen.render();
for(int i = 0; i < WIDTH * HEIGHT; i++){
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String args[]){
JFrame frame = new JFrame();
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
frame.getContentPane().setCursor(blankCursor);
Display game = new Display();
frame.setUndecorated(true);
frame.add(game);
frame.setTitle(title);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
fps f = new fps();
game.start();
}
}
The class I need to call:
public class fps{
public void fps(){
System.out.println("Test 1 successful.");
int frames = 0;
double unprocessedSeconds = 0;
long previousTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int tickCount = 0;
boolean ticked = false;
Display c = new Display();
System.out.println("Test 2 successful.");
c.render();
long currentTime = System.nanoTime();
long passedTime = currentTime - previousTime;
previousTime = currentTime;
unprocessedSeconds += passedTime / 1000000000.0;
while(unprocessedSeconds > secondsPerTick){
System.out.println("Test 3 successful.");
tick();
unprocessedSeconds -= secondsPerTick;
ticked = true;
tickCount++;
if(tickCount % 60 == 0){
System.out.println(frames + " fps");
previousTime += 1000;
frames = 0;
}
}
if(ticked){
c.render();
frames++;
}
c.render();
frames++;
}
public void tick(){}
}
/* 我不知道该怎么做,我一直在尝试各种各样的事情。我基本上需要 * 确保 fps 在显示运行时打印到控制台。我似乎不能这样 */ 这个。我在方法 run() 中尝试过,但它不会调用它。