Write a JPanel to a Texture with TextureRenderer

Hi all !
I would like to draw some JPanel in my GLJPanel. In order to draw a lot of them with correct performances I think creating texture of the panels is better than add the panels to the GLJPanel. I don’t need the Swing Events so it is not a problem.

I’ve succeeded to draw some Java2D in texture but I can’t see anything with the panels. I’ve tried this :

TextureRenderer renderer = new TextureRenderer(64,64, true);
Graphics2D g2 = renderer.createGraphics();

JPanel panel = new JPanel();
JLabel label = new JLabel("test");
panel.add(label);
panel.setPreferredSize(new Dimension(64, 64));
			
panel.paint(g2);
			
g2.dispose();
			
Texture t = renderer.getTexture();

So if I replace panel.paint(g2) by g2.fillOval(0, 0, 64, 64) it is working well I can see the shape.

And after I draw the texture on a rectangle like this :

texture.enable();
		
gl.glBegin(GL.GL_TRIANGLE_STRIP);
texture.bind();
gl.glTexCoord2d(1, 1);
gl.glVertex3f(w, h, 0);
gl.glTexCoord2d(0, 1);
gl.glVertex3f(-w, h,0);
gl.glTexCoord2d(1, 0);
gl.glVertex3f(w, -h, 0);
gl.glTexCoord2d(0, 0);
gl.glVertex3f(-w,-h, 0);
gl.glEnd();
	    
texture.disable();

What I’m doing wrong ? Thank a lot :wink:

I’m not a Swing expert but you may need to turn off double-buffering on your JPanel. However it might be another issue. Take a look at the source code for the XTrans demo in the jogl-demos workspace.

Depending on the used LayoutManager you could have to call the doLayout-Method or something like that.

Do you see the contents if you display the JPanel in a (second) JFrame before?

Yes seems to be more a Swing problem than TextureRenderer. I’m not a Swing expert either but I think it doesn’t work because the panel is not realized.

I did the test Saxer recommended : After the creation of the JPanel and before the render of the Texture, I added the JPanel into a JFrame, pack it and make it visible. It works, the Texture shows the JPanel (in the inverse order but doesn’t matter for now). So I did some tests, if I just set the JFrame visible without pack() it doesn’t work. So I assumed the problem is because I try to render into a Texture a JPanel which has never been realized.
Hence I tried to validate manually my JPanel after creation, unfortunately the validate() method doesn’t validate because the test isValid() is still false after it. I’m not sure of the behaviour of Swing when working on non-realized Component, I also tried doLayout() but it doesn’t work…

try to call addNotify() before validate()
http://forums.java.net/jive/thread.jspa?messageID=267766&#267880

Thank you all, with addNotify() and setSize() the JPanel is drawn into the texture. I also use print() instead of paint(), which do an Exception in JOGL when setSize() is defined.

Here is my code for creating the texture


TextureRenderer renderer = new TextureRenderer(64,64, true);
		
JPanel panel = new JPanel();
JLabel label = new JLabel("test");
panel.add(label);
panel.setPreferredSize(new Dimension(64, 64));
panel.setSize(panel.getPreferredSize());
panel.addNotify();
panel.validate();

Graphics2D g2 = renderer.createGraphics();
g2.scale(1, -1);
g2.translate(0, -128);
	
panel.print(g2);

g2.dispose();
Texture t = renderer.getTexture();