-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

メインクラスにfpsクラスのインスタンスを作成し、それを介してメソッドを呼び出す必要があると思います。

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

少なくともこれは、Javaと非常によく似ていることを私が知っているC#での方法です。

これで問題が解決しない場合は、スレッドの問題であると考えられます。スレッドを適切に処理していることを確認する必要があります。(私はこの問題を解決することはできません、私はまだJavaスレッドにグリーンです。)

于 2012-04-05T16:20:02.947 に答える
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オブジェクトを作成した後でメソッドを呼び出すことができます。

于 2012-04-05T16:21:50.900 に答える
0

コードにエラーがあります:

private void start(){
    if(running){
        return;
    }else{
        running = true;
        thread = new Thread(this);
        thread.start();
    }
}

スレッドは、startメソッドを呼び出すことによって開始されます。あなたがしたことは、start()メソッドを変更することです。Thread.start()はスレッドを開始するためのものであり、run()は実行コードを配置する場所であるため、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. 
于 2012-06-29T12:58:21.480 に答える
-1

ええと、あなたは2つのことのうちの1つをすることができます。fpsオブジェクトを宣言することができます。

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

または、すべて静的にすることができます。

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

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