Skip to main content
gamedev.net
🔒 Locked

[java] Canvas issues

Started by phaelax 0 replies 3.4k views
Original Post
phaelax
phaelax
All I'm doing is drawing some boxes onto a Canvas that can be dragged with the mouse. Since it flickers when i move them I wanted to use double buffering. Here's part of the class.

public class DisplayCanvas extends Canvas implements MouseInputListener
{
    private BufferStrategy buffer;
    private ArrayList objects = new ArrayList();
    private DisplayObject selectedObject = null;
    

    public DisplayCanvas()
    {
        this.createBufferStrategy(1);
        buffer = this.getBufferStrategy();
        
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

When I try to set the number of buffers to anything higher than 1, I get this error when trying to run the program: java.lang.IllegalStateException: Component must have a valid peer Not entirely sure what it means and I couldn't really find anything about it other than the docs said its thrown if the canvas isn't displayable.
blow
blow
U have this prob becouse you create bufferStrategy before that this component is add to container.
Try this:
public void addNotify(){   super.addNotify();   this.createBufferStrategy(2);   buffer = this.getBufferStrategy();}


So only when your add this compunent u create a byfferstrategy

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.