title: Component must be a valid peer when I remove frame.add(Component)
body: <p>I have this code here for creating and drawing an array of pixels into an image:</p>
  
  <pre><code>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 &lt; w; i++) {
              for (int j = x; j &lt; h; j++) {
                  pixels[i + j * WIDTH] = 346346;
              }
          }
  
  
      }
  }
  </code></pre>
  
  <p>Why do I get "Component must be a valid peer" error when I remove the line:</p>
  
  <pre><code>frame.add(wind);
  </code></pre>
  
  <p>Why do I want to remove it? Because I want to create a frame using a class object (from another file) and use the code <code>Window myWindow = new Window()</code> to do exactly the same thing.</p>