BufferStrategy,Java中的多个异常和错误

时间:2012-04-09 19:31:20

标签: java swing exception

我正在尝试制作一款相当基本的3D游戏,以便在我参加年度比赛Ludum Dare 23时为即将到来做准备。

我正在使用Java而我正在使用Swing和AWT。 (我正在使用JFrame来创建我的窗口(显然是?idk))

当我试图在屏幕上绘制像素时,我遇到的问题正在发生。关于BufferStrategy以及显然是线程问题,我得到了许多例外。

以下是Eclipse中的控制台框向我抛出的内容。

Exception in thread "Thread-2" java.lang.IllegalStateException: Component must have a valid peer
    at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3982)
    at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3956)
    at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4479)
    at java.awt.Component.createBufferStrategy(Component.java:3833)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:194)
    at java.awt.Component.createBufferStrategy(Component.java:3756)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:169)
    at com.ottdev.tale.TaleOfDwarvesComponent.render(TaleOfDwarvesComponent.java:66)
    at com.ottdev.tale.TaleOfDwarvesComponent.run(TaleOfDwarvesComponent.java:55)
    at java.lang.Thread.run(Thread.java:722)

关于如何解决这个问题的任何想法?它正在努力,阻止我前进。所有帮助都非常感谢!

PS:如果需要,我可以提供我的源代码,但是现在,我宁愿知道如何来修复它,这样我就知道如果遇到这种情况该怎么办未来,但代码不同。

编辑:我的主要课程 - TaleOfDwarvesComponent:

package com.ottdev.tale;    
import java.awt.*;
import java.awt.image.*;

import javax.swing.*;

import com.ottdev.tale.gui.*;

public class TaleOfDwarvesComponent extends Canvas implements Runnable {
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int SCALE = 2;
    public static final String TITLE = "Tale Of Dwarves!";

    private boolean running = false;
    private BufferedImage img;
    public int[] pixels;
    private Thread thread;
    private Game game;
    private Screen screen;

    public TaleOfDwarvesComponent() {
        game = new Game();
        screen = new Screen(WIDTH, HEIGHT);

        img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();



    }

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

    public void stop() {
        if (!running)
            return;
        running = false;
        try{
            thread.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }

    public void run() {
        while(running){
            render();
        }
    }

    public void tick(){
        game.tick();
    }

    public void render(){

        BufferStrategy bs = getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }
        screen.render(game);
        for (int i = 0; i < WIDTH * HEIGHT; i++){
            pixels[i] = screen.pixels[i];
        }

        Graphics g = bs.getDrawGraphics();
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(img, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        TaleOfDwarvesComponent game = new TaleOfDwarvesComponent();
        JFrame frame = new JFrame();
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setTitle(TITLE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        game.start();

    }
}

我有一个Bitmap课程和一个Screen课程,但就目前而言,我会这样做,希望能从中获得对我的麻烦的答案。

1 个答案:

答案 0 :(得分:0)

如果你看一下javadocs http://docs.oracle.com/javase/7/docs/api/java/awt/Canvas.html#createBufferStrategy(int

我相信它专门解决了您的问题,即您在屏幕上显示Canvas之前调用createBufferStrategy。因此,要解决您的问题,您需要将ComponentListener添加到Canvas并在componentShown上调用createBufferStrategy,或者在paint方法中调用它