0

所以我在 java 上制作了一个 2D 游戏,并在我的游戏中实现了 BufferStrategy 方法。它卡在“this.createBufferStrategy(3);”这一行

从那时起,我收到此错误:

Exception in thread "Thread-2" java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at main.game.Game.render(Game.java:72)
at main.game.Game.run(Game.java:51)
at java.lang.Thread.run(Unknown Source)

我不知道为什么..请帮忙。这是我的 Game.java 代码:

package main.game;

import java.awt.image.BufferStrategy;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;


public class Game extends Canvas implements Runnable{

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;

    private static Boolean GameRunning = false;

    private static final long serialVersionUID = 5551732181250630703L;

    public synchronized void ThreadCreation(){
        new Thread(new Game()).start();
        GameRunning = true;
        System.out.println("Thread created and running");
    }

    public void run(){
        //A common game loop used which is the 'heart' of the game.
        int frames = 0;
        int updates = 0;
        double ticknumber = 60.0;
        double divider = 1000000000 / ticknumber;
        double difference = 0;

        long prevTime = System.nanoTime();
        long timeKeeper = System.currentTimeMillis();

        while(GameRunning = true){
            long currentTime = System.nanoTime();
            difference += (currentTime - prevTime) / divider;
            prevTime = currentTime;

            while(difference >= 1){
                difference = difference - 1;
                tick();
                updates++;
            }

            render();
            frames++;

            if(System.currentTimeMillis() - timeKeeper >= 1001){
                timeKeeper = timeKeeper + 1000;
                System.out.println(frames);
                System.out.println(updates);
                frames = 0;
                updates = 0;
            }
        }
    }

        //Count frames (refreshes)
        private void tick(){

    }

    private void render(){
        BufferStrategy strats = this.getBufferStrategy();
        if (strats == null) {
            this.createBufferStrategy(3);
            return;
        }       
        Graphics g = strats.getDrawGraphics();

        //These are going to have a colored background and fill the game WindowFrame with it
        g.setColor(Color.black);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.dispose();
        strats.show();
    }

    public static void main(String args[]){
        new WindowFrame(WIDTH, HEIGHT, "Combat", new Game());
    }
}

和 WindowFrame.java 代码:

package main.game;

import javax.swing.*;
import java.awt.*;

//This is the main WindowFrame class where the JFrame window's dimensions and features will be setted.
public class WindowFrame extends Canvas {

    private static final long serialVersionUID = -152448970909146807L;

    WindowFrame(int width, int height, String name, Game combat){

        combat.setPreferredSize(new Dimension(width, height));

        JFrame window = new JFrame(name);

        window.add(combat);                 //Adding the game to the new JFrame we made
        window.pack();
        window.setLocationRelativeTo(null); //Brings the window to the center of the screen instead of top left
        window.setResizable(false);         //Disabling resizing abilities
        window.setVisible(true);            //Visibility of windowFrame
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Actually closes the JFrame when we press exit
        combat.ThreadCreation();            //Starting the Thread creation and running it to execute game
    }
}
4

0 回答 0