Firstly: WOW. I’ve been using GL4Java for about 2 years now, and so I can really appreciate what a fantastic, literally fantastic job you’ve all done putting this together. Excellent cross platform work, and very simple for installation - and I love the debugging of GL errors made easy through the GLExceptions. Well done.
Now for my problem. I’m trying to get a GLCanvas to behave properly when added with other siblings to a parent. I’m finding that as I resize my window, the GLCanvas consumes more and more space, eventually (often) occupying all the available area.
In the following code, I add a label and a GLCanvas, both of which are told to share the remaining space equally. However as I resize the frame containing them both, the GLCanvas consumes more and more space on each resize. Eventually the GLCanvas actually occupies all of the canvas area, and we cannot see the label at all. (A quick maximize then restore operation should do it for you).
import net.java.games.jogl.*;
import java.awt.*;
import javax.swing.*;
public class Test
{
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel c = new JPanel();
c.setPreferredSize(new Dimension(400,400));
f.getContentPane().add(c);
c.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.weightx = gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel p = new JPanel();
p.setBackground(Color.RED);
p.add(new JLabel("don't squeeze me!"));
c.add(p,gbc);
/*
JPanel p2 = new JPanel();
p2.setBackground(Color.BLUE);
p2.add(new JLabel("me neither!"));
c.add(p2,gbc);
*/
GLCanvas glc = GLDrawableFactory.getFactory().createGLCanvas(
new GLCapabilities());
glc.addGLEventListener(
new GLEventListener() {
public void init(GLDrawable drawable) {}
public void display(GLDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
}
public void displayChanged(GLDrawable d,boolean b,boolean c){}
public void reshape(GLDrawable d,int a,int b,int c,int e){}
}
);
c.add(glc,gbc);
f.pack();
f.setVisible(true);
}
}
Some test code (overriding the preferred size operation by using an intermediate class) shows me that what’s happening is that the GLCanvas’ preferred size is continuously increasing each time the GLCanvas is resized. I can therefore avoid the problem by just returning a new Dimension(0,0). But tell me, am I missing something? Is there a good reason for this behaviour?