3

I have this code here for creating and drawing an array of pixels into an image:

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;
            }
        }


    }
}

Why do I get "Component must be a valid peer" error when I remove the line:

frame.add(wind);

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 Window myWindow = new Window() to do exactly the same thing.

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53
  • 1
    It's an era of Swing, so why you using `Canvas`, why not use the important feature of `JPanel`, which is `DoubleBuffering`, why not extend `JPanel` instead of the way old schema `Canvas` ? Moreover, why you blocking your EDT with code lines like `Thread.sleep(...)`, why not use [Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for this purpose ? – nIcE cOw Jun 22 '12 at 15:24
  • 4
    @Downvoter : Any solid reason to downvote this ? – nIcE cOw Jun 22 '12 at 15:28
  • 1
    @nIcE cOw you attracted my attention: Timers and JPanel? hmmm can you show me your version of the code? – Ionel Lupu Jun 22 '12 at 15:40
  • 1
    @nIcEcOw: I see no down-vote; you may have seen an accidental click. See also [*How do I contact other users*](http://meta.stackexchange.com/questions/57537/how-do-i-contact-other-users)? – trashgod Jun 22 '12 at 15:41
  • @boyd : Try any versions on this [thread](http://stackoverflow.com/a/9852739/1057230), especially by "trashgod", too good an example on the topic. – nIcE cOw Jun 22 '12 at 16:03
  • @nIcE cOw you know java well? because (if it's possible) we can make a small team... – Ionel Lupu Jun 22 '12 at 16:05
  • @boyd : I am also a learner like you, just know a bit about Swing now, still learning Painting in Swing with each day. It's always a pleasure to work as a team though, happy to be a part of one :-) – nIcE cOw Jun 22 '12 at 16:29

1 Answers1

5

As @nIcE cOw comments, you appear to be Mixing Heavyweight and Lightweight Components. Substituting Frame still leaves the underlying problem: createBufferStrategy() throws the exception because the Canvas is not displayable until added to the Frame, which relies on the features of the heavyweight peer component provided by the host platform. In effect you are trying to choose a BufferStrategy without specifying which buffer should use the strategy.

Instead, use an existing engine, or rely on the default buffer strategy provided by JComponent, for example.

Exception in thread "Thread-2" java.lang.IllegalStateException: Component must have a valid peer
    at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3843)
    at java.awt.Component$FlipBufferStrategy.(Component.java:3817)
    at java.awt.Component$FlipSubRegionBufferStrategy.(Component.java:4358)
    at java.awt.Component.createBufferStrategy(Component.java:3699)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:166)
    at java.awt.Component.createBufferStrategy(Component.java:3623)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:141)
    at test.render(test.java:52)
    at test.run(test.java:40)
    at java.lang.Thread.run(Thread.java:680)
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    +1, wish I knew Painting in Java, in that depth. LOL still studying this part each day as it comes :-) – nIcE cOw Jun 22 '12 at 16:28