线程和缓冲区策略问题:illegalstateexception:组件必须有有效的对等方

wooyq4lh  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(259)

以下是我尝试执行但失败的代码:

public class Game extends Canvas implements Runnable,KeyListener {

    private static final long serialVersionUID = 1L;
    private static Thread thread ;
    private int width = 240;
    private int height = 120;
    private int scale = 3;
    private boolean isRunning = true;

    private BufferedImage image;
    public static Spritesheet spritesheet;

    private Player player;

    Game(){
         Dimension dimension = new Dimension(width*scale,height*scale);
         this.setPreferredSize(dimension);

         image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

    }

    void initframe() {

        JFrame f = new JFrame("Game");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
    }

    public synchronized void start() {

        thread = new Thread(this);
        isRunning = true;
        thread.start();
    }

    public synchronized void stop() {

        isRunning = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {

        Game game = new Game();
        game.start();

    }

    void tick() {

    }

    void render() {

        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = image.getGraphics();
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, width, height);

        g.dispose();
        g = bs.getDrawGraphics();
        g.drawImage(image, 0, 0, width*scale,height*scale,null);
        bs.show();

    }

    public void run() {

     long lastTime = System.nanoTime();
     double amountOfTicks = 60.0;
     double ns = (10^9)/amountOfTicks ;
     int frames = 0;
     double delta;
     double timer = System.currentTimeMillis();

     while(isRunning) {
         long now = System.nanoTime();
         delta = (now - lastTime)/ns ;
         lastTime = now;
         if(delta >= 1) {
             tick();
             render();
             frames++;
             delta--;
         }

         if(System.currentTimeMillis() - timer >= 1000) {
             System.out.println(" A taxa de fps e:   "+frames);
             frames = 0;
             timer+=1000;
         }

     }

        stop();
    }

我觉得错误是:

Exception in thread "Thread-0" java.lang.IllegalStateException: Component must have a valid peer
    at java.desktop/java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:4105)
    at java.desktop/java.awt.Component$FlipBufferStrategy.<init>(Component.java:4079)
    at java.desktop/java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4611)
    at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3942)
    at java.desktop/java.awt.Canvas.createBufferStrategy(Canvas.java:195)
    at java.desktop/java.awt.Component.createBufferStrategy(Component.java:3866)
    at java.desktop/java.awt.Canvas.createBufferStrategy(Canvas.java:170)
    at com.main.Game.render(Game.java:83)
    at com.main.Game.run(Game.java:115)
    at java.base/java.lang.Thread.run(Thread.java:834)

我检查了这段代码的每一行,我不知道为什么会显示问题出在我使用的线程或缓冲策略、呈现方法或运行方法上。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题