Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Write a JPanel to a Texture with TextureRenderer  (Read 1075 times)
0 Members and 1 Guest are viewing this topic.
Offline zeyous

JGO n00b
*

Posts: 23



« on: 2008-04-04 17:25:31 »

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 :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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
Offline Ken Russell

JGO Kernel
*****

Posts: 3446
Medals: 3


Java games rock!


« Reply #1 on: 2008-04-07 10:58:05 »

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.

Offline Saxer

Full Member
**

Posts: 115



« Reply #2 on: 2008-04-07 11:27:10 »

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?
Games published by our own members! Go get 'em!
Offline zeyous

JGO n00b
*

Posts: 23



« Reply #3 on: 2008-04-07 12:29:31 »

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...
Offline bienator

JGO Ninja
***

Posts: 632
Medals: 1


OutOfCoffeeException


« Reply #4 on: 2008-04-07 13:00:53 »

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

Offline zeyous

JGO n00b
*

Posts: 23



« Reply #5 on: 2008-04-07 14:11:07 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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();
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.126 seconds with 21 queries.