init() method never called... why?

Hello

For some reason the init method is never getting called, and as a result nothing is displayed on the screen in my app. The following is the code of the GraphicsComponent that I’m trying to get to work with JOGL:

public class GLTest extends GraphicsComponent implements GLEventListener {

	private Plot2DPanel parent = null;
	
	private GLJPanel glJPanel = null;

	private Animator anim = null;

	/** Creates a GLTest
	 * 
	 * @param parent The Plot2DPanel this is stored in
	 */
	public GLTest(Plot2DPanel parent) {
	
		this.parent = parent;
		
		GLCapabilities caps = new GLCapabilities();
		caps.setHardwareAccelerated(true);
		caps.setDoubleBuffered(true);
		
		glJPanel = GLDrawableFactory.getFactory().createGLJPanel(caps);
		glJPanel.addGLEventListener(this);
		glJPanel.setSize(parent.getWidth(),parent.getHeight());
		glJPanel.setVisible(true);
		glJPanel.setOpaque(false);
		
		this.add(glJPanel);
		
		anim = new Animator(glJPanel);
		anim.start();
		
	} // GLTest()
	
	public void paintComponent(Graphics g) {
	
		System.out.println("go");
		glJPanel.repaint();
		
	} // paintComponent()
	
	public void init(GLDrawable gld) {

		gld.setGL(new DebugGL(gld.getGL()));
		
		System.out.println("init");
	
	} // init()
	
	public void display(GLDrawable gld) {
	
		GL gl = gld.getGL();
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
		gl.glLoadIdentity();

		gl.glColor3f(1.0f, 0.0f, 0.0f );

		gl.glBegin( GL.GL_TRIANGLES );
			gl.glVertex3f( 0.0f, 0.0f, 0.0f );
			gl.glVertex3f( 1.0f, 0.0f, 0.0f );
			gl.glVertex3f( 1.0f, 1.0f, 0.0f );
		gl.glEnd();
	
	} // display()
	
	public void reshape(GLDrawable gld, int x, int y, int w, int h) {} // reshape()
	public void displayChanged(GLDrawable gld, boolean modeChng, boolean devChng) {} // displayChanged()

} // GLTest

The only thing about this that might be worth noting that doesn’t have anything to do with JOGL is that a GraphicsComponent is just a class that extends JPanel (and provides some other features for our program), and this will ultimately be added into a component that is inside a JInternalFrame. Could anyone tell me what might be going wrong here?

It doesn’t work either with or without an Animator present, and there is no code anywhere else in the program that uses JOGL (as this is a test file to try and make it work).

Thank you!
-Tom

For the purposes of your test program you don’t need an Animator and due to its pushing the CPU hard you may want to avoid it. Have you looked at the JOGL demos’ source code which use a GLJPanel and tried to extract what you need from e.g. the JGears demo? Do you know what is happening with layout managers, etc., and are you getting the screen real estate you expect for your GLJPanel?

If you aren’t doing so already, I would strongly suggest you use the JSR-231 beta 1 recently released. The GLJPanel has been largely reimplemented which has fixed several preexisting bugs in the 1.1.1 release.

Ok I did quite a bit of work with the demos, and I’ve learned a few things;

I think the problem with the GLJPanel is that I’m adding it into another JPanel before adding it into the JInternalFrame. When the GLJPanels were added straight into the internal frame in the JRefract demo, they worked, but when I added another JPanel as an intermediate, they didn’t work.

However, there is a GLJPanel inserted into a JPanel before into Internal frame in the JGears class (the gradientPanel). I thought there might have been some setting in this that made it work differently, but couldn’t see for sure. This is the code from that:


  // Helper routine for various demos
  public static JPanel createGradientPanel() {
    JPanel gradientPanel = new JPanel() {
        public void paintComponent(Graphics g) {
          ((Graphics2D) g).setPaint(new GradientPaint(0, 0, Color.WHITE,
                                                      getWidth(), getHeight(), Color.DARK_GRAY));
          g.fillRect(0, 0, getWidth(), getHeight());
        }
      };
    gradientPanel.setLayout(new BorderLayout());
    return gradientPanel;
  } 

The only things it does are change the paintComponent() method and make it a border layout. I tried the border layout thing, with the same results of the GLJPanel not working inside my JPanel. I can’t see how paintComponent being different would affect it, unless the default paintComponent method does something that I don’t know about which interferes with adding a GLJPanel.

Any thoughts?

Check the layout of your components. The JPanel you put your GLJPanel inside may have a preferred size of (0, 0) and be causing your GLJPanel to simply disappear. If that isn’t it, could you please write a small and complete test case and either post it or file a bug?

What exactly do you mean by small and complete test case? As someone who is a “n00b” in java games terms, I’m not exactly sure what would constitute one.

The preferred size of that JPanel is in fact 0,0, but when I checked the preferred sizes of the JPanels in my app (and not the JRefract demo) those were not 0,0, so I’m not sure what the issue is. I’ll play around with it some more.

Sources for a program as small as possible reproducing the problem, preferably which don’t require any support from a networked server or similar.

Ok I got the problem solved.

I tried as you suggested, writing a test case that created a JInternalFrame and added in a GLJPanel (through multiple layers of other panels as well). However, it worked. So I had no idea what the problem was that was preventing init from getting called in my main program.

So I went through and traced everything, and it turns out the way graphics components were being handled meant that the GLJPanel was actually never physically added into a JInternalFrame. The graphicscomponents had just had their drawings drawn onto the context of a graphics object already in the internal frame. Since the GLJPanel was never entered into this hierarchy, its init() method was never called, and therefore repaint() was doing nothing.

Thanks a LOT for your help!! I really appreciate it! :slight_smile:

Unfortunately for ya’ll this probably won’t be the last time I ask for such help :wink:

Glad you tracked down your problem. There’s a great quote by Maurice Wilkes which I frequently find myself remembering when I’m debugging something.