//这是我的代码:
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()但它不会调用它。
答案 0 :(得分:0)
我相信你需要在主类中创建一个fps类的实例,然后通过它调用该方法。
fps nameOfClassInstance = new fps(//don't forget anything you need to send for constructor);
fps.run();
至少这就是它在C#中的表现方式,我知道它与java非常相似。
如果这没有帮助,那么我相信它是一个线程问题,你需要确保正确处理线程。 (我对这个问题无能为力,我仍然是绿色的java线程。)
答案 1 :(得分:0)
有一个与你的同名同名的方法有点奇怪。它是否意味着像这样的构造函数?
public class fps{
//Note that the void is removed here
public 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对象后调用您的方法。
答案 2 :(得分:0)
您的代码中存在错误:
private void start(){
if(running){
return;
}else{
running = true;
thread = new Thread(this);
thread.start();
}
}
通过调用start方法启动线程。你所做的是改变start()方法。您应该使用run方法而不是start方法,因为Thread.start()用于启动线程,而run()是您应该放置执行代码的位置。
从Java API的JavaDoc开始,http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#start%28%29
public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
答案 3 :(得分:-1)
嗯,你可以做两件事之一。您可以声明一个fps对象:
fps myfps = new fps();
myfps.fps();
或者你可以使它全部静止:
public static void fps(){
...
}
public static void tick(){}
//elsewhere
fps.fps()