-2

// 这是我的代码:

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() 中尝试过,但它不会调用它。

4

4 回答 4

0

It is a little strange to have a method with the same name as your class. Is it meant to be a constructor, like this?

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

I would also suggest that all that code is not really appropriate for a constructor and should be moved to its own method. Then, as mentioned by others, you can call your method after first creating an fps object.

于 2012-04-05T16:21:50.900 回答
0

我相信您需要在主类中创建 fps 类的实例,然后通过它调用该方法。

fps nameOfClassInstance = new fps(//don't forget anything you need to send for constructor);
fps.run();

至少在 C# 中是这样的,我知道它与 java 非常相似。

如果这没有帮助,那么我相信这是一个线程问题,您需要确保正确处理线程。(我对这个问题无能为力,我对 Java 线程仍然很陌生。)

于 2012-04-05T16:20:02.947 回答
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

公共无效开始()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. 
于 2012-06-29T12:58:21.480 回答
-1

Well, you can do one of two things. You can either declare an fps object:

fps myfps = new fps();
myfps.fps();

or you can make it all static:

public static void fps(){
  ...
}
public static void tick(){}

//elsewhere
fps.fps()
于 2012-04-05T16:21:18.937 回答