Component must have a valid peer

trying to use double bufferign strategy, thats the material part of code

public class Panel_Glowny extends Canvas

{

public Panel_Glowny()

{

.....

createBufferStrategy(2);

buforowanie = getBufferStrategy();

requestFocus();

.....

}

public BufferStrategy buforowanie;

}

as a result i am getting this :

Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer

at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)

at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)

at java.awt.Component.createBufferStrategy(Unknown Source)

at java.awt.Canvas.createBufferStrategy(Unknown Source)

at java.awt.Component.createBufferStrategy(Unknown Source)

at java.awt.Canvas.createBufferStrategy(Unknown Source)

where is the problem and how can i solve it ?

regards

[956 byte] By [Koniecznya] at [2007-10-3 4:29:02]
# 1
i've the same problem...in theory, we must setVisible(true) the component in which we had add the canvas. After i had a new error!! an illegalArgumentException...But is there an example working fine?
snoopybad77a at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 2

According to the api method detail an IllegalArgumentException is thrown if the number of buffers (method argument) is less than one.

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferStrategy;

public class PG extends Canvas

{

public BufferStrategy buforowanie;

public PG()

{

requestFocus();

}

public void addNotify()

{

super.addNotify();

// the peer has been created

System.out.println("isDisplayable = " + isDisplayable());

// but this still throws an IllegalStateException

//if(isDisplayable())

//createBufferStrategy(2);

System.out.println("default strategy = " + getBufferStrategy());

}

public void paint(Graphics g)

{

super.paint(g);

// this component must be displayable before its

// paint method is called for the first time

System.out.println("getBufferStrategy = " + getBufferStrategy());

if(buforowanie == null)

{

createBufferStrategy(2);

buforowanie = getBufferStrategy();

System.out.println("buforowanie= " + buforowanie);

}

g.drawString(buforowanie.toString(), 10, 55);

}

public Dimension getPreferredSize()

{

return new Dimension(300,100);

}

public static void main(String[] args)

{

PG pg = new PG();

Frame f = new Frame();

f.addWindowListener(closer);

f.add(pg);

f.pack();

f.setVisible(true);

// this works okay, too

//pg.createBufferStrategy(2);

}

private static WindowListener closer = new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

};

}

74philipa at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 3

hey thats me konieczny, somehow the password doesnt work anymore for me ^^ so i was ought to make another account

i added what you adviced, the good thing was that no errors occured anymore and the application started to work, bad thing is that the result was even worse then the one i achieved before, without using doublebuffering, the screen started to flicker drastically

i changed ur code a bit and added a simple animation to figure some things out

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferStrategy;

import javax.imageio.ImageIO;

import javax.swing.*;

import javax.swing.JPanel;

import java.awt.image.BufferedImage;

import java.net.URL;

public class PG extends Canvas

{

public BufferStrategy buforowanie;

public BufferedImage pot;

public int x = 50, y = 100; ;

public PG()

{

requestFocus();

pot = loadImage("potworek.gif");

}

public void addNotify()

{

super.addNotify();

// the peer has been created

System.out.println("isDisplayable = " + isDisplayable());

// but this still throws an IllegalStateException

//if(isDisplayable())

//createBufferStrategy(2);

System.out.println("default strategy = " + getBufferStrategy());

}

public void paint(Graphics g)

{

super.paint(g);

// this component must be displayable before its

// paint method is called for the first time

System.out.println("getBufferStrategy = " + getBufferStrategy());

if(buforowanie == null)

{

createBufferStrategy(2);

buforowanie = getBufferStrategy();

System.out.println("buforowanie= " + buforowanie);

}

g.drawString(buforowanie.toString(), 10, 55);

g.drawImage(pot,x,y,this);

}

public Dimension getPreferredSize()

{

return new Dimension(300,100);

}

public static void main(String[] args)

{

PG pg = new PG();

Frame f = new Frame();

f.addWindowListener(closer);

f.add(pg);

f.pack();

f.setVisible(true);

pg.createBufferStrategy(2);

pg.game();

}

private static WindowListener closer = new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

};

public BufferedImage loadImage(String sciezka)

{

URL url = null;

try

{

url = getClass().getClassLoader().getResource(sciezka);

return ImageIO.read(url);

}

catch (Exception e)

{

System.out.println("Przy otwieraniu " + sciezka +" jako " + url);

System.out.println("Wystapil blad : "+e.getClass().getName()+" "+e.getMessage());

System.exit(0);

return null;

}

}

public void game() {

while (isVisible())

{

x = x + 2;

paint(getGraphics());

try {

Thread.sleep(20);

} catch (InterruptedException e) {}

}

}

}

used a random 30 x 30 gimp made gif file, the screen is flickering even wth such simple animation, what am I doing wrong ?

regards, Lukasz

Message was edited by:

szeryf

