【问题标题】:Component must be a valid peer when I remove frame.add(Component)当我删除 frame.add(Component) 时,组件必须是有效的对等体
【发布时间】:2012-06-24 22:05:48
【问题描述】:

我这里有这段代码,用于创建像素数组并将其绘制到图像中:

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() 来做完全相同的事情。

【问题讨论】:

  • 现在是 Swing 时代,那你为什么要使用Canvas,为什么不使用JPanel 的重要功能,即DoubleBuffering,为什么不扩展JPanel 而不是旧的方式架构Canvas ?此外,为什么你用 Thread.sleep(...) 这样的代码行来阻止你的 EDT,为什么不使用 Timer 来达到这个目的呢?
  • @Downvoter : 有什么理由不赞成这个吗?
  • @nIcE cOw 你引起了我的注意:定时器和 JPanel?嗯,你能告诉我你的代码版本吗?
  • @nIcEcOw:我看不到反对票;您可能已经看到意外点击。另见How do I contact other users?
  • @boyd :试试这个thread 的任何版本,尤其是“trashgod”,这个主题的例子太好了。

标签: java swing awt bufferedimage


【解决方案1】:

作为@nIcE cOw cmets,您似乎是Mixing Heavyweight and Lightweight Components。替换Frame 仍然会留下潜在的问题:createBufferStrategy() 会引发异常,因为Canvas 在添加到Frame 之前是不可显示的,这依赖于主机平台提供的重量级对等组件的功能。实际上,您正在尝试选择 BufferStrategy 而不指定应使用该策略的缓冲区。

改为使用现有的engine,或依赖JComponent 提供的默认缓冲区策略,用于example

线程“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)

【讨论】:

  • +1,希望我能深入了解 Java 绘画。 LOL 仍然每天都在学习这部分 :-)
猜你喜欢
  • 2016-09-15
  • 2015-06-28
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2019-04-25
  • 2015-01-03
相关资源
最近更新 更多