GlEventListener initialization

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

GlEventListener initialization

John No
Hi,

i am currently writing a small visualization plugin for an existing software and have encountered a problem with the initialization of the GEventListener.

The software uses JPanels as container for forms and the GLCanvas of my plugin is added directly to one of these JPanels. In the GLCanvas contructor i'm adding my GLEventListener.
Now, when i open the form for the first time everything works as expected (init is called). When a form is closed the JPanel gets removed from the window, but it is cached in a map datastructure in case the user reenters the form later. This is where the problems start. When the GLCanvas is removed from the visible component hierarchy, the dispose method of the GLEventListener gets called. However, when i reenter the form (parent JPanel is displayable again) it does not call its init method.
The interesting thing is, if i now close and reenter the form again the GLEventListener calls init as expected. It works every second time i enter the form.

Is there a dispose procedure that i should keep to get it working every time? (Currently the dispose method of my GLEventListener doesn't contain anything joglrelated.)

Thanks,
John

Btw, thanks for maintaining the incredibly useful Jogl project.
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

gouessej
Administrator
Hi

Please provide a short example. It's difficult to make exactly the same thing to reproduce your problem, we might "forget" something that you do.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

John No
Sorry, i thought it might be a basic concept issue on my side (therefore no code in the first post).

I built a quick testcase showing my issue:

package misc.canvastest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.jogamp.opengl.util.FPSAnimator;

public class App extends JFrame implements GLEventListener, ActionListener {
	private static final long serialVersionUID = 1895794130238960411L;
	boolean formShow;
	JPanel form;
	FPSAnimator animator;
	
	int clickCount;

	public static void main(String[] args) {
		new App();
	}

	public App() {
		this.setSize(400, 300);

		JButton toggleButton = new JButton();
		toggleButton.setSize(100, 30);
		toggleButton.addActionListener(this);

		form = new JPanel();
		form.setSize(160, 90);

		GLCanvas canvas = new GLCanvas();
		canvas.setSize(160, 90);
		canvas.addGLEventListener(this);

		animator = new FPSAnimator(canvas, 20, true);
		animator.start();

		this.add(toggleButton);
		this.add(form);
		form.add(canvas);

		formShow = true;
		clickCount = 1;
		this.setVisible(true);
	}

	public void init(GLAutoDrawable drawable) {
		System.out.println("init");
	}

	public void dispose(GLAutoDrawable drawable) {
		System.out.println("dispose");
	}

	public void display(GLAutoDrawable drawable) {
		System.out.println("display");
	}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		System.out.println("reshape");
	}

	public void actionPerformed(ActionEvent e) {
		if (formShow) {
			System.out.println("remove Form - click " + clickCount);
			formShow = false;
			clickCount++;
			this.remove(form);

		} else {
			System.out.println("add Form - click " + clickCount);
			formShow = true;
			clickCount++;
			this.add(form);
		}
	}
}

The debug output for 4 button clicks in this program is:
init
reshape
display
display
...
display
display
remove Form - click 1
dispose
add Form - click 2
remove Form - click 3
add Form - click 4
init
reshape
display
display
...

I would expect to get init, reshape and the display loop again after click 2.

I uploaded the maven project for quick testing:
http://www.filedropper.com/canvastest 

EDIT: My Jogl version is 2.1.5-01 and i'm running Java 1.7.0_51

Cheers,
John
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

John No
I found a solution by playing around with the testcase.
Stopping and starting the animator in the dispose and init methods seems to fix the issue.

public void init(GLAutoDrawable drawable) {
		System.out.println("init");
		animator.start();
	}

	public void dispose(GLAutoDrawable drawable) {
		System.out.println("dispose");
		animator.stop();
	}

Is that the right way to do it? Or should the original version have worked?

Cheers,
John
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

gouessej
Administrator
When I read your test case, I thought about a workaround I suggested for a similar problem. Someone used a CardLayout with a JPanel containing several GLCanvas instances. Maybe pausing/resuming the animator would be enough.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

John No
You are right, pause/resume makes more sense than start/stop in this case.
The workaround fixes the issue in my original program as well, so it is good enough for me ;)

Thank you for confirming the solution,
John
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

gouessej
Administrator
You're welcome.

Maybe Sven could confirm what the expected behavior is, especially the lifecycle of the AWT GLCanvas in your test case.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

John No
Short update on the issue, it seems like pause/resume is not enough.
I have to stop the animator to get the init call again.

(Just adding this for future forum readers ;) )
Reply | Threaded
Open this post in threaded view
|

Re: GlEventListener initialization

gouessej
Administrator
Thank you for the feedback. Maybe someone will have to fill a bug report if things become worse...
Julien Gouesse | Personal blog | Website