szeryfa at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 4

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

// you want to stay with AWT, right?

// swing has double buffering for free

// and is much easier...

//import javax.swing.*;

public class PG2 extends Canvas implements Runnable

{

public BufferStrategy buforowanie;

public BufferedImage pot;

Thread thread;

boolean animating;

public int x = 50, y = 100;

public PG2()

{

animating = false;

requestFocus();

pot = //loadImage("potworek.gif");

makeImage();

}

public Dimension getPreferredSize()

{

return new Dimension(400,400);

}

public static void main(String[] args)

{

PG2 pg = new PG2();

pg.addMouseListener(new Controller(pg));

Frame f = new Frame("click me");

f.addWindowListener(closer);

f.add(pg);

f.pack();

f.setVisible(true);

pg.createBufferStrategy(2);

pg.buforowanie = pg.getBufferStrategy();

//pg.start();

}

private static WindowListener closer = new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

};

public BufferedImage loadImage(String sciezka)

{

URL url = null;

try

{

url = getClass().getClassLoader().getResource(sciezka);

return ImageIO.read(url);

}

catch (IOException e)

{

System.out.println("Przy otwieraniu " + sciezka +" jako " + url);

System.out.println("Wystapil blad : "+e.getClass().getName()+

" "+e.getMessage());

System.exit(0);

return null;

}

}

public void toggle()

{

if(animating)

stop();

else

start();

}

private void start()

{

if(!animating)

{

animating = true;

thread = new Thread(this);

thread.start();

}

}

private void stop()

{

animating = false;

thread = null;

}

public void run()

{

int w = pot.getWidth(), h = pot.getHeight();

Rectangle clip = new Rectangle(w, h);

while (isVisible() && animating)

{

x = x + 2;

//paint(getGraphics()); // don't do this, call repaint

Graphics g = buforowanie.getDrawGraphics();

Graphics2D g2 = (Graphics2D)g;

g2.setPaint(getBackground());

//g2.setPaint(Color.red);

clip.setFrameFromDiagonal(x-8, y-8, x+w+6, y+h+6);

g2.fill(clip);

//g2.draw(clip);

g.drawImage(pot,x,y,this);

buforowanie.show();

try

{

Thread.sleep(30);

}

catch (InterruptedException e)

{

System.out.println("interrupt");

animating = false;

}

}

}

private BufferedImage makeImage()

{

int w = 30, h = 30;

int transparency = Transparency.OPAQUE;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice gd = ge.getDefaultScreenDevice();

GraphicsConfiguration gc = gd.getDefaultConfiguration();

BufferedImage bi = gc.createCompatibleImage(w, h, transparency);

System.out.println("bi type = " + bi.getType());

Graphics2D g2 = bi.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(Color.white);

g2.fillRect(0,0,w,h);

g2.setPaint(Color.green.darker());

g2.draw(new Rectangle2D.Double(w/16, h/16, w*7/8, h*7/8));

g2.setPaint(Color.red);

g2.fill(new Ellipse2D.Double(w/4, h/4, w/2, h/2));

g2.dispose();

return bi;

}

}

class Controller extends MouseAdapter

{

PG2 panel;

public Controller(PG2 pg2)

{

panel = pg2;

}

public void mousePressed(MouseEvent e)

{

panel.toggle();

}

}

74philipa at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 5

public void paint(Graphics g)

{

super.paint(g);

System.out.println("getBufferStrategy = " + getBufferStrategy());

createBufferStrategy(2);

buff = getBufferStrategy();

Graphics G = buff.getDrawGraphics();

G.drawImage(bg,0,0,this);

G.drawImage(anim[index], 10, 10, this);

G.dispose();

buff.show();

}

We must draw on the bufferStrategy Graphics object.

This is the right way, i' ve thought. But it is worse than earlier!!! The flicking is exended at the background!!! Now i hope in the other example.

Otherwise i think is a big joke by Sun

P.S. you must use the user name to login. It' your complete e-mail address. I digit snoopybad@hotmail.it

snoopybad77a at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 6
That's good!! We must draw like i've said, but out of paint() method !!! Finally we have solved the problem. Thanks very much to 74philip!!!
snoopybad77a at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 7

right :)

thanks for all ur feedback (including the one about logging on:), now everything works smooth and well

and the last question of mine, the moving object that i am going to buffer is the arkanoid ball that has to move a bit faster, therefore i changed the argument of sleep method down to 3 (originally it was 30), the object moves smoothly but sometimes applications slows down and then speed ups, what causes this ?

Message was edited by:

Konieczny

Koniecznya at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...
# 8
The sleep method make you sure that thread don't work for the declared time, but not that at the end of this time the thread will restart the execution!! I haven't try, but probably we must use the Timer class, for a better fluency
snoopybad77a at 2007-7-14 22:32:04 > top of Java-index,Security,Cryptography...