3

ピクセルの配列を作成して画像に描画するための次のコードがあります。

import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

public class test extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;
    public static int WIDTH = 800;
    public static int HEIGHT = 600;
    public boolean running = true;
    public int[] pixels;
    public BufferedImage img;
    public static JFrame frame;
    private Thread thread;

    public static void main(String[] arg) {
        test wind = new test();
        frame = new JFrame("WINDOW");
        frame.add(wind);
        frame.setVisible(true);
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        wind.init();
    }

    public void init() {
        thread = new Thread(this);
        thread.start();
        img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    }

    public void run() {
        while (running) {
            render();
            try {
                thread.sleep(55);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(4);
            return;
        }
        drawRect(0, 0, 150, 150);
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();

    }

    private void drawRect(int x, int y, int w, int h) {
        for (int i = x; i < w; i++) {
            for (int j = x; j < h; j++) {
                pixels[i + j * WIDTH] = 346346;
            }
        }


    }
}

行を削除すると、「コンポーネントは有効なピアである必要があります」というエラーが表示されるのはなぜですか:

frame.add(wind);

なぜ削除したいのですか?クラスオブジェクトを使用して(別のファイルから)フレームを作成し、コードWindow myWindow = new Window()を使用してまったく同じことをしたいからです。

4

1 に答える 1

5

@nIcE cOw のコメントとして、ヘビーウェイトコンポーネントとライトウェイト コンポーネントを混在させているようです。置換Frameしても根本的な問題は残ります。ホスト プラットフォームによって提供される重量級のピア コンポーネントの機能に依存する に追加されるまで は表示できないcreateBufferStrategy()ため、 は例外をスローします。実際には、戦略を使用するバッファを指定せずに を選択しようとしています。CanvasFrameBufferStrategy

代わりに、既存のエンジンJComponentを使用するか、によって提供されるデフォルトのバッファ戦略に依存してください。

スレッド「Thread-2」の例外 java.lang.IllegalStateException: コンポーネントには有効なピアが必要です
    java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3843) で
    java.awt.Component$FlipBufferStrategy.(Component.java:3817) で
    java.awt.Component$FlipSubRegionBufferStrategy.(Component.java:4358) で
    java.awt.Component.createBufferStrategy (Component.java:3699) で
    java.awt.Canvas.createBufferStrategy (Canvas.java:166) で
    java.awt.Component.createBufferStrategy (Component.java:3623) で
    java.awt.Canvas.createBufferStrategy (Canvas.java:141) で
    test.render(test.java:52)で
    test.run(test.java:40)で
    java.lang.Thread.run(Thread.java:680) で
于 2012-06-22T16:13:40.693 に答